AndreaNobili AndreaNobili - 5 months ago 16
Android Question

Why I obtain this RuntimeException when I try to set a different content view into the onCreate() method of my activity?

I am absolutly new in Android development and I have the following problem.

I have a MainActivity class that contains this onCreate() method that is performed at the startup of my application:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setContentView(R.layout.activity_screen_slide);

// Instantiate the list of samples.
mSamples = new Sample[] {
new Sample(R.string.title_screen_slide, ScreenSlideActivity.class),

};

setListAdapter(new ArrayAdapter<Sample>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
mSamples));
}


As you can see in the previous code snippet currently it is performed at the startup application and show the layout.activity_main layout that is defined into the res/layout/layout.activity_main.xml file, that only contain this code:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" />


So, I tryed to modify the previous method in order that is shown another view. So basically I replaced this line:

setContentView(R.layout.activity_main);


whith this one:

setContentView(R.layout.activity_screen_slide);


that set the res/layout/layout.activity_screen_slide.xml view, that only contains a ViewPager element, this one:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />


The problem is that now when this line is performed:

setContentView(R.layout.activity_screen_slide);


I obtain this exception:

06-11 17:43:58.167 28738-28738/com.example.android.animationsdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.animationsdemo, PID: 28738
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.animationsdemo/com.example.android.animationsdemo.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:408)
at android.app.Activity.setContentView(Activity.java:2156)
at com.example.android.animationsdemo.MainActivity.onCreate(MainActivity.java:61)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5696) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 


Why? How is it possible? The ListView element is declared inside the first layout.activity_main that is replaced by this instruction:

setContentView(R.layout.activity_screen_slide);


Why I obtain this exception? What am I missing? How can I try to solve this issue?

Answer

I assume that your MainActivity inherits from ListActivity because you are using the method setListAdapter.

If you don't want to use a ListView then you can inherit from Activity.

But if you want to continue using a ListView then add it to layout.activity_screen_slide.

Comments