ArrayAdapter.clear()上的NPE

时间:2014-01-25 14:24:54

标签: java android nullpointerexception runtime-error

在我初始化之后,我在if(adapter.getCount() != 0) adapter.clear();行找到了一个N​​PE。我的代码在哪里错了?我认为,因为我刚刚初始化它,不应该有NPE,但我错了.. 在这里,我将3个方法重新放入适配器:

    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? 提前致谢

2 个答案:

答案 0 :(得分:2)

您在ArrayAdapter<String> adapter方法中声明了两次onViewCreated,并且在该类中成为一次。

onViewCreated初始化时,实际上它正在初始化一个在popolamentoSpinner中无法看见的局部变量。基本上,它与2个函数中的变量不同,所以实际上你永远不会初始化在adapter中使用的名为popolamentoSpinner的变量!

一种解决方案是删除onViewCreated中的声明,以便您确定初始化成员变量:

@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);
    ...

答案 1 :(得分:1)

您在onViewCreated()中声明的适配器是一个局部变量,因此您尝试在private ArrayAdapter<String> adapter;中访问的字段popolamentoSpinner()仍为null。在onViewCreated()执行:

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