1

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

当我在操作项中添加“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.access$100(AppCompatDelegateImplV7.java:89)
        at android.support.v7.app.AppCompatDelegateImplV7$1.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) ...

            

4

2 回答 2

3

create的方法需要将 aMenuItem作为参数,而不是 a View

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

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

@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);
}

两者都行,我的偏好是第二种方法。

于 2015-07-28T04:41:53.103 回答
0

你最好以编程方式进行。去掉 XML 中的 'android:OnClick' 行,然后在设置 'tv' 的文本后放入:

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

这应该可以解决问题!

于 2015-07-28T04:50:26.763 回答