gpt4 book ai didi

java - ArrayAdapter.clear() 上的 NPE

转载 作者:行者123 更新时间:2023-12-01 23:15:13 24 4
gpt4 key购买 nike

在初始化之后,我在 if(adapter.getCount() != 0)adapter.clear(); 行得到了一个 NPE。我的代码哪里错了?我认为因为我刚刚初始化了它,所以不应该有 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?提前致谢

最佳答案

您声明了两次ArrayAdapter<String> adapter ,一旦进入onViewCreated方法,并作为类的成员一次。

当你在 onViewCreated 中初始化它时,事实上它正在初始化一个局部变量,该变量在 popolamentoSpinner 中不可见。 。基本上,这两个函数中的变量不是相同的,因此实际上您永远不会初始化名为 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);
...

关于java - ArrayAdapter.clear() 上的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com