3

This is my code:

public class className extends Activity{
    private MenuActivity menuActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class
                .getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        menuActivity = new MenuActivity(this, context, R.menu.menu);
        );
    }
    @Override


    public boolean onCreateOptionsMenu(Menu menu) {
        menuActivity.onCreateOptionsMenu(menu);   // <--
        return super.onCreateOptionsMenu(menu);
    }
}

public class MenuActivity {
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = mRootActivity.getMenuInflater();
        inflater.inflate(typeMenu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
    }

    // ...
}

I receive a NullPointerException on row:

menuActivity.onCreateOptionsMenu(menu);

This exception is present only some moble like Galaxy note. Do you know why there is this exception?

Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
shuttle1978
  • 123
  • 2
  • 12

2 Answers2

1

// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);

MohsinSyd
  • 175
  • 2
  • 13
  • I've it "inflater.inflate(R.menu.menu, menu);" – shuttle1978 May 20 '13 at 13:20
  • on my mobile it works fine the problem is only some mobile, for example Galaxy note, it works fine also in emulator, I don't know why this initialization crash in some mobile and I don't know how to replicate this exception – shuttle1978 May 21 '13 at 05:53
0

Change code from

  public boolean onCreateOptionsMenu(Menu menu) {
    menuActivity.onCreateOptionsMenu(menu);   // <--
    return super.onCreateOptionsMenu(menu);
}

To

 public boolean onCreateOptionsMenu(Menu menu) {
   MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);  // <--
    return true;
}

This work on my program.

AmmY
  • 1,821
  • 1
  • 20
  • 31