1

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();

}

tsrac
  • 13
  • 5

2 Answers2

1

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.

2Dee
  • 8,609
  • 7
  • 42
  • 53
  • Yes, but the app is working on the HTC device. So I think that the method is already included in its java file. – Alexis C. Feb 12 '14 at 20:16
  • couldn`t answer my questen because not enough reputation so edited again... since onItemSelected returns boolean, i don`t know how / where to implement the solution of your link ZouZou – tsrac Feb 12 '14 at 20:51
  • meh and my friend with the samsung had to leave, dont know when i see him within the next days, so if there is a solution trying and answering might take some days :( – tsrac Feb 12 '14 at 20:57
  • Although ZouZou is most probably right, maybe the HTC desire version is running a lower than 3.0 version of Android, in which case the onClick attribute declared in xml would be ignored, thus only called and creating problems with the Galaxy S3. tsrac, whenever you have time, review my edit and let us know if anything works for you. No rush, of course, we're trying to help you ;) – 2Dee Feb 12 '14 at 21:17
  • hmmm... i said i didn`t use onClick, but actually had it declared in xml file... but there was no function handling it, but the desire ignored it... – tsrac Feb 12 '14 at 21:29
  • so now i removed the onclick from the xml file from the menu and need to wait a few days if it did the trick xD.... damn :-p but thanks alot! – tsrac Feb 12 '14 at 21:29
  • Yes, my guess is your HTC is an old version (there are actually several versions of the Desire, see http://www.htc.com/www/smartphones/ for the latest ones) running either Froyo or Gingerbread, which will ignore onClick in xml. The S3 will not. Hopefully I was able to help you. Answer in a few days ;) – 2Dee Feb 12 '14 at 21:34
  • Yes its a very old version... actually its 2.2... i know i should flash a newer version... – tsrac Feb 12 '14 at 21:48
  • Aha, we're getting closer to a solution then, I think removing onClick from xml will work. You could run your app on an emulator with JellyBean or KitKat, if you don't want to wait for your friend ;) – 2Dee Feb 12 '14 at 21:59
  • Jep, i already noticed the remaining onClick in the xml file... thought i removed all of them, when it didn`t work on the desire... – tsrac Feb 13 '14 at 08:52
0

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

tsrac
  • 13
  • 5