android - Activity 与选项菜单崩溃

标签 android android-activity crash

此 Activity 在启动时崩溃

package rian.derous;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.Toast;

public class TargetOverviewActivity extends Activity {

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

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }

    public void createTarget() {
        Toast toast = Toast.makeText(this, "create Target", Toast.LENGTH_SHORT);
        toast.show();
    }

    public void refreshTarget() {
        Toast toast = Toast.makeText(this, "refresh Target", Toast.LENGTH_SHORT);
        toast.show();
    }

    public void deleteAllTargets(){
        Toast toast = Toast.makeText(this, "delete Target", Toast.LENGTH_SHORT);
        toast.show();
    }

}

我认为 onCreateOptionsMenu 有问题,当我将那段代码放在注释中时,它不会在启动时崩溃。

遵循了我们的 Android 类(class)手册中的所有步骤......有人可以帮助我吗?非常感谢!

菜单文件夹中的菜单文件
   <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:id="@+id/createTarget" android:title="Create" android:onClick="createTarget"></item>
        <item android:id="@+id/refreshTarget" android:title="refresh" android:onClick="refreshTarget"></item>
        <item android:id="@+id/deleteAllTargets" android:title="Delete All" android:onClick="deleteAllTargets"></item>



    </menu>

我的项目 list
    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="rian.derous"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".DetailActivity"
            android:label="@string/app_name" >

        </activity>
        <activity
            android:name=".TargetOverviewActivity"
            android:label="@string/title_activity_target_overview" >
           <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我使用 Eclipse 作为 IDE。

最佳答案

http://developer.android.com/guide/topics/ui/menus.html

Tip: Android 3.0 adds the ability for you to define the on-click behavior for a menu item in XML, using the android:onClick attribute. The value for the attribute must be the name of a method defined by the activity using the menu. The method must be public and accept a single MenuItem parameter—when the system calls this method, it passes the menu item selected. For more information and an example, see the Menu Resource document.



或者你可以这样做:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.createTarget:
            createTarget();
            return true;
        case R.id.refreshTarget:
            refreshTarget();
            return true;
        case R.id.deleteAllTargets:
            deleteAllTargets();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

关于android - Activity 与选项菜单崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27389547/

相关文章:

ios - Xcode 错误 - 由于未捕获的异常 'NSUnknownKeyException' 导致应用程序终止,原因 : '[ setValue:forUndefinedKey:]:

android - -表格中的webkit-gradient导致Android中出现边框线

android - 发送内容不可编辑的电子邮件

java - Android - 关闭一个 Activity 后,当我再次运行该应用程序时,两个 Activity 同时运行。我怎样才能避免它?

Android Intent Activity 崩溃行为 - 应用程序在应用程序中途重启

android - Android无法更改按钮文字

android - 如何在Android中获取文件的绝对路径?

java - 我收到以下运行时错误。请帮我找出原因

java - 在定义的时间段后关闭 Activity

android - 如何杀死一个 Activity