ListActivity与单击RadioButton菜单崩溃

时间:2017-03-08 23:28:41

标签: java android listview menu android-actionbar

我正在尝试将“设置”菜单项添加到我的溢出菜单中。 “设置”菜单包含RadioButtons。问题是,当我单击“设置”菜单项时,活动崩溃。

我的代码出了什么问题?

感谢您的帮助。

Java代码:

    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)

1 个答案:

答案 0 :(得分:0)

在您onOptionsItemSelected内调用setContentView(),它会将您的活动布局设置为您要用作菜单布局的布局。

该布局不包含任何ListView,这就是您收到错误的原因。

我认为您想要打开一个新活动或片段,而不是更改当前的一个布局。

活动示例:

内部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;
    }
    ...

然后使用如下所示的假装布局创建一个AudioPlayerSettings类:

public class AudioPlayerSettings extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audio_player_settings);
    }
}