我的Android设备运行2.2和2.3.5时出现问题。版本。我一直在开发一个简单的应用程序,到目前为止我的任何代码都没有问题。自从我上次测试后,菜单项已经停止加载其他活动/运行Intent点击,我真的不知道为什么我重新加载我的bin / gen文件夹,重新构建项目,清理它但没有雪茄!为了清楚起见,我已经在我的应用程序和Android清单中包含了这些类。
包含应用导航意图的主要活动
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 MANIFEST 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" />
有人能解决问题吗?菜单按钮未加载预期的意图。欢呼声。
答案 0 :(得分: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);
}
}
答案 1 :(得分:0)
在menu-&gt; menu.xml中使用如下所示
而不是android:showAsAction =“ifRoom | withText”,请使用这样的方法。机器人:showAsAction = “总是”
希望它能奏效。