I'm trying to make my menu items go to different activities, so I tried to override the onOptionsItemSelcted on every activity, but I'm getting this error.
also getting "Constant expression required" on every case "R.id.ItemX".
#Override
public boolean onOptionsItemSelected(MenuItem menu){
switch (item.getItemId()){ // ERROR IS HERE ON THE WORD 'item', Cannot resolve symbol 'item'.
Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
case R.id.Item1: // Constant expression required
goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(goToNextActivity);
break;
case R.id.Item2: // Constant expression required
goToNextActivity = new Intent(getApplicationContext(), VidPage.class);
startActivity(goToNextActivity);
break;
case R.id.Item3: // Constant expression required
goToNextActivity = new Intent(getApplicationContext(), DatePage.class);
startActivity(goToNextActivity);
break;
}
return true;
}
Use this
menu.getItemId()
Instead of this
item.getItemId()
SAMPLE CODE
#Override
public boolean onOptionsItemSelected(MenuItem menu){
switch (menu.getItemId()){ // ERROR IS HERE ON THE WORD 'item', THE REST WORKS FINE.
Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
case R.id.Item1:
goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(goToNextActivity);
break;
case R.id.Item2:
goToNextActivity = new Intent(getApplicationContext(), VidPage.class);
startActivity(goToNextActivity);
break;
case R.id.Item3:
goToNextActivity = new Intent(getApplicationContext(), DatePage.class);
startActivity(goToNextActivity);
break;
}
return true;
}
use this
menu.getItemId()
this will return you the item id,
Related
I wrote this code following the skeleton of Reto Meier's "Professional Android 4 Application Development" and some slide of my professor, but i can't understand why the new activity (PreferencesActivity, fully coded) is not starting and is not raising any kind of errors: in the VM it just won't do anything when i press "Preferences" in the standard android menu I created.
I added the new activity in app's manifest correctly (just name, label, theme and screen orientation).
Here's the code
public class MainActivity extends Activity implements OnClickListener, OnValueChangeListener {
static final private int MENU_PREFERENCES = Menu.FIRST+1;
...
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_PREFERENCES, Menu.NONE, "Preferences");
return true;
}
public boolean onOptionsitemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case (MENU_PREFERENCES): {
Intent i = new Intent(this, PreferencesActivity.class);
startActivity(i);
return true;
}
}
return false;
}
...
}
The only strange thing I get is this warning in Logcat
06-20 14:50:49.760: W InputManagerService(699): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#41219950
You can use both of them
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
Intent i = new Intent(MainActivity.this, PreferencesActivity.class);
But it's better to use 1st one because in 2nd one memory leakage problem may occour and also just add this line in your manifest file.
<activity android:name=".PreferencesActivity" />
Your Code :
Intent i = new Intent(this, PreferencesActivity.class);
startActivity(i);
return true;
Instead of this you need to pass MainActivity.this
Intent i = new Intent(MainActivity.this, PreferencesActivity.class);
startActivity(i);
return true;
Issue is Proper context is not passing so its not starting Activity.
Instead of using this you could use getApplicationContext(), it gets you the context of the application object for the currents process.
Try this....
Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
startActivity(i);
This May Help You..
You need to pass MainActivity
Intent i = new Intent(MainActivity.this, PreferencesActivity.class);
startActivity(i);
return true;
Better to use menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected()");
switch (item.getItemId()) {
case android.R.id.yourId:
finish();
return true;
case R.id.Yourid:
return true;
default:
return super.onOptionsItemSelected(item);
You can also write
startActivity(new Intent(getApplicationContext(),NextActivity.class));
write your activity name in NextActivity.class
I have got ExpandableListView. It looks like this http://www.androidhive.info/wp-content/uploads/2013/07/android-expandable-listview.jpg . I just want to OnChildClick for example Despicable Me 2 go to the SecondActivity and send PutExtra string with "Despicable Me 2". I know how to use PutExtra and GetExtra method.
Can someone give me advice how to solve this problem? I tried to go with switches but it does not work it does not do anything(It just stay in ExpandableListView).
Here is MainActivity.java:
Public string movie;
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Intent intent0 = new Intent(getApplicationContext(), SecondActivity.class);
switch(groupPosition)
{
//Top250
case 0: switch(childPosition)
{
case 0: movie="The Conjuring";
intent0.putExtra("text1", movie);
startActivity(intent0);
break;
case 1: movie="Despicable Me 2";
intent0.putExtra("text1", movie);
startActivity(intent0);
break;
}
break;
//Now Showing
case 1: switch(childPosition)
{
case 0:
break;
}
break;
//Coming Soon
case 2:switch(childPosition)
{
case 0:
break;
}
break;
}
return false;
}
});
I downloaded this code from http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ you can see all of xml and activities here except SecondActivity.
try to put the intent inside the switch case
like this
case 0: switch(childPosition)
{
case 0: Intent intent0 = new Intent(getApplicationContext(), SecondActivity.class);
movie="The Conjuring";
intent0.putExtra("text1", movie);
startActivity(intent0);
break;
or
if(childPosition ==1)
Intent intent0 = new Intent(getApplicationContext(), SecondActivity.class);
movie="The Conjuring";
intent0.putExtra("text1", movie);
startActivity(intent0);
I am new to android development so there is probably something simple that is wrong. If you need any more info I will be glad to give that to you. Thanks in advance.
I am trying to add a button in my navdrawer.class. This is what I have.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.new_account:
Intent intent = new Intent(this, AddAccountActivity.class);
this.startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
I get an error.
since you´re into a fragment you must use:
Intent intent = new Intent(getActivity(), AddAccountActivity.class);
or
Intent intent = new Intent(getActivity().getApplicationContext(), AddAccountActivity.class);
for example see the context used in your Toast (getActivity())
Toast.makeText(getActivity(), "This Will Create A New Account.", Toast.LENGTH_SHORT).show();
You should write
Intent intent = new Intent(AddAccountActivity.this, AddAccountActivity.class);
instead of
Intent intent = new Intent(this, AddAccountActivity.class);
Am I right that this is the fragment instance? If this is the case, thats your problem. The intent constructor needs a context and an activity class to work.
Fragment does not inherit from context. You can get the underlying activity with the getActivity() method.
try this:
Intent intent = new Intent(getActivity(), AddAccountActivity.class);
I am trying to implement a side menu in my application it works fine for the most part, but the problem I have is that once I display de menu and try to scroll down the whole list turns white (background) and the text disappears.
http://i.imgur.com/6a6TgJJ.png
http://i.imgur.com/ykT7hCN.png
Above I attach two pictures showing the behavior of the menu, when I slide my finger from the left to the right the side menu shows, but if I scroll down in the menu, it becomes an empty white list, here is my code:
public class ControlApp extends SlidingActivity{
ArrayList<String> datos ;
ArrayAdapter<String> adaptador;
Intent intent;
private String[] mMenuLista;
// private MyCustomAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notificaciones);
setBehindContentView(R.layout.activity_menu);
Parse.initialize(this, "BqCCNsbb14MPgeWz3rznxO4DamuXUbsgiTug8P8I", "9j7GSLWnV46fkPsNMwnMD2FormAiclKlGitfDq2b");
ParseAnalytics.trackAppOpened(getIntent());
getSlidingMenu().setBehindOffset(100);
// mAdapter = new MyCustomAdapter();
PushService.setDefaultPushCallback(this, Notificaciones.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
mMenuLista = getResources().getStringArray(R.array.lista_menu);
// for(int i=0; i<=9;i++){
// mAdapter.addItem(mMenuLista[i]);
// if(i == 9){
// mAdapter.addSeparatorItem(mMenuLista[i]);
// }
// }
ListView primario = (ListView) findViewById(R.id.left_drawer);
primario.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list, mMenuLista));
primario.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> pariente, View view, int posicion, long id) {
selectItem(posicion);
}
});
}
public void selectItem(int posicion){
switch (posicion) {
case 0:
intent = new Intent(ControlApp.this, Notificaciones.class);
startActivity(intent);
break;
case 1:
intent = new Intent(ControlApp.this, Calificaiones.class);
startActivity(intent);
break;
case 2:
intent = new Intent(ControlApp.this, Mensajes.class);
startActivity(intent);
break;
case 3:
intent = new Intent(ControlApp.this, Citas.class);
startActivity(intent);
break;
case 4:
intent = new Intent(ControlApp.this, Permisos.class);
startActivity(intent);
break;
case 5:
intent = new Intent(ControlApp.this, Eventos.class);
startActivity(intent);
break;
case 6:
intent = new Intent(ControlApp.this, Horarios.class);
startActivity(intent);
break;
case 7:
intent = new Intent(ControlApp.this, Circulares.class);
startActivity(intent);
break;
case 8:
intent = new Intent(ControlApp.this, ProgramaDeEstudios.class);
startActivity(intent);
break;
case 9:
intent = new Intent(ControlApp.this, Ajustes.class);
startActivity(intent);
break;
case 10:
ParseUser.logOut();
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser == null){
// sesion cerrada correctamente
Toast toast = Toast.makeText(getApplicationContext(), "Sesión cerrada correctamente", Toast.LENGTH_SHORT);
toast.show();
Intent regis = new Intent (ControlApp.this, MainActivity.class);
startActivity(regis);
finish();
}else{
Toast toast = Toast.makeText(getApplicationContext(), "No se logro cerrar sesion, intentelo de nuevo", Toast.LENGTH_SHORT);
toast.show();
}
break;
default:
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I hope someone can help me find my mistakes so I can correct the error. thank you!!!
I'm guessing you are manually setting the background color of a ListView in your menu to black, while the main activity theme has a white background?
You need to set the android:cacheColorHint attribute on the ListView to match the list background color. The white is appearing due to rendering optimization by Android which uses that cacheColorHint value to quickly redraw the list while scrolling.
I read a good blog post detailing this issue once. I can't find it right now but will link to it if I do :)
I have an issue whereby, the back button works fine. Unless you push the home button, then re-enter the application, then push the back button again. It then quits the App, because their is no task trail (of activities)
Here is my colleagues code, of which I am trying to fix. Android.R.id.home is the problematic soft back button, although same thing is happening with OS back button.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case android.R.id.home:
activity.finish();
return true;
case R.id.menu_paymentLocs:
intent = new Intent(activity, PaymentLocationsPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
activity.startActivity(intent);
return true;
case R.id.menu_feedback:
intent = new Intent(activity, FeedbackPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
activity.startActivity(intent);
return true;
case R.id.menu_about:
intent = new Intent(activity, AboutPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
activity.startActivity(intent);
return true;
case R.id.menu_changeconsumer:
new SelectConsumerDialogFragment().show(getFragmentManager(), "select_consumer");
return true;
case R.id.menu_logout:
intent = new Intent(activity, SplashPage.class);
myMeter.logout();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
activity.startActivity(intent);
return true;
}
return true;
}
To prevent the back button from doing anything predefined you need to override the onbackpressed() method
try this if you are using api level 2.0 or higher
#Override
public void onBackPressed() {
// Do Here what ever you want do on back press;
}
Remove all intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);