1

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

包含应用导航意图的主 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);
        }
    }

}

另一个使用原始意图代码的活动

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

谁能找出问题所在?菜单按钮未加载预期的意图。干杯。

4

2 回答 2

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);
}
}
于 2013-05-12T01:31:09.027 回答
0

在 menu->menu.xml 中这样使用

而不是 android:showAsAction="ifRoom|withText",像这样使用。机器人:showAsAction="总是"

希望它会奏效。

于 2013-05-12T01:25:25.630 回答