Java 代码行未初始化,导致nullpointerexecption

Java 代码行未初始化,导致nullpointerexecption,java,android,Java,Android,我有一行代码,当我调试nullpointerexception时,我可以看到这是我的应用程序崩溃的原因 导致空指针的代码行如下所示: ListView storeList = (ListView) findViewById(R.id.storeList); 然后在调试模式下,当它尝试分配以下内容时崩溃: storeList.setAdapter(arrayAdapter2); 我不知道为什么对象没有初始化。下面的ArrayAdapter初始化良好。如果你 下面是实际的java活动文件: pu

我有一行代码,当我调试nullpointerexception时,我可以看到这是我的应用程序崩溃的原因

导致空指针的代码行如下所示:

ListView storeList = (ListView) findViewById(R.id.storeList);
然后在调试模式下,当它尝试分配以下内容时崩溃:

storeList.setAdapter(arrayAdapter2);
我不知道为什么对象没有初始化。下面的
ArrayAdapter
初始化良好。如果你

下面是实际的java活动文件:

public class StoreListView extends Activity {
UserFunctions userFunctions  = new UserFunctions();
ArrayAdapter<String> arrayAdapter2;
ArrayList<String> spinnerArray;



protected void onCreate(Bundle savedInstanceState) {
    ListView storeList = (ListView) findViewById(R.id.storeList);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.storelistviewpage);
    arrayAdapter2 = new ArrayAdapter<String>(StoreListView.this,android.R.layout.simple_list_item_1);
    spinnerArray = getIntent().getStringArrayListExtra("cusName");
    arrayAdapter2.addAll(spinnerArray);
    storeList.setAdapter(arrayAdapter2);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main_screen, menu);
    return true;
}

}
公共类StoreListView扩展活动{
UserFunctions UserFunctions=新的UserFunctions();
ArrayAdapter ArrayAdapter 2;
ArrayList Spinnerary;
创建时受保护的void(Bundle savedInstanceState){
ListView storeList=(ListView)findViewById(R.id.storeList);
super.onCreate(savedInstanceState);
setContentView(R.layout.storelistviewpage);
arrayAdapter2=新的ArrayAdapter(StoreListView.this,android.R.layout.simple\u list\u item\u 1);
SpinneArray=getIntent().GetStringArrayListXTRA(“cusName”);
arrayAdapter2.addAll(spinnerray);
storeList.setAdapter(arrayAdapter2);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u主屏幕,菜单);
返回true;
}
}
下面是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/storeList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center_vertical" >

</ListView>

编辑: 如果有人想知道我是否将活动添加到了android mainfest中

ListView storeList = (ListView) findViewById(R.id.storeList);
在调用
setContentView()之后

由于当前代码在调用
setContentView()
之前分配
findViewById()的结果,因此您将始终获取
storeList
null
,因此,您将获得一个NPE。

移动

ListView storeList = (ListView) findViewById(R.id.storeList);
在调用
setContentView()之后


由于当前代码在调用
setContentView()
之前分配
findViewById()的结果,因此您将始终获取
storeList
null
,因此,您将获得一个NPE。

这太完美了!!!我不敢相信是这样。我还以为是别的事。oncreate中的所有内容都必须在这两行之后吗?@Mark这是一种很好的做法,因为
findViewById()
返回所需的视图,只有在设置了
contentView
并且传递的
Id
有效时,才会发生这种情况。在
setContentView()
之前,我能想到的唯一一件事就是请求窗口功能。@如果这个答案对您有帮助,您应该将它标记为已接受的答案。如果你还有其他问题,最好是再发一个问题。对此我很抱歉。它告诉我我必须在15分钟内回来接受它,因为你回答得太快了。太完美了!!!我不敢相信是这样。我还以为是别的事。oncreate中的所有内容都必须在这两行之后吗?@Mark这是一种很好的做法,因为
findViewById()
返回所需的视图,只有在设置了
contentView
并且传递的
Id
有效时,才会发生这种情况。在
setContentView()
之前,我能想到的唯一一件事就是请求窗口功能。@如果这个答案对您有帮助,您应该将它标记为已接受的答案。如果你还有其他问题,最好是再发一个问题。对此我很抱歉。它告诉我我必须在15分钟内回来接受它,因为你回答得太快了。