Android Studio:添加操作栏项目 "onClick" 遇到 "FATAL EXCEPTION"

Android Studio: Adding Action Bar Item with "onClick" encounters "FATAL EXCEPTION"

所以我开始开发我的第一个 Android 应用程序,但我已经碰壁了。我想从顶部操作菜单中的 "Create" 按钮开始,该按钮能够创建新联系人。但是后来我遇到了一些我无法理解的错误,它一直在阻碍我的进程。

当我将 "onClick" 行添加到操作项时开始出现错误,此后,在我的模拟器上停止了 运行。

谢谢。

MainActivity.java

package com.xephos.detra;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

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

@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_main, 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) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void create(View v){
    TextView tv = (TextView) findViewById(R.id.test);
    tv.setText("Working!");
}
}

这可能有帮助:menu_main.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=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />

<item
    android:id="@+id/add"
    android:title="Add New Contact"
    app:showAsAction ="always"
    android:icon="@drawable/ic_add_white_48dp"
    android:onClick="create"/>
</menu>

最后,错误:

20653-20653/com.xephos.detra E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.xephos.detra, PID: 20653
android.view.InflateException: Couldn't resolve menu item onClick handler create in class com.xephos.detra.MainActivity
        at android.support.v7.internal.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:242)
        at android.support.v7.internal.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:443)
        at android.support.v7.internal.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:479)
        at android.support.v7.internal.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
        at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
        at com.xephos.detra.MainActivity.onCreateOptionsMenu(MainActivity.java:22)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2823)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:277)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:275)
        at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1117)
        at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1399)
        at android.support.v7.app.AppCompatDelegateImplV7.access0(AppCompatDelegateImplV7.java:89)
        at android.support.v7.app.AppCompatDelegateImplV7.run(AppCompatDelegateImplV7.java:126)
        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:5257)
        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)
 Caused by: java.lang.NoSuchMethodException: create [interface android.view.MenuItem]
        at java.lang.Class.getMethod(Class.java:664)
        at java.lang.Class.getMethod(Class.java:643)
        at android.support.v7.internal.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:240) ...

create 方法需要将 MenuItem 作为参数,而不是 View

public void create(MenuItem item){
    TextView tv = (TextView) findViewById(R.id.test);
    tv.setText("Working!");
}

或者,删除菜单项的 android:onClick 属性并通过检查项目的 ID 处理 onOptionsItemSelected() 中的点击:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.add:
            // your code here
            return true;
        case R.id.action_settings:
            // copied from auto-generated stub
            return true;
    }
    return super.onOptionsItemSelected(item);
}

两者都行,我更喜欢第二种方法。

最好以编程方式进行。删除 XML 中的 'android:OnClick' 行,然后在设置 'tv' 的文本后输入:

   tv.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       //Process click here
   });

这应该可以解决问题![​​=11=]