WebDevDanno
WebDevDanno

Reputation: 1122

Menu options not working on Android 2.2 and 2.3.5

I'm having a problem with my android devices running 2.2 and 2.3.5. versions. I have been developing a simple app and have had no problems with any of my code so far. Since my last test the menu items have stopped loading up other Activities/running Intents on click and I really don't know why as I have reloaded my bin/gen folders, re-built the project, cleaned it but to no cigar! I've included the classes in my app and the android manifest for clarity.

Main Activity containing the Intents for app navigation

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

}

Another Activity which makes use of the original intent code

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 FILE AND ACTION_BAR XML FILE

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

Can anyone figure out the problem? The menu buttons are not loaded the intended intents. Cheers.

Upvotes: 1

Views: 1491

Answers (2)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

Your onOptionsItemSelected() should override

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

Upvotes: 1

Shadow
Shadow

Reputation: 6899

Use like this in menu->menu.xml

Instead of android:showAsAction="ifRoom|withText", use like this. android:showAsAction="always"

Hope it will work.

Upvotes: 0

Related Questions