繁体   English   中英

通过菜单按钮启动活动?

[英]Launch an activity from a menu button?

我正在制作一个应用程序,但我想从菜单启动一个新活动,但是每当我单击菜单按钮时,该应用程序就会崩溃。 我尝试了很多方法,但所有方法都失败了。

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清单

  <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);

这需要成为

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

可能有几件事,例如根包中的About类? (因为您将其声明为android:name=".About"

但是解决此问题的最佳方法是查看Logcat中的崩溃详细信息(Eclipse和IntelliJ都有Logcat插件)。 它说什么?

我对onOptionsItemSelected()实现略有不同,因为我自己处理选择后返回true ,而不是总是传递给超类。

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);
    }
}

检查文件

我们确实需要查看logcat以确保

我只是做同样的事情,即从菜单按钮启动一个活动。 而且效果很好。 至于与类同名的变量的解析,我不太确定。 声明可能工作正常,但我猜想对该变量的进一步引用将不准确。 另外,请确保onOptionsItemSelected函数中的所有控件路径都返回布尔值。

也许这可以解决问题。 我在下面粘贴一个示例部分。 因为,我认为您忘记了在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>

希望对您有所帮助!

添加的帖子:

这是我的代码,必须有效! 将其放在您的主要内容或其他内容中,而不是放在按钮中的活动中。

    @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);
}

}

您还可以使用:Toast.LENGTH_SHORT-> Toast.LENGTH_LONG

希望对您有所帮助!

检查关于Java活动文件。 我有一个类似的问题,项目将编译为OK,但是当选择菜单按钮时,应用程序将强制关闭。 当我重写第二项活动时,一切都很好!

您正在尝试将类(而不是Intent)传递给startActivity方法。 您是在声明意图。 您需要执行Intent myIntent Intent intentIntent myIntent 变量的名称不能与类的名称相同,就像int int

Android可能无法启动About类,例如,在onCreate()引发了异常。 查看logcat中的堆栈跟踪可能会确认这一点。 如果不清楚是什么原因,请发布堆栈跟踪。

只是观察,但是您的switch语句应该中断并具有默认情况。 这将有助于防止将来出现错误,并可能会解决此问题。 除此之外,发布日志...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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