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)
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
My android application crashes in emulator. The application did not open in emulator. I am working with java. It was working perfectly before i added the bottom navigation into my application. Please sort out my issue as i am new in this field. Here is the code of my main activity below.
package com.example.bottomnavigation;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import com.example.bottomnavigation.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
ImageView picture, video, message, mail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getSupportActionBar().hide();
binding.bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.first:
FragmentTransaction firsttrans = getSupportFragmentManager().beginTransaction();
firsttrans.replace(R.id.frame, new HomeFragment());
firsttrans.commit();
break;
case R.id.search:
FragmentTransaction searchtrans = getSupportFragmentManager().beginTransaction();
searchtrans.replace(R.id.frame, new SearchFragment());
searchtrans.commit();
break;
case R.id.exit:
new AlertDialog.Builder(MainActivity.this)
.setTitle("Exit")
.setIcon(R.drawable.exit)
.setMessage("Are you sure you want to exit")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finishAffinity();
}
}).show();
}
return true;
}
});
picture = findViewById(R.id.arham);
picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Pictures.class);
startActivity(intent);
}
});
}
}
Here is the logcat
2021-04-05 12:35:38.662 19375-19375/? E/ottomnavigatio: Unknown bits set in runtime_flags: 0x8000
2021-04-05 12:35:40.544 19375-19375/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bottomnavigation, PID: 19375
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bottomnavigation/com.example.bottomnavigation.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference
at com.example.bottomnavigation.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
The exception is being thrown because you are calling hide() on a null reference to an ActionBar. Assuming that this is the relevant code, it is occurring on this line:
getSupportActionBar().hide();
If you read the javadocs (here and here) for getSupportActionBar() you will see that it returns null if the activity doesn't have an action bar. That must be happening here.
(So why is your app trying to hide the activity bar when it doesn't have one??)
It would appear that the solution is to delete the offending line. Or if there are contexts in which your app might have an action bar, change it to:
ActionBar bar = getSupportActionBar();
if (bar != null) {
bar.hide();
}
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.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I start android developing recently. I may have some lack of knowledge in this scenario.But I searched a lot and can't make a solution for this project.
Facing problem with this.I failed to figure it out. My SharedPreferences Class is.
public class PermanentScoreHolder {
public static boolean storeScore(String prefName,float score)
{
float temp = VarHolder.SHARED_PREFERENCES.getFloat(VarHolder.SUFFIX_PREFERENCES+prefName, 0f);
if(temp<score)
{
VarHolder.EDITOR.putFloat(VarHolder.SUFFIX_PREFERENCES+prefName, score);
VarHolder.EDITOR.commit();
return true;
}
return false;
}
public static float getScore(String prefName)
{
return VarHolder.SHARED_PREFERENCES.getFloat(VarHolder.SUFFIX_PREFERENCES+prefName, 0f);
}
}
My activity class is like this:
public class ScoreActivity extends Activity {
TextView normal,danger,thunder,zen,rush;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_score);
normal = (TextView) findViewById(R.id.normalScore);
normal.setText(PermanentScoreHolder.getScore("1")+"");//Logcat says problem is here.
}
public void onBackPressed() {
Intent i = new Intent(getBaseContext(),OptionMenuActivity.class);
startActivity(i);
finish();
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.score, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPause() {
super.onResume();
overridePendingTransition(0, 0);
}
#Override
protected void onResume() {
super.onResume();
overridePendingTransition(0, 0);
}}
My Logcat is here:
FATAL EXCEPTION: main
Process: cafesoft.td.tappingtile, PID: 8740
java.lang.RuntimeException: Unable to start activity ComponentInfo{cafesoft.td.tappingtile/cafesoft.td.tappingtile.ScoreActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'float android.content.SharedPreferences.getFloat(java.lang.String, float)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'float android.content.SharedPreferences.getFloat(java.lang.String, float)' on a null object reference
at cafesoft.td.tappingtile.PermanentScoreHolder.getScore(PermanentScoreHolder.java:18)
at cafesoft.td.tappingtile.ScoreActivity.onCreate(ScoreActivity.java:98)
at android.app.Activity.performCreate(Activity.java:6980)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
I don't understand whats wrong with this code.I need a solution and why this occurs.
Your VarHolder.SHARED_PREFERENCES in the class PermanentScoreHolder has a null reference.
This is how we get SharedPreferences instance:
SharedPreferences pref = context.getSharedPreferences("unique_key", Context.MODE_PRIVATE);
Then get the float from SharedPreferences:
pref.getFloat("key", defaultValue);
Below is my MainActivity.java (I have removed some of unnecessary code. My main activity was a Navigation Viewer activity)
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<MainStoryTile> list = new ArrayList<MainStoryTile>();
int[] image_id = { R.drawable.sample, R.drawable.sample, R.drawable.sample};
String[] name, email, mobile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = getResources().getStringArray(R.array.person_name);
email = getResources().getStringArray(R.array.person_email);
mobile = getResources().getStringArray(R.array.person_mobile);
int count = 0;
for(String NAME: name){
MainStoryTile contact = new MainStoryTile(image_id[count], NAME, email[count], mobile[count]);
count++;
list.add(contact);
}
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new MainStoryAdapter(list);
recyclerView.setAdapter(adapter);
}
#Override
public void onBackPressed() {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
return true;
}
}
Below is my Adapter class. (Note : there is an inner class)
public class MainStoryAdapter extends RecyclerView.Adapter<MainStoryAdapter.StoryViewHolder>{
ArrayList<MainStoryTile> contacts = new ArrayList<MainStoryTile>();
public MainStoryAdapter(ArrayList<MainStoryTile> contacts){
this.contacts = contacts;
}
#Override
public StoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_main,parent,false );
StoryViewHolder storyViewHolder = new StoryViewHolder(view);
return storyViewHolder;
}
#Override
public void onBindViewHolder(StoryViewHolder holder, int position) {
MainStoryTile CON = contacts.get(position);
holder.person_img.setImageResource(CON.getImage_id());
holder.person_name.setText(CON.getName());
holder.person_email.setText(CON.getEmail());
holder.person_mobile.setText(CON.getMobile());
}
#Override
public int getItemCount() {
return contacts.size();
}
public static class StoryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView person_img;
TextView person_name, person_email, person_mobile;
public StoryViewHolder(View view){
super(view);
person_img = (ImageView) view.findViewById(R.id.person_image);
person_name = (TextView) view.findViewById(R.id.person_name);
person_email = (TextView) view.findViewById(R.id.person_email);
person_mobile = (TextView) view.findViewById(R.id.person_mobile);
view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Log.d("MY LOG", "sdurfhiusrhdidysdysdysdysdys r");
}
}
}
When I click on a item, my app crashes. below is error log :
03-17 15:20:38.638 28744-28744/com.storyteller.pro.storyteller E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.storyteller.pro.storyteller, PID: 28744
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4286)
at android.view.View.performClick(View.java:5242)
at android.view.View$PerformClick.run(View.java:21196)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4281)
at android.view.View.performClick(View.java:5242)
at android.view.View$PerformClick.run(View.java:21196)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4277)
at android.view.ViewGroup.addView(ViewGroup.java:4127)
at android.view.ViewGroup.addView(ViewGroup.java:4068)
at android.view.ViewGroup.addView(ViewGroup.java:4041)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:247)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:114)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4281)
at android.view.View.performClick(View.java:5242)
at android.view.View$PerformClick.run(View.java:21196)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
03-17 15:20:43.048 28744-28744/com.storyteller.pro.storyteller I/Process: Sending signal. PID: 28744 SIG: 9
Update: Even after I commented the following line in the view.setOnClickListener(this); in the MainStoryAdapter, my app still crashing when I click on an item.
Below is the project for those who like to see more > project zip version
I checked your project and you, why ever, define android:onClick="setContentView" in your content_main.xml layout for the CardView.
Remove that attribute and it works.
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;
}
}