我有一行代码,当我调试空指针异常时,我可以看到这是我的应用程序崩溃的原因。
导致空指针的代码行是这样的:
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;
}
}
下面是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