0
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_screen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_settings:
        Intent Options = new Intent(this,
                Options.class);
        this.startActivity(Options);
        break;
    default:
        return super.onOptionsItemSelected(item);
    }

    return true;
}

我的 onOptionsItemSelected() 没有将我发送到我设置的活动。它只是让我回到上一个屏幕。没有错误或任何东西,只是把我踢回屏幕?我也在清单文件中声明了 Options.class。

有任何想法吗?

编辑:

选项.class

import android.app.Activity;
import android.os.Bundle;

public class Options extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.options);
   }
}

main_screen.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_settings"
    android:title="@string/menu_settings"
    android:orderInCategory="100"
    android:showAsAction="never" />

LogCat:只需将我发送到活动,然后立即返回

02-04 12:26:55.494: I/ActivityManager(150): Starting activity: Intent { cmp=com.app.test/.Options } from pid 7978
02-04 12:26:55.504: I/ActivityManager(150): Starting activity: Intent { cmp=com.app.test/.MainScreen (has extras) } from pid 7978
4

2 回答 2

0

对我来说,它似乎没有进入正确的 catch 块并运行默认选项。试试看:

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main_screen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_settings:
        Intent Options = new Intent(this,
                Options.class);
        this.startActivity(Options);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }


}

如果还不行,放Log或者toast之类的看看能不能进去

 case R.id.menu_settings:
于 2013-02-04T18:50:44.983 回答
0

尝试相同但没有

case R.id.menu_settings:
    Intent Options = new Intent(MainActivity.this, Options.class);
    startActivity(Options);
        return true;

它对我有用,我希望它有所帮助。

于 2016-08-24T18:57:01.180 回答