-1

I am trying to add a Settings menu item to my overflow menu. The Settings menu has RadioButtons. The problem is that when I click on the Settings menu item the activity crashes.

What is wrong with my code?

Thank you for your help.

Java code:

    public class AudioPlayer extends ListActivity


@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.menu_action, menu);

        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {

            setContentView(R.layout.audio_player_settings);

            makeText(this, "Settings Menu Item Clicked", LENGTH_SHORT).show();

            return true;
        }

    return super.onOptionsItemSelected(item);

    }

menu_action.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".PlayerActivity">

    <item android:id="@+id/action_popup" android:title="Sort Popup" android:icon="@drawable/ic_sort"
        android:showAsAction="always" />

    <item android:id="@+id/action_settings" android:title="Settings"
        app:showAsAction="never" />

    <item android:id="@+id/action_help" android:title="Help"
        app:showAsAction="never" />

audio_player_settings.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".PlayerActivity">

    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/radio_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton android:id="@+id/high_quality"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="High"
            android:onClick="onRadioButtonClicked"/>
        <RadioButton android:id="@+id/medium_quality"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium"
            android:onClick="onRadioButtonClicked"/>
        <RadioButton android:id="@+id/low_quality"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Low"
            android:onClick="onRadioButtonClicked"/>
    </RadioGroup>
</RelativeLayout>

Logcat:

03-08 12:22:10.300  31930-31930/com.test.audio E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.test.audio, PID: 31930
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:382)
        at android.app.Activity.setContentView(Activity.java:2145)
        at com.test.audio.TouchActivity.onOptionsItemSelected(TouchActivity.java:2833)
        at android.app.Activity.onMenuItemSelected(Activity.java:2885)
        at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1133)
        at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
        at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:186)
        at android.widget.AdapterView.performItemClick(AdapterView.java:305)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
        at android.widget.AbsListView$3.run(AbsListView.java:3860)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        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:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
user2308699
  • 239
  • 1
  • 5
  • 14

1 Answers1

0

Inside your onOptionsItemSelected you are calling setContentView() which will set the layout of you Activity to the layout you want to use as the menu layout.

That layout doesn't contain any ListView so that's why you get the error.

I think you wanted to open a new activity or fragment instead of changing the current one layout.

Example for Activity:

Inside onOptionsItemSelected if:

    ...
    if (id == R.id.action_settings) {

        makeText(this, "Settings Menu Item Clicked", LENGTH_SHORT).show();

        Intent intent = new Intent(this, AudioPlayerSettings.class);
        startActivity(intent);

        return true;
    }
    ...

Then create an AudioPlayerSettings class using the pretended layout like the following:

public class AudioPlayerSettings extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audio_player_settings);
    }
}
Rui Cardoso
  • 739
  • 6
  • 18
  • Thanks, The Settings menu is working. Is there a way to show an arrow symbol at the top left of the Settings activity to click for return to previous activity? – user2308699 Mar 09 '17 at 02:52
  • That depends on if you are using a custom toolbar or not. But follow this thread: http://stackoverflow.com/questions/26651602/display-back-arrow-on-toolbar-android Don't forget to select the answer as correct if it helped you, thanks – Rui Cardoso Mar 09 '17 at 08:48