每当我旋转设备时,应用程序崩溃,我无法弄清楚原因。这是LogCat信息:
07-26 10:11:48.260: E/AndroidRuntime(25775): FATAL EXCEPTION: main
07-26 10:11:48.260: E/AndroidRuntime(25775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cleanlist/com.cleanlist.MainActivity}: java.lang.NullPointerException
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3554)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.app.ActivityThread.access$800(ActivityThread.java:139)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1230)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.os.Looper.loop(Looper.java:137)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.app.ActivityThread.main(ActivityThread.java:4918)
07-26 10:11:48.260: E/AndroidRuntime(25775): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 10:11:48.260: E/AndroidRuntime(25775): at java.lang.reflect.Method.invoke(Method.java:511)
07-26 10:11:48.260: E/AndroidRuntime(25775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
07-26 10:11:48.260: E/AndroidRuntime(25775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
07-26 10:11:48.260: E/AndroidRuntime(25775): at dalvik.system.NativeStart.main(Native Method)
07-26 10:11:48.260: E/AndroidRuntime(25775): Caused by: java.lang.NullPointerException
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.widget.ListView.setAdapter(ListView.java:466)
07-26 10:11:48.260: E/AndroidRuntime(25775): at com.cleanlist.MainActivity$DummySectionFragment.onCreateView(MainActivity.java:307)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.app.Activity.performStart(Activity.java:5058)
07-26 10:11:48.260: E/AndroidRuntime(25775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
07-26 10:11:48.260: E/AndroidRuntime(25775): ... 12 more
以下是问题所在的代码部分,它将问题指向listView.setAdapter(listadapter);
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public transient ListObject list;
public static final String ARG_SECTION_NUMBER = "section_number";
private View thisView;
private MainActivity mainActivity;
private CustomListAdapter listadapter;
public DummySectionFragment() {
}
public DummySectionFragment(MainActivity act, CustomListAdapter adp, ListObject thisList)
{
list = thisList;
mainActivity = act;
listadapter = adp;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.list_layout, container, false);
final int whichList = getArguments().getInt(ARG_SECTION_NUMBER);
listadapter = new CustomListAdapter(getActivity(), R.layout.list_row, list);
ListView listView = (ListView)rootView.findViewById(R.id.listView);
if (listView != null)
listView.setAdapter(listadapter);
else
Log.e("wbbug","ListView is NULL");
listView.setSelection(1);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Log.d("wbbug","List item click: " + position + "List size: " + list.size());
}
});
thisView = rootView;
return rootView;
}
答案 0 :(得分:0)
ArrayAdapter getCount方法中存在空指针异常。所以很可能你的列表已经变为空。轮换时,活动将被重新创建,并且您的列表可能无法正确初始化。