Question

My app runs perfectly on my HTD Desire:

Java snippet:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.plans_screen, menu);
    return true;
    }

and my xml File:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:id="@+id/newPlan"
      android:visible="true" 
      android:title="Neuer Plan"
      android:onClick="addPlan"></item>
     <item
     android:id="@+id/menu_main_spinner"
      android:visible="true" 
     android:title="Lade Plan"
     android:showAsAction="always"></item>
     </menu>

But if I run it on a Samsung Galaxy S3 the app crashes and stops working when I hit Menubutton. Does the Samsung Galaxy S3 need some special treatment?

Or is there something wrong in my Menu?

Thanks in advance, Tom

P.S. Stacktrace:

02-12 20:56:24.090: E/AndroidRuntime(25656): FATAL EXCEPTION: main
02-12 20:56:24.090: E/AndroidRuntime(25656): android.view.InflateException: Couldn't resolve menu item onClick handler addPlan in class com.example.myfitnessapp.PlansScreen
02-12 20:56:24.090: E/AndroidRuntime(25656):    at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:218)
02-12 20:56:24.090: E/AndroidRuntime(25656):    at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:422)
02-12 20:56:24.090: E/AndroidRuntime(25656):    at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:456)
02-12 20:56:24.090: E/AndroidRuntime(25656):    at android.view.MenuInflater.parseMenu(MenuInflater.java:189)
02-12 20:56:24.090: E/AndroidRuntime(25656):    at android.view.MenuInflater.inflate(MenuInflater.java:111)
02-12 20:56:24.090: E/AndroidRuntime(25656):    at com.example.myfitnessapp.PlansScreen.onCreateOptionsMenu(PlansScreen.java:146)
02-12 20:56:24.090: E/AndroidRuntime(25656):    at android.app.Activity.onCreatePanelMenu(Activity.java:2578)
02-12 20:56:24.090: E/AndroidRuntime(25656):    at 

[...]

P.P.S.

I don`t use the onclick method...

my code is as follows:

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
   case R.id.newPlan:
    addPlan();
     break;
   case R.id.menu_main_spinner:
       loadPlan();
      break;
   }
   return super.onOptionsItemSelected(item);
  }

i have funtcioning methods addPlan() :

public void addPlan() {
final EditText input = new EditText(PlansScreen.this);
new AlertDialog.Builder(PlansScreen.this)
.setTitle("Erstell einen neuen Plan")
.setMessage("Wie soll der soll der Plan heissen?")
.setView(input)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        String value = input.getText().toString(); 
        Data.plannames.add(value);
        Data.trainingsplaene.add(new trainingsplan(value));
        spinadapter.notifyDataSetChanged();
        Data.currentPlan = Data.trainingsplaene.size()-1;
        listadapter=new myListViewAdapter(PlansScreen.this,Data.trainingsplaene.get(Data.currentPlan));
        myList2.setAdapter(listadapter);
        listadapter.notifyDataSetChanged();

    }
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // Do nothing.
    }
}).show();

}

Was it helpful?

Solution

As per the stacktrace, this is what seems to be causing the issue :

java.lang.NoSuchMethodException: addPlan [interface android.view.MenuItem]

When defining onClick method for a menu item in xml, you should provide a method with the correct name, taking a single MenuItem object as argument. Quoting the docs :

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

You should include the following method in your Activity code as this will be called by the system when you declare onClick in xml even if you also have onOptionsItemSelected in your code :

public boolean addPlan (MenuItem menuItem){
    // Your code here
}

You best bet would probably be to use only onOptionsItemSelected(), because that method should work on all versions of Android whereas declaring onClick in xml will be ignored for devices running Gingerbread or lower. So as an alternative, you could do this :

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
        case R.id.profile_edit: 
            addPlan();
            return true;
        case R.id. menu_main_spinner:
            loadPlan();
            return true;
    }
}

... and remove the onClick declarations from your menu xml.

OTHER TIPS

Just removed the remaining onClick from xml file... sometimes i feel like so blind!

But it was stupid to detect, when running on my phone and no compiling errors :(

Thanks for the help! :)

Since i can`t mark a comment as Solution, i just answered, thank you a lot ZouZou and 2Dee :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top