简体   繁体   中英

Launch an activity from a menu button?

I've making an app and I want to launch a new activity from a menu, but ever time I click the menu button, the app crashes. I've tried many ways and all of the have failed.

public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplication())
        .inflate(R.menu.menu, menu);


return(super.onPrepareOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
   case R.id.Menu1:          
    Toast.makeText(this, "Coming soon", Toast.LENGTH_SHORT).show();
    break;
   case R.id.Menu2:
       Intent Intent = new Intent(this, About.class);
       startActivity(Intent);


 }
return(super.onOptionsItemSelected(item));
}
}

Android Manifest

  <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".AndroidRssReader" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".About" android:label="@string/app_label"></activity> 
Intent Intent = new Intent(this, About.class);
startActivity(Intent);

This needs to become

Intent intent = new Intent(this, About.class);
startActivity(intent);

It could be a few things, for example is the About class in your root package? (since you declare it as android:name=".About" )

But the best way to troubleshoot this is to have a look at the crash details in Logcat (both Eclipse and IntelliJ have a Logcat plugin). What does it say?

My implementation of onOptionsItemSelected() is slightly different as I am returning true when I have handled the selection myself, rather than always passing to the superclass.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.Menu1:
        Intent myIntent = new Intent(this,About.class);
        startActivity(myIntent);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

Check the documentation

We really need to see the logcat to be sure though

I just did the same thing, ie, launch an activity from a menu button; and it worked fine. As for the resolving of the variable with the same name as the class, i'm not so sure of. The declaration may work fine, but further references to that variable won't be accurate is my guess. Also, make sure all control paths in the onOptionsItemSelected function return a boolean value.

Maybe this fix the problem. I paste a example part below. Because, I think you forgot to made a new/second activity in your AndroidManifest.

  <application
    android:allowBackup="true"
    android:allowTaskReparenting="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    <activity
        android:name="YourPackageName.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    **<activity 
       android:name="YourPackageName.SecondActivity" />   

    <activity 
       android:name="YourPackageName.ThirdActivity" />**   

</application>

I hope this is gonna help you!

ADDED Post:

And this is my code, must be work! Put it in your main or something else, but not in the activity from your button.

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.MenuButton_About:
        Toast.makeText(this, "YourPopupText.", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent("Name of this activity".this, "Name of the menu button acivity".class));
        break;
    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}

}

You can also use: Toast.LENGTH_SHORT -> Toast.LENGTH_LONG

I hope this is gonna help you!

Check your About java activity file. I had a similar problem where the project would compile OK but the app would force a shutdown when the menu button was selected. When I rewrote the second activity everything was fine!

You're trying to pass a class into your startActivity method, not an Intent. You're declaring Intent Intent. You need to do Intent intent , or Intent myIntent , something like that. You can't have the same name for a variable as it's class, just as you can't do int int .

Android may be having difficulty starting your About class, eg an exception is being thrown in onCreate() . Looking at the stack trace in logcat may confirm this. Post the stack trace if its unclear to what is the root cause.

Just an observation but your switch statement should break and have a default case. it will help prevent errors in the future and possibly help with this issue. Other than that, post logs...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM