2013-05-12 81 views
1

我遇到了運行2.2和2.3.5的Android設備的問題。版本。我一直在開發一個簡單的應用程序,迄今爲止我的任何代碼都沒有問題。自從我上次測試以來,菜單項已停止加載其他活動/點擊運行內容,我真的不知道爲什麼,因爲我重新加載了我的bin/gen文件夾,重新構建了項目,清理了它,但沒有雪茄!爲了清晰起見,我已將這些類包含在我的應用程序和Android清單中。菜單選項不能在Android 2.2和2.3.5上工作

包含用於應用程序導航

package rcahmw.prototype.crowdsnapcymru; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 

public class CrowdSnapCymru extends Activity { 

MenuItem item; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_crowd_snap_cymru); 
} 

/** 
* Empty constructor for CrowdSnapCymru 
*/ 
public CrowdSnapCymru() { 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.action_bar, menu); 
    return true; 
} 

public boolean onOptionsItemSelected(){ 
    switch(item.getItemId()) 
    { 
     case R.id.app_camera: 
      Intent firstIntent = new Intent(this, SnapCamera.class); 
      startActivity(firstIntent); 
      return true; 

     case R.id.app_dataentry: 
      Intent secondIntent = new Intent(this, DataEntry.class); 
      startActivity(secondIntent); 
      return true; 

     case R.id.app_upload: 
      Intent thirdIntent = new Intent(this, UploadRecord.class); 
      startActivity(thirdIntent); 
      return true; 

     case R.id.app_home: 
      Intent fourthIntent = new Intent(this, ImageSelector.class); 
      startActivity(fourthIntent); 
      return true; 

     case R.id.app_about: 
      Intent fifthIntent = new Intent(this, CrowdSnapCymru.class); 
      startActivity(fifthIntent); 
      return true; 

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

}

另一活動,其利用原意代碼

package rcahmw.prototype.crowdsnapcymru; 

import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 

public class UploadRecord extends CrowdSnapCymru { 

MenuItem item; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_upload_record); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.action_bar, menu); 
    return true; 
} 

public void loadMenuResponses() { 
    onOptionsItemSelected(item); 
} 

}

的意圖主要活動

Android清單XML文件,並ACTION_BAR XML文件

<?xml version="1.0" encoding="utf-8"?> 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<uses-permission android:name="android.hardware.camera" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/MyTheme" > 
    <activity 
     android:name="rcahmw.prototype.crowdsnapcymru.CrowdSnapCymru" 
     android:uiOptions="splitActionBarWhenNarrow" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="rcahmw.prototype.crowdsnapcymru.SnapCamera" 
     android:uiOptions="splitActionBarWhenNarrow" > 
    </activity> 
    <activity 
     android:name="rcahmw.prototype.crowdsnapcymru.DataEntry" 
     android:uiOptions="splitActionBarWhenNarrow" > 
    </activity> 
    <activity 
     android:name="rcahmw.prototype.crowdsnapcymru.UploadRecord" 
     android:label="@string/title_activity_upload_record" 
     android:uiOptions="splitActionBarWhenNarrow" > 
    </activity> 
    <activity 
     android:name="rcahmw.prototype.crowdsnapcymru.Login" 
     android:label="@string/title_activity_login" 
     android:windowSoftInputMode="adjustResize|stateVisible">   
    </activity> 
    <activity 
     android:name="rcahmw.prototype.crowdsnapcymru.ImageSelector" 
     android:label="@string/title_activity_image_selector" 
     android:uiOptions="splitActionBarWhenNarrow" > 
    </activity> 
</application> 

<?xml version="1.0" encoding="utf-8"?> 

<item android:id="@+id/app_camera" 
     android:title="@string/Camera" 
     android:icon="@drawable/ic_camera" 
     android:showAsAction="ifRoom|withText" /> 

<item android:id="@+id/app_dataentry" 
     android:title="@string/Edit" 
     android:icon="@drawable/ic_edit" 
     android:showAsAction="ifRoom|withText"/> 

<item android:id="@+id/app_upload" 
     android:title="@string/Upload" 
     android:icon="@drawable/ic_upload" 
     android:showAsAction="ifRoom|withText" /> 

<item android:id="@+id/app_about" 
    android:title="@string/About" 
    android:icon="@drawable/ic_about" 
    android:showAsAction="ifRoom|withText" /> 

任何人都可以找出問題嗎?菜單按鈕沒有加載預期的意圖。乾杯。

回答

1

onOptionsItemSelected()應該重寫

@Override 
public boolean onOptionsItemSelected(Menu item){ 
switch(item.getItemId()) 
{ 
    case R.id.app_camera: 
     Intent firstIntent = new Intent(this, SnapCamera.class); 
     startActivity(firstIntent); 
     return true; 

    case R.id.app_dataentry: 
     Intent secondIntent = new Intent(this, DataEntry.class); 
     startActivity(secondIntent); 
     return true; 

    case R.id.app_upload: 
     Intent thirdIntent = new Intent(this, UploadRecord.class); 
     startActivity(thirdIntent); 
     return true; 

    case R.id.app_home: 
     Intent fourthIntent = new Intent(this, ImageSelector.class); 
     startActivity(fourthIntent); 
     return true; 

    case R.id.app_about: 
     Intent fifthIntent = new Intent(this, CrowdSnapCymru.class); 
     startActivity(fifthIntent); 
     return true; 

    default: 
     return super.onOptionsItemSelected(item); 
} 
} 
+0

釘它。隊友的歡呼聲 :) – WebDevDanno 2013-05-12 01:36:09

0

使用像這樣的菜單 - > menu.xml文件

相反的android:showAsAction = 「ifRoom | withText」,這樣使用。 android:showAsAction =「always」

希望它能工作。

相關問題