Java 阵列适配器上的NPE.clear()

Java 阵列适配器上的NPE.clear(),java,android,nullpointerexception,runtime-error,Java,Android,Nullpointerexception,Runtime Error,我在if(adapter.getCount()!=0)adapter.clear()行得到一个NPE就在我初始化它之后。我的代码哪里错了?我认为,因为我刚刚初始化了它,所以不应该有NPE,但我错了。。 在这里,我将3种方法放在适配器上: package com.example.app.Fragment; public class FragmentGestioneCorsa extends Fragment implements AdapterView.OnItemSelectedList

我在
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();
}
}
package com.example.app.Fragment;
公共类FragmentGestineCorsa扩展片段实现AdapterView.OnItemSelectedListener{
@抑制警告(“未使用”)
private static final String TAG=FragmentGestioneCorsa.class.getSimpleName();
私人语境;
专用数据库语言环境数据库;
专用阵列适配器;
私人纺纱机;
专用按钮aggiornamento;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
linee=新的ArrayList();
context=getActivity();
返回充气机。充气(R.layout.fragment_-gestione_-corsa,容器,假);
}
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
spnLinee=(微调器)getView().findViewById(R.id.spinnerLinee);
ArrayAdapter=新的ArrayAdapter(上下文,android.R.layout.simple\u微调器\u项);
spnline.setAdapter(适配器);
spnline.setOnItemSelectedListener(此);
popolamentoSpinner();
aggiornamento=(按钮)getView().findViewById(R.id.buttonagiornamento);
aggiornamento.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//Se scrivo DownloadLinee.execute()ottengo l'errore:
//如果我写DownloadLinee.execute(),我会得到错误:
//无法从静态contecxt引用非静态方法
DownloadLinee l=新的DownloadLinee();
l、 执行();
}
});
}
私有void popolamentoSpinner(){
如果(adapter.getCount()!=0)adapter.clear();
db=新数据库语言环境(上下文);
SQLiteDatabase dbLeggibile=db.getReadableDatabase();
字符串[]colonne={DatabaseLocale.getTagCodiceLinea(),DatabaseLocale.getTagNomeLinea()};
字符串tabella=DatabaseLocale.getTableName();
Cursor cursore=dblegibile.query(tabella,colonne,null,null,null,null);
while(cursore.moveToNext()){
add(cursore.getString(0)+“”+cursore.getString(1));
}
cursore.close();
db.close();
}
}
为什么是NPE?
提前感谢

您在
onViewCreated()
中声明的适配器是一个局部变量,因此字段
private ArrayAdapter adapterpopolamentoSpinner()
中尝试访问的代码>,仍将是
null
。在
onViewCreated()
do中:

spnLinee = (Spinner) getView().findViewById(R.id.spinnerLinee);
adapter =new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
spnLinee=(微调器)getView().findviewbyd(R.id.spinnerLinee);
adapter=newarrayadapter(上下文,android.R.layout.simple\u微调器\u项);

onViewCreated()
中声明的适配器是一个局部变量,因此字段
private ArrayAdapter adapterpopolamentoSpinner()
中尝试访问的代码>,仍将是
null
。在
onViewCreated()
do中:

spnLinee = (Spinner) getView().findViewById(R.id.spinnerLinee);
adapter =new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
spnLinee=(微调器)getView().findviewbyd(R.id.spinnerLinee);
adapter=newarrayadapter(上下文,android.R.layout.simple\u微调器\u项);

onViewCreated()
中声明的适配器是一个局部变量,因此字段
private ArrayAdapter adapterpopolamentoSpinner()
中尝试访问的代码>,仍将是
null
。在
onViewCreated()
do中:

spnLinee = (Spinner) getView().findViewById(R.id.spinnerLinee);
adapter =new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
spnLinee=(微调器)getView().findviewbyd(R.id.spinnerLinee);
adapter=newarrayadapter(上下文,android.R.layout.simple\u微调器\u项);

onViewCreated()
中声明的适配器是一个局部变量,因此字段
private ArrayAdapter adapterpopolamentoSpinner()
中尝试访问的代码>,仍将是
null
。在
onViewCreated()
do中:

spnLinee = (Spinner) getView().findViewById(R.id.spinnerLinee);
adapter =new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
spnLinee=(微调器)getView().findviewbyd(R.id.spinnerLinee);
adapter=newarrayadapter(上下文,android.R.layout.simple\u微调器\u项);

您将声明两次
ArrayAdapter
,一次在
onViewCreated
方法中,一次作为类的成员

当您在
onViewCreated
中初始化它时,实际上它正在初始化一个局部变量,该变量在
popolamentoSpinner
中不可见。基本上,这两个函数中的变量不同,因此事实上,您从未初始化在
popolamentoSpinner
中使用的名为
adapter
的变量

一种解决方案是删除
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);
    ...
@覆盖
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
spnLinee=(微调器)getView().findViewById(R.id.spinnerLinee);
adapter=newarrayadapter(上下文,android.R.layout.simple\u微调器\u项);
spnline.setAdapter(适配器);
...

您将声明两次
ArrayAdapter
,一次在
onViewCreated
方法中,一次作为类的成员

当您在创建的视图中初始化它时,实际上它是在初始化一个局部变量,该变量在
popolamentoSpinner
中不可见。基本上,这两个函数中的变量不同,因此实际上您从未初始化在
popolamentoSpinner
中使用的名为
adapter
的变量

一种解决方案是删除
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);
    ...
@覆盖
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
spnLinee=(微调器)getView().findViewById(R.id.spinnerLinee);
适配器=新的ArrayAdapter(上下文,一个