Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to load an activity, when an option is selected from the menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_flash:
Intent intent = new Intent(this, FlashActivity.class);
this.startActivity(intent);
break;
case R.id.menu_color:
Intent intent = new Intent(this, FlashActivity.class);
this.startActivity(intent);
break; break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
but it keep to give me intent error
Change with this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Intent intent = new Intent(this, FlashActivity.class);
this.startActivity(intent);;
return true;
case R.id.item2:
Intent intent = new Intent(this, FlashActivity.class);
this.startActivity(intent);
return true;
default:
return super.onContextItemSelected(item);
}
}
Intent intent = new Intent(youractivity.this, FlashActivity.class);
startActivity(intent);
Change to above code...
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menu_flash:
Intent intent = new Intent(this, FlashActivity.class);
startActivity(intent);
return true;
}
return false;
Have you declared FlashActivity in your manifest file??
if not,add this to your manifest:
<activity
android:name=".FlashActivity" >
</activity>
Make sure that the FlashActivity declare in manifest file.Then try this..
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_flash:
openFlashmenu();
return true;
case R.id.menu_color:
openFlashmenu();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void openFlashmenu(){
Intent intent = new Intent(this, FlashActivity.class);
this.startActivity(intent);
}
Related
I'm converting some activities to several fragments , and now when I press back button it doesn't work.
what changes should I do in this fragment when its returning back to Previous activity and when returning to Previous fragment?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.returnHome:
Intent i= new Intent(getActivity().getApplicationContext(), WoundNavigation.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
return true;
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try like this
write this in onCreateView() method
setHasOptionsMenu(true)
and make these changes
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.returnHome:
Intent i= new Intent(getActivity(), WoundNavigation.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
return true;
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It may help. If this also doesn't works then you have to manage fragment backstack in activity from where you are calling your fragment
I am getting and displaying images after doing Parse Queries and caching images. My problem is that when i click on the clear cache it doesn't delete the images and my pageviewer with i use to display them. It displays duplicates.I'm using Image Fetcher https://developer.android.com/samples/DisplayingBitmaps/src/com.example.android.displayingbitmaps/util/ImageFetcher.html from Caching Bitmaps from http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html saving files to the phone memory
private ImageFetcher mImageFetcher;
...
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.logout:
ParseUser.logOut();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish(); // logout code
return true;
case R.id.prefs:
Intent i = new Intent(this, SharedPrefsActivity.class);
startActivityForResult(i, RESULT_SETTINGS);
// intent = new Intent(this, SharedPrefsActivity.class);
// startActivity(intent);
// finish(); //logout code
return true;
case R.id.clear_cache:
mImageFetcher.clearCache();
Toast.makeText(this, R.string.clear_cache_complete_toast,
Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
This question already has answers here:
switch case statement error: case expressions must be constant expression
(9 answers)
Closed 8 years ago.
I have problems with this type of code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_sign_out_all:
doHardShutdown();
return true;
case R.id.menu_add_account:
// showExistingAccountListDialog();
return true;
case R.id.menu_settings:
Intent sintent = new Intent(this, SettingActivity.class);
startActivityForResult(sintent,0);
return true;
case R.id.menu_import_keys:
importKeyStore();
return true;
// case R.id.menu_exit:
// signOutAndKillProcess();
// return true;
}
return super.onOptionsItemSelected(item);
}
It happens in all code where is some switch/case.
On every:
case R.id.XXX
eclipse returns me an error:"case expressions must be constants expressions"
Then I tried to delete R.java, clean, and regenerate it, but didn't work.
How can I fix it?
Thats part of the xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_add_account"
android:title="#string/menu_add_account"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="always|withText"
>
</item>
This problem I had happens to this reason:
In a regular Android project, constants in the resource R class are declared like this:
public static final int main=0x7f030004;
However, as of ADT 14, in a library project, they will be declared like this:
public static int main=0x7f030004;
The solution to this type of problems is convert "switch-case" to "if-else"
(see more at: http://tools.android.com/tips/non-constant-fields)
Original:
switch (item.getItemId()) {
case R.id.menu_sign_out_all:
doHardShutdown();
return true;
case R.id.menu_add_account:
// showExistingAccountListDialog();
return true;
case R.id.menu_settings:
Intent sintent = new Intent(this, SettingActivity.class);
startActivityForResult(sintent,0);
return true;
case R.id.menu_import_keys:
importKeyStore();
return true;
}
Solution
int itemId = item.getItemId();
if (itemId == R.id.menu_invite_user) {
Intent i = new Intent(ContactListActivity.this, AddContactActivity.class);
i.putExtra(ImServiceConstants.EXTRA_INTENT_PROVIDER_ID, mProviderId);
i.putExtra(ImServiceConstants.EXTRA_INTENT_ACCOUNT_ID, mAccountId);
i.putExtra(ImServiceConstants.EXTRA_INTENT_LIST_NAME,
mContactListView.getSelectedContactList());
startActivity(i);
return true;
} else if (itemId == android.R.id.home || itemId == R.id.menu_view_accounts) {
startActivity(new Intent(getBaseContext(), ChooseAccountActivity.class));
// finish();
return true;
} else if (itemId == R.id.menu_settings) {
Intent sintent = new Intent(this, SettingActivity.class);
startActivity(sintent);
return true;
}
code like below to solve
assign menuId in final int and then use it in switch case.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
final int menuId = item.getItemId();
switch (menuId)
{
case R.id.menu_sign_out_all:
doHardShutdown();
return true;
case R.id.menu_add_account:
return true;
case R.id.menu_settings:
Intent sintent = new Intent(this, SettingActivity.class);
startActivityForResult(sintent,0);
return true;
case R.id.menu_import_keys:
importKeyStore();
return true;
}
return super.onOptionsItemSelected(item);
}
I'm beginning android developpement and i don't why my code is not working. The aim is simple : I have a main activity, a menu and a second activity. I want to send a float value from the main to the second activity and .. it's not working !
Here is my code from the main :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.menu_home:
return true;
case R.id.menu_settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.menu_battstat:
intent = new Intent(MainActivity.this, StatsActivity.class);
intent.putExtra("consumOn", 4);
intent.putExtra("consumOff", 5);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And the StatsActivity.class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats_main);
Intent intent = getIntent();
float consumOn = intent.getFloatExtra("consumOn", 0);
float consumOff = intent.getFloatExtra("consumOff", 0);
EditText editTextA = (EditText)findViewById(R.id.editText1);
editTextA.setText(String.valueOf(consumOn), TextView.BufferType.EDITABLE);
EditText editTextB = (EditText)findViewById(R.id.editText2);
editTextB.setText(String.valueOf(consumOff), TextView.BufferType.EDITABLE);
}
When I launch my code, it stills 0 and not a 4 and 5. Any ideas ? Thx.
Replace the getFloatExtra with
Bundle bundle = getIntent().getExtras();
float yourFloat = bundle.getFloat("key");
I'm going to go out on a limb here and say that maybe the putExtra is being interpreted as int rather than a float so it cannot find it. Try replacing these lines
intent.putExtra("consumOn", 4);
intent.putExtra("consumOff", 5);
with
intent.putExtra("consumOn", 4f);
intent.putExtra("consumOff", 5f);
because you haven't actually defined their type anywhere and they aren't variables.
You should pass bundle with your floats to intent and only then extract them
I am actually surprised that your intent is launching at all.. it seems like you only initialize the intent in the first case try moving Intent declaration out of the switch
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.menu_about:
intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.menu_home:
return true;
case R.id.menu_settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.menu_battstat:
intent = new Intent(MainActivity.this, StatsActivity.class);
intent.putExtra("consumOn", 4f);
intent.putExtra("consumOff", 5f);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
i'm not really sure what i'm doing wrong. I am working on a 6 button menu. The buttons Display but do not call the activity, and i cant see any text displayed on the menu button. They appear Blank, Help Please!!
package com.cerealBarApps;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class FirstLoginActivity extends Activity {
protected void onCreate(Bundle Ebenezersbundle)
{
super.onCreate(Ebenezersbundle);
setContentView(R.layout.testlayout);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.main_menu, menu);
return true;
}
/*
* Intent nextScreen = new Intent(getApplicationContext(),
* AllFaculty.class); // Sending data to another Activity
* startActivity(nextScreen);
*/
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Intent intent1 = new Intent(this, SMS.class);
startActivity(intent1);
break;
case 2:
Intent intent2 = new Intent(this, MenuRecieved.class);
startActivity(intent2);
break;
case 3:
Intent intent3 = new Intent(this, MenuSent.class);
startActivity(intent3);
case 4:
Intent intent4 = new Intent(this, MenuSettings.class);
startActivity(intent4);
case 5:
Intent intent5 = new Intent(this, MenuExit.class);
startActivity(intent5);
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
Menu XML:
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="xmlns:android= http:/schemas.android.com/apk/res/android" >
<item
android:id="#+id/menuMenu"
android:alphabeticShortcut="m"
android:title="MenuTest"/>
<item
android:id="#+id/menuNewMessage"
android:alphabeticShortcut="n"
android:title="New Message"/>
<item
android:id="#+id/menuSent"
android:alphabeticShortcut="s"
android:title="Sent"/>
<item
android:id="#+id/menuRecieved"
android:alphabeticShortcut="r"
android:title="Recieved"/>
<item
android:id="#+id/menuSettings"
android:alphabeticShortcut="s"
android:title="Settings"/>
<item
android:id="#+id/menuExit"
android:alphabeticShortcut="e"
android:title="Exit"/>
</menu
Code:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuNewMessage:
startActivity(new Intent(getApplicationContext(), SMS.class));
return true;
case R.id.menuSent:
startActivity(new Intent("com.cerealBarApps"));
return true;
case R.id.menuRecieved:
startActivity(new Intent("com.cerealBarApps"));
return true;
case R.id.menuSettings:
startActivity(new Intent("com.cerealBarApps"));
return true;
case R.id.menuExit:
startActivity(new Intent("com.cerealBarApps"));
return true;
}
return false;
}
}
Remove return false and replace it with:
return super.onOptionsItemSelected(menuItem);
follow the below code
#Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId())
{
case 0:
this.SaveData();
break;
case 1:
Intent intent2 = new Intent(DryWall.this,Help.class);
startActivity(intent2);
break;
default:
break;
}
return super.onOptionsItemSelected(menuItem);
}
Hey guys i figured it out, it was because i didnt do an onCreateOptionsMenu method. Thanks gusy for the help
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, menuNewMessage, 0, "New Message");
menu.add(0, menuSent, 0, "Outbox");
menu.add(0, menuRecieved, 0, "Inbox");
menu.add(0, menuSettings, 0, "Settings");
menu.add(0, menuExit, 0, "Exit");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case menuNewMessage:
startActivity(new Intent(getApplicationContext(), SMS.class));
break;
case menuSent:
startActivity(new Intent(getApplicationContext(), MenuSent.class));
break;
case menuRecieved:
startActivity(new Intent(getApplicationContext(),
MenuRecieved.class));
break;
case menuSettings:
startActivity(new Intent(getApplicationContext(),
MenuSettings.class));
break;
case menuExit:
startActivity(new Intent(getApplicationContext(), MenuExit.class));
break;
}
return super.onOptionsItemSelected(item);
}
}