2011-02-02 73 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活動文件。我有一個類似的問題,那就是項目編譯成功,但是當菜單按鈕被選中時,應用程序會強制關閉。當我重寫第二個活動時,一切都很好!