Reputation: 87
Whenever I hit one of my two image buttons (EditButton, SaveButton), my app crashes.
The error's the same everytime:
java.lang.IllegalStateException: Could not find a method onSaveClicked(view) in the activity class com.example.groceryrunner.MainActivity for onClick handler on view class android.widget.ImageButton with id 'SaveButton'
The thing is I don't even have an onSaveClicked(view) method in any event in my xml. I've tried all sorts of combinations with the actual method that the button is supposed to go to when clicked (onCreateLGClick), but it doesn't affect anything since my app never gets there. Also, the only button that does work isn't consistent, many times nothing happens or takes 10 clicks to make its event go off, even though it's worked before(CreateLG button).
createlgmenu (xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/Create_List"
android:title="@string/Create_List"/>
<item
android:id="@+id/Create_Food_Group"
android:title="@string/Create_Food_Group"/>
</menu>
menu (xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
activity main (xml):
<TextView
android:id="@+id/GetStarted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/ListName"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:text="Select or Create a list to get started!"
android:textSize="20sp" />
<ImageButton
android:id="@+id/EditButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignRight="@+id/SaveButton"
android:layout_alignTop="@+id/ListName"
android:layout_marginRight="32dp"
android:background="@null"
android:scaleType="centerInside"
android:src="@drawable/edit_button"
android:onClick="onCreateLGClick" />
<Button
android:id="@+id/CreateLG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/ListName"
android:layout_toRightOf="@+id/ListsButton"
android:background="@null"
android:text="+"
android:textSize="40sp"
android:onClick="onCreateLGClick" />
<TextView
android:id="@+id/ListName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="Grocery Runner"
android:textSize="22sp"
android:onClick="onCreateLGClick" />
<Button
android:id="@+id/ListsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/EditButton"
android:layout_alignLeft="@+id/GetStarted"
android:background="@null"
android:text="≡"
android:textSize="40sp" />
<ImageButton
android:id="@+id/SaveButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignRight="@+id/GetStarted"
android:layout_below="@+id/EditButton"
android:background="@null"
android:onClick="onCreateLGClick"
android:scaleType="centerInside"
android:src="@drawable/save_disk" />
</RelativeLayout>
MainActivity.java:
package com.example.groceryrunner;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.app.ActionBar;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button Save = (Button) this.findViewById(R.id.SaveButton);
}
@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;
}
public void onCreateLGClick(View v) {
final int id = v.getId();
/*switch (id) {
case R.id.CreateLG:
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGMenu(v);
break;
case R.id.ListsButton:
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGMenu(v);
break;
}*/
}
public void createLGMenu(View v) {
PopupMenu LGMenu = new PopupMenu(this, v);
LGMenu.getMenuInflater().inflate(R.menu.createlgmenu, LGMenu.getMenu());
LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
String choice = new String((String) item.getTitle());
if (choice == "Create_List") {
createListDialog();
}
else if (choice == "Create_Group") {
createListDialog();
}
return false;
}
});
LGMenu.show();
}
public AlertDialog.Builder dialogBuilder;
private void createListDialog() {
dialogBuilder = new AlertDialog.Builder(this);
EditText textInput = new EditText(this);
dialogBuilder.setTitle("Create list");
dialogBuilder.setMessage("Name your list: ");
dialogBuilder.setView(textInput);
dialogBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
findViewById(R.id.ListName).setVisibility(View.INVISIBLE);
//Toast.makeText(getApplicationContent(), "List has been created.", toast.LENGTH_SHORT);
// add list to ListsButton
//findViewById(R.id.ListName). -> Change ListName text to created list
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getApplicationContent(), "List has been created.", toast.LENGTH_SHORT);
}
});
// Output
AlertDialog dialogue = dialogBuilder.create();
dialogue.show();
}
}
Upvotes: 0
Views: 1441
Reputation: 87
Alright, I found my fix! First, since I was encountering other errors, I just saved my code and deleted Eclipse and reinstalled/extracted from Android's site again. The button is no longer crashing my app.
Furthermore, the xml files that weren't being recognized were another big hindrance that I fixed with the help from AdamM:
1) I deleted the R import
2) Commented out the lines that were giving me errors in recognizing my xml files
3) Restarted Eclipse & Built All
Upvotes: 0
Reputation: 23638
Try out below its working fine on my side. Hope will work for you also.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button Save = (Button) this.findViewById(R.id.SaveButton);
}
@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;
}
public void onCreateLGClick(View v) {
final int id = v.getId();
switch (v.getId()) {
case R.id.CreateLG:
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGMenu(v);
break;
case R.id.ListsButton:
findViewById(R.id.GetStarted).setVisibility(View.VISIBLE);
createLGMenu(v);
break;
}
}
public void createLGMenu(View v) {
PopupMenu LGMenu = new PopupMenu(this, v);
LGMenu.getMenuInflater().inflate(R.menu.createmenu, LGMenu.getMenu());
LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.Create_List:
createListDialog();
break;
case R.id.Create_Food_Group:
createListDialog();
break;
default:
break;
}
/*
* String choice = new String((String) item.getTitle()); if
* (choice.equalsIgnoreCase("Create_List")){ createListDialog();
* } else if (choice.equalsIgnoreCase("Create_Group")) {
* createListDialog(); }
*/
return true;
}
});
LGMenu.show();
}
public AlertDialog.Builder dialogBuilder;
private void createListDialog() {
dialogBuilder = new AlertDialog.Builder(this);
EditText textInput = new EditText(this);
dialogBuilder.setTitle("Create list");
dialogBuilder.setMessage("Name your list: ");
dialogBuilder.setView(textInput);
dialogBuilder.setPositiveButton("Create",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
findViewById(R.id.ListName).setVisibility(
View.INVISIBLE);
// Toast.makeText(getApplicationContent(),
// "List has been created.", toast.LENGTH_SHORT);
// add list to ListsButton
// findViewById(R.id.ListName). -> Change ListName text
// to created list
}
});
dialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(getApplicationContent(),
// "List has been created.", toast.LENGTH_SHORT);
}
});
// Output
AlertDialog dialogue = dialogBuilder.create();
dialogue.show();
}
}
Upvotes: 0
Reputation: 1566
In your
@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;
}
You have inflated the main.xml
menu. Are you sure its not the createlgmenu.xml
that you need to inflate?
Upvotes: 1