NPE上ArrayAdapter.clear() [英] NPE on ArrayAdapter.clear()

查看:232
本文介绍了NPE上ArrayAdapter.clear()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该行获得了NPE 如果(adapter.getCount()!= 0)adapter.clear(); 就在我已经初始化它。凡我错了,在我的code?我认为,因为我刚刚初始化它,不应该有一个NPE,但我错了..
在这里,我把3种方法相关者适配器:

I get a NPE at the line if(adapter.getCount() != 0) adapter.clear(); just after I have initialized it. Where I'm wrong in my code? I think that because I had just initialized it, there should not be a NPE, but I'm wrong.. Here I put 3 methods relater to the adapter:

    package com.example.app.Fragment;
public class FragmentGestioneCorsa extends Fragment implements AdapterView.OnItemSelectedListener{

@SuppressWarnings("unused")
private static final String TAG = FragmentGestioneCorsa.class.getSimpleName();

private Context context;
private DatabaseLocale db;
private ArrayAdapter<String> adapter;
private  Spinner spnLinee;
private Button aggiornamento;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    linee = new ArrayList<HashMap<String, String>>();
    context = getActivity();
    return inflater.inflate(R.layout.fragment_gestione_corsa, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    spnLinee = (Spinner) getView().findViewById(R.id.spinnerLinee);
    ArrayAdapter<String> adapter =new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
    spnLinee.setAdapter(adapter);
    spnLinee.setOnItemSelectedListener(this);
    popolamentoSpinner();

    aggiornamento = (Button) getView().findViewById(R.id.buttonAggiornamento);
    aggiornamento.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Se scrivo DownloadLinee.execute() ottengo l'errore:
            // If I write DownloadLinee.execute() I get the error:
            // Non-static method cannot be referenced from a static contecxt
            DownloadLinee l = new DownloadLinee();
            l.execute();
        }
    });
}

private void popolamentoSpinner(){
    if(adapter.getCount() != 0) adapter.clear();
    db = new DatabaseLocale(context);
    SQLiteDatabase dbLeggibile = db.getReadableDatabase();
    String[] colonne = {DatabaseLocale.getTagCodiceLinea(), DatabaseLocale.getTagNomeLinea()};
    String tabella = DatabaseLocale.getTableName();
    Cursor cursore = dbLeggibile.query(tabella, colonne, null, null, null, null, null);
    while(cursore.moveToNext()){
        adapter.add(cursore.getString(0) + " " + cursore.getString(1));
    }
    cursore.close();
    db.close();
}
}

为什么这个NPE?
在此先感谢

Why this NPE? Thanks in advance

推荐答案

您正在申报两次 ArrayAdapter&LT;串GT;适配器,曾经在 onViewCreated 方法,另一次是在类的成员。

You are declaring twice ArrayAdapter<String> adapter, once in the onViewCreated method, and once as a member of the class.

当您在 onViewCreated 初始化它,其实它的初始化它在 popolamentoSpinner 不可见的局部变量。基本上,它是不是在2功能相同的变量,所以实际上你从来没有初始化称为varibale 适配器您在使用 popolamentoSpinner

When you initialize it in onViewCreated, in fact it's initializing a local variable which has no visibility in popolamentoSpinner. Basically, it's not the same variable in the 2 functions, so in fact you never initialize the varibale called adapter that you use in popolamentoSpinner!

一个解决办法是去除 onViewCreated 的声明,所以你确信你初始化成员变量:

One solution would be to remove the declaration in onViewCreated so you are sure that you initialize the member variable:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    spnLinee = (Spinner) getView().findViewById(R.id.spinnerLinee);
    adapter =new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
    spnLinee.setAdapter(adapter);
    ...

这篇关于NPE上ArrayAdapter.clear()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆