2011-02-02 71 views
1

我做了一个应用程序,我想从菜单中启动一个新的活动,但有时候我点击菜单按钮,应用程序崩溃。我尝试了很多方法,都失败了。从菜单按钮启动一个活动?

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> 
+1

logcat中的任何数据? – 2011-02-02 23:21:11

+0

是关于声明在您的清单文件中的活动? – 2011-02-02 23:23:49

+0

没有日志/堆栈跟踪,我们不能帮助很多...... – Hameno 2011-02-02 23:25:06

回答

1

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

检查documentation

我们真的需要看到的logcat,以确保虽然

1

这可能是一个几件事情,例如在你的根包的关于类? (因为你把它声明为android:name=".About"

但要解决它的最好方法就是看看在坠机细节logcat的(Eclipse和的IntelliJ有logcat的插件)。它说什么?

0

你试图将一个类传递给你的startActivity方法,而不是一个Intent。你正在申明Intent Intent。你需要做Intent intentIntent myIntent,就像那样。你不能为变量指定相同的名称,就像你不能做int int一样。

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

这需要成为

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

,Android可以为您启动类About困难,例如onCreate()正在抛出异常。查看logcat中的堆栈跟踪可能会证实这一点。如果不清楚根源是什么,请发布堆栈跟踪。

0

只是一个观察,但你的switch语句应该打破,并有一个默认情况下。它将有助于防止将来发生错误,并可能有助于解决此问题。除此之外,发布日志...

1

我只是做了同样的事情,即从菜单按钮启动一个活动;它运行良好。至于解决与班级同名的变量,我不太确定。该声明可能正常工作,但对该变量的进一步引用将不准确是我的猜测。另外,请确保onOptionsItemSelected函数中的所有控制路径都返回一个布尔值。

0

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

我希望这是要去帮助你!

ADDED POST:

这是我的代码,一定是工作!把它放在你的主或其他东西中,但不要放在你按钮的活动中。

@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

我希望这是要去帮助你!

0

检查您的关于java活动文件。我有一个类似的问题,那就是项目编译成功,但是当菜单按钮被选中时,应用程序会强制关闭。当我重写第二个活动时,一切都很好!