can't get menuitem on android 4.1, it crashes - java

I have code, that gets Menu Item and changes icon color. on Android 8.1 it's working fine, but when I tested it on android 4.1.1, app crashes.
Drawable drawable = menuos.getItem(1).getIcon();
if(drawable != null) {
drawable.mutate();
drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
}
Menuos:
Menu menuos = menu
It is located in onCreateOptionsMenu
Logcat:
07-28 11:20:15.794 3443-3443/com.developerfromjokela.edison E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.developerfromjokela.edison.MainActivity$5.onPageStarted(MainActivity.java:223)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:318)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
menuos = menu;
return super.onCreateOptionsMenu(menu);
}

If this is the line that throws NPE:
Drawable drawable = menuos.getItem(1).getIcon();
then there are 2 options:
menuos is null or menuos.getItem(1) is null
so do this before the above line:
Log.i("menuos_isnull", menuos == null);
if (menuos != null) {
Log.i("Item1_isnull", menuos.getItem(1) == null);
}
and see what'rs printed.
This is debugging and you must use it more often.

Related

Problem with actionView() - java.lang.NullPointerException:

So this is a part of code found on my MainActivity.java. What it basically does is allowing me to go to the Cart Fragment when I click on the icon (cart_badge_text_view) at the top of my application.
When I try to open the application,the view loads up but then it immediately crashes.
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
final MenuItem menuItem = menu.findItem(R.id.cartFragment);
View actionView = menuItem.getActionView(); --> PROBLEM
TextView cartBadgeTextView = actionView.findViewById(R.id.cart_badge_text_view);
cartBadgeTextView.setText(String.valueOf(cartQuantity));
cartBadgeTextView.setVisibility(cartQuantity == 0 ? View.GONE : View.VISIBLE);
actionView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(menuItem);
}
});
This is the logcat after building the app:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.example, PID: 8621
**java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' on a null object reference --> MAIN PROBLEM**
at com.example.example.views.MainActivity.onCreateOptionsMenu(MainActivity.java:69)
at android.app.Activity.onCreatePanelMenu(Activity.java:4206)
at androidx.fragment.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:324)
at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:3070)
at androidx.appcompat.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1895)
at androidx.appcompat.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:2176)
at androidx.appcompat.app.AppCompatDelegateImpl$2.run(AppCompatDelegateImpl.java:268)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Android DexClassLoader error, 'optimized data directory .. not owned by current user'

I am trying to produce a simple android application that can load a DEX file from the SD card at run-time.
The application has two activites. The first activity is a simple screen that has a button. When the button is pressed, the second activity is launched which causes the loadDex() method to be invoked. The loadDex() method attempts to locate a jar file on the SD card and load it into the current application.
Here is my code for the first activity:
package poc.example.del.customclass;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
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 launchLoadClass(View view) {
Intent intent = new Intent(MainActivity.this, LoadClass.class);
startActivity(intent);
}
}
Here is the code for my second activity (the one that loads the DEX file):
package poc.example.del.customclass;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import dalvik.system.DexClassLoader;
public class LoadClass extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_class);
loadDex();
}
#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_load_class, 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 loadDex() {
String dexFile = "/sdcard/output.jar";
File jarFile = new File(dexFile);
if (jarFile.exists()) {
// Toast.makeText(getApplicationContext(), "It Worked!", Toast.LENGTH_LONG).show();
DexClassLoader cl = new DexClassLoader (jarFile.toString (), "/data/test", null, ClassLoader.getSystemClassLoader());
}
}
}
The issue arise when the DexClassLoader constructor is called. The following error can be found in the log:
03-25 10:15:48.441 1934-1934/poc.example.del.customclass E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{poc.example.del.customclass/poc.example.del.customclass.LoadClass}: java.lang.IllegalArgumentException: Optimized data directory /data/test is not owned by the current user. Shared storage cannot protect your application from code injection attacks.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Optimized data directory /data/test is not owned by the current user. Shared storage cannot protect your application from code injection attacks.
at dalvik.system.DexFile.<init>(DexFile.java:100)
at dalvik.system.DexFile.loadDex(DexFile.java:149)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:261)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:229)
at dalvik.system.DexPathList.<init>(DexPathList.java:96)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:56)
at dalvik.system.DexClassLoader.<init>(DexClassLoader.java:57)
at poc.example.del.customclass.LoadClass.loadDex(LoadClass.java:54)
at poc.example.del.customclass.LoadClass.onCreate(LoadClass.java:23)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5039)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
Here is the line in the log that I believe represents the issue:
java.lang.IllegalArgumentException: Optimized data directory /data/test is not owned by the current user. Shared storage cannot protect your application from code injection attacks.
Any help would be appreciate as I have found very little information on the web regarding the issue. I am developing the application for Android 4.2, Api 17.
Thanks in advance.
I found an answer after a few days of following various tutorials. I thought id post the solution here in case anyone else has a similar problem.
For security reasons, Android does not allow the application to load files to any random folder. Instead it should be loaded to the applications environment. Here is the modified code that has allowed me to continue with the project. The code shown is for the 'loadDex()' method:
public void loadDex() {
// Toast the show the method has been invoked correctly
// Toast.makeText(getApplicationContext(), "loadDex() Method invoked", Toast.LENGTH_LONG).show();
// name of the DEX file
String dexFile = "/output.jar";
// Get the path to the SD card
File f = new File(Environment.getExternalStorageDirectory().toString() + dexFile);
// optimized directory, the applciation and package directory
final File optimizedDexOutputPath = getDir("outdex", 0);
// DexClassLoader to get the file and write it to the optimised directory
DexClassLoader classLoader = new DexClassLoader(f.getAbsolutePath(),
optimizedDexOutputPath.getAbsolutePath(),null, getClassLoader());
// The classpath is created for the new class
String completeClassName = "poc.example.del.mylibrary.name";
String methodToInvoke = "display";
try {
Class<?> myClass = classLoader.loadClass(completeClassName);
Object obj = (Object)myClass.newInstance();
Method m = myClass.getMethod(methodToInvoke);
String s = ""+m.invoke(obj);
makeToast(s);
}
catch (Exception e) {
e.printStackTrace();
makeToast("Something went wrong!");
}
}
The specific line(s) of code that resolved the problem is:
// DexClassLoader to get the file and write it to the optimized directory
DexClassLoader classLoader = new DexClassLoader(f.getAbsolutePath(),
optimizedDexOutputPath.getAbsolutePath(),null, getClassLoader());
As you can see, the optimizedDexOutputPath,getAbsolutePath() method returns the directory that can be used to write files to by the application.
Hope this helps anyone else with a similar issue!

How to resolve this java.lang.UnsupportedOperationException: This is not supported in Android L?

i am try on multiple item click to share item using MultiChoiceModeListener i got FATAL EXCEPTION in onCreateActionMode method
i have already using AppCompat v21.0.2, before L update this code work perfectly in AppCompat v19
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
mActionMode = mode;
nr = 0;
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.contextual_menu, menu);
MenuItem menuItem = menu.findItem(R.id.item_menu_share);
mShareActionProvider = (ShareActionProvider) menuItem
.getActionProvider();
mShareActionProvider
.setOnShareTargetSelectedListener(PlanetFragment.this);
return true;
}
Exception:
FATAL EXCEPTION: main
java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setActionProvider()
at android.support.v7.internal.view.menu.MenuItemImpl.setActionProvider(MenuItemImpl.java:639)
at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:445)
at android.view.MenuInflater$MenuState.addSubMenuItem(MenuInflater.java:457)
at android.view.MenuInflater.parseMenu(MenuInflater.java:186)
at android.view.MenuInflater.inflate(MenuInflater.java:110)
at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:109)
at com.mydata.PlanetFragment$GetDataAsyncTask$1.onCreateActionMode(PlanetFragment.java:252)
at android.widget.AbsListView$MultiChoiceModeWrapper.onCreateActionMode(AbsListView.java:6203)
at android.support.v7.internal.view.SupportActionModeWrapper$CallbackWrapper.onCreateActionMode(SupportActionModeWrapper.java:154)
at android.support.v7.app.ActionBarActivityDelegateBase$ActionModeCallbackWrapper.onCreateActionMode(ActionBarActivityDelegateBase.java:1297)
at android.support.v7.app.ActionBarActivityDelegateBase.startSupportActionModeFromWindow(ActionBarActivityDelegateBase.java:648)
at android.support.v7.app.ActionBarActivityDelegate$1.startActionMode(ActionBarActivityDelegate.java:99)
at android.support.v7.widget.WindowCallbackWrapper.startActionMode(WindowCallbackWrapper.java:69)
at android.support.v7.internal.app.ToolbarActionBar.startActionMode(ToolbarActionBar.java:206)
at android.support.v7.app.ActionBarActivityDelegateBase.startSupportActionMode(ActionBarActivityDelegateBase.java:581)
at android.support.v7.app.ActionBarActivityDelegateHC.startActionModeForChild(ActionBarActivityDelegateHC.java:62)
at android.support.v7.internal.widget.NativeActionModeAwareLayout.startActionModeForChild(NativeActionModeAwareLayout.java:44)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:677)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:677)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:677)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:677)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:677)
at android.view.View.startActionMode(View.java:4347)
at android.widget.AbsListView.performLongPress(AbsListView.java:2874)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2834)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5162)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
at dalvik.system.NativeStart.main(Native Method)
It is old question, but I will post my solution, maybe someone will find it usable.
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Log.d(TAG, "onCreateActionMode");
mode.getMenuInflater().inflate(R.menu.list_select_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
MenuItemCompat.setActionProvider(item, mShareActionProvider);
return true;
}
change:
mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
to
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

Action bar home button crashes while back button works

I'm completely stumped on this one. I have an activity C, when I try and press the back button, it works. But when I use the home/up button in the action bar it just crashes with (see the following error). Here are the parts of my code which deal with the back/up buttons.
Activity C:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offline_viewer);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(!isOnline) {
menu.removeItem(R.id.saveRoute);
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.offline_viewer, menu);
return true;
}
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
//Get names for saving
String[] startParts = onlineFrom.split(",");
String startName = startParts[0] + "," + startParts[1];
String[] endParts = onlineTo.split(",");
String endName = endParts[0] + "," + endParts[1];
System.out.println(item.getItemId());
switch(item.getItemId()){
//Save xml file or route once pressed
case R.id.saveRoute:
//TODO:Uncomment once server is ready
new DownloadFileFromURL(this, startName, endName).execute(urlForDownload);
return true;
case android.R.id.home:
System.out.println(item.getItemId());
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Error:
08-22 16:15:43.629: W/dalvikvm(4908): threadid=1: thread exiting with uncaught exception (group=0x41c7e888)
08-22 16:15:43.634: E/AndroidRuntime(4908): FATAL EXCEPTION: main
08-22 16:15:43.634: E/AndroidRuntime(4908): java.lang.NullPointerException
08-22 16:15:43.634: E/AndroidRuntime(4908): at com.example.otpxmlgetter.OfflineViewer.onOptionsItemSelected(OfflineViewer.java:185)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.app.Activity.onMenuItemSelected(Activity.java:2590)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
08-22 16:15:43.634: E/AndroidRuntime(4908): at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:167)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.view.View.performClick(View.java:4204)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.view.View$PerformClick.run(View.java:17354)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.os.Handler.handleCallback(Handler.java:725)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.os.Looper.loop(Looper.java:137)
08-22 16:15:43.634: E/AndroidRuntime(4908): at android.app.ActivityThread.main(ActivityThread.java:5232)
08-22 16:15:43.634: E/AndroidRuntime(4908): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 16:15:43.634: E/AndroidRuntime(4908): at java.lang.reflect.Method.invoke(Method.java:511)
08-22 16:15:43.634: E/AndroidRuntime(4908): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
08-22 16:15:43.634: E/AndroidRuntime(4908): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
08-22 16:15:43.634: E/AndroidRuntime(4908): at dalvik.system.NativeStart.main(Native Method)
Does this have something to do with the fact that there are two possible ways to get to Activity C? Either A->B->C or A->D->C?
The fact that the back button still works completely confounds me.
It is commonly not recommended to call Activity life cyle methods. The onWhatEver methods are used by the framework. If you want to finish the activity rather use ...
this.finish()
or ...
getActivity.finish()
if you are in a fragment context.
p.s.s: onBackPressed does actually work (against initial asumption of this answer). See comments below for the root cause of this issue.
p.s.: this post proposes this.dispatchKeyEvent(new Keyevent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); as an alternative

Show menu in android on startup

Is there any way to automatically show the menu when an activity starts as it's a list activity which will be blank when it starts for the first time.
Check Following link, It explain how to open and close option menu progamatically
http://kahdev.wordpress.com/2010/03/15/progamatically-open-and-close-an-activitys-option-menu/
I played around with this one and it didn't matter if I put it in onCreate, onStart, onResume, onPostResume it always threw (physical Galaxy S4 # 4.4.2 and Genymotion Galaxy S2 # 2.3.7):
10-23 12:50:22.389 27702-27702/net.twisterrob.debug D/AndroidRuntime﹕ Shutting down VM
10-23 12:50:22.389 27702-27702/net.twisterrob.debug W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418e3da0)
10-23 12:50:22.389 27702-27702/net.twisterrob.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.twisterrob.debug, PID: 27702
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.twisterrob.debug/net.twisterrob.android.MyActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
...
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:751)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:746)
at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:621)
at android.app.Activity.openOptionsMenu(Activity.java:2960)
at net.twisterrob.android.MyActivity.onCreate(MyActivity.java:35)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
...
The final solution that worked is:
#Override public void onAttachedToWindow() {
super.onAttachedToWindow();
openOptionsMenu();
}
After the activity has resumed and !isFinishing() it is safe to use openOptionsMenu in any event handler or AsyncTask.onPostExecute.
All you have to do is call either on of these in a listener and you can open or close the menu no problem.
openOptionsMenu();
closeOptionsMenu();
So call it in onCreate.
public class List extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
openOptionsMenu();
}
// Menu Button Stuff
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater Menu = getMenuInflater();
Menu.inflate(R.menu.menu_layout, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menuBack:
finish();
return true;
}
return false;
}
}

Categories