1

So I'm starting to work on my first Android App and I've already hit a wall. I want to start off with a "Create" button at the action menu at the top that has the ability to create new contacts. But then I hit some sort of error I can't understand and it's been hindering my process.

The error started when I added the "onClick" line tot he action item and since then, stopped running on my emulator.

Thank you.

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!");
}
}

This might help: the 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>

And finally, the error:

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) ...

            

Xephos
  • 63
  • 1
  • 11

2 Answers2

3

You create method needs to take a MenuItem as the argument, not a View.

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

Alternatively, remove the android:onClick attribute of your menu item and handle the click in onOptionsItemSelected() by checking the id of the item:

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

Either will work, my preference is the second approach.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • What would make a decisive difference? For example, the add will have the method to create a new contact that will have values assigned to that contact. – Xephos Jul 29 '15 at 06:11
  • 1
    It's a matter of preference. I prefer the second because it doesn't require any "magic"--I always know where menu item clicks are handled and which ones are handled. I ever want to use the same menu resource in more than one place, or if I decide to remove that particular item from the menu XML, I don't have to remember that there are these special methods to consider. – Karakuri Jul 29 '15 at 13:57
0

You'd be better off doing it programmatically. Get rid of the 'android:OnClick' line in the XML, then after you set the text of 'tv' put in this:

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

This should do the trick!

Joseph Meadows
  • 160
  • 2
  • 8