質問

The problem is, that if I press the hardware button for the OptionsMenu, the app freezes and after a half second the fragment view disappears. There is no LogCat error, this is some kind of confusing me. The menu is not working in one out of six fragments. Same problem happens in every fragment.

I want to create an OptionsMenu with the following code. The OptionsMenu is created in the activity, where the fragments get shown.

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.main_activity_actions, menu);

    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
    // then it has handled the app icon touch event

    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    //return super.onOptionsItemSelected(item);

    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_add:
            add();
            return true;
        case R.id.action_logoff:
            Intent i = new Intent(this, Login.class);
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);

    }


}

XML: main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_add"
        android:title="@string/action_add"
        android:showAsAction="ifRoom" />

    <item android:id="@+id/action_logoff"
        android:title="Ausloggen"
        android:showAsAction="ifRoom"/>

</menu>

I tried switching around the return statment to true and super.onOptionsItemSelected(item); but nothing changes. The App is developed for Android 2.3

役に立ちましたか?

解決

Maybe there are some problems with the hardwarebutton and Fragments in Android version < 3.0.
Here is a guide, how to implement a Actionbar with a softwarebased solution: http://hmkcode.com/add-actionbar-to-android-2-3-x/

他のヒント

Try to return super.onCreateOptionsMenu(menu) instead true

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top