问题
I'm working for hours on an error I get, when I want to start a ListActivity. Short workaround about what I want to do: I have a main application with a normal menu, from which I want to start a setting list where the user can add specific settings.
I'm trying to start the ListView like this:
startActivity(new Intent(this, FileManagerSettings.class));
Then in the on Create method I got the error:
userSettings = deserializeObject();
if(userSettings.isEmpty())
{
userSettings.add(new SettingItem("Camera", android.os.Environment.DIRECTORY_DCIM, false));
}
super.onCreate(icicle);
Context mContext = this.getApplicationContext();
settingsList = (ListView)findViewById(R.id.list_settings);
settingsList.setAdapter(new CustomSettingsAdapter(mContext, userSettings));
The last line causes the error. For better understanding here the Constructor of the adapter class:
public CustomSettingsAdapter(Context context, ArrayList<SettingItem> sitems)
{
settingsArrayList = sitems;
mInflater = LayoutInflater.from(context);
}
And here is the error from the logcat: (Sorry, don't know how to format it right)
04-01 11:07:26.756: ERROR/AndroidRuntime(375): FATAL EXCEPTION: main 04-01 11:07:26.756: ERROR/AndroidRuntime(375): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.openintents.filemanager/org.openintents.filemanager.FileManagerSettings}: java.lang.NullPointerException 04-01 11:07:26.756: ERROR/AndroidRuntime(375): Caused by: java.lang.NullPointerException 04-01 11:07:26.756: ERROR/AndroidRuntime(375): at org.openintents.filemanager.FileManagerSettings.onCreate(FileManagerSettings.java:43)
In my thinking the error is caused because the LayoutInflater of the context is null... But I don't know how to get the LayoutInflater then and even don't know if this is really the error...
edit: error log with using setContentView():
04-01 12:06:56.315: ERROR/AndroidRuntime(669): FATAL EXCEPTION: main 04-01 12:06:56.315: ERROR/AndroidRuntime(669): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.openintents.filemanager/org.openintents.filemanager.FileManagerSettings}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
this is the xml-File:
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/list"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/settings_list_title"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/settings_header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center" />
<ListView android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
回答1:
findViewById() only returns a valid reference to a View if the layout of the Activty has been set. Looking at the code you have pasted in to your question, it does not appear that you are making a call to
setContentView(R.layout.whatever_your_layout_file_id);
Your variable settingsList, would then be a null reference
来源:https://stackoverflow.com/questions/5512710/nullpointerexception-when-starting-listactivity