gpt4 book ai didi

java - 菜单选项不适用于 Android 2.2 和 2.3.5

转载 作者:行者123 更新时间:2023-11-30 03:37:04 25 4
gpt4 key购买 nike

我的 Android 设备运行 2.2 和 2.3.5 时遇到问题。版本。我一直在开发一个简单的应用程序,到目前为止我的任何代码都没有问题。自从我上次测试以来,菜单项已停止加载其他 Activity/单击时运行 Intents,我真的不知道为什么,因为我重新加载了我的 bin/gen 文件夹,重新构建了项目,清理了它,但没有雪茄!为了清楚起见,我在我的应用程序和 android list 中包含了这些类。

包含应用导航 Intent 的主 Activity

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

}

另一个使用原始 Intent 代码的 Activity

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 list 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" />

谁能解决这个问题?菜单按钮未按预期加载。干杯。

最佳答案

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

关于java - 菜单选项不适用于 Android 2.2 和 2.3.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16503235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com