每當我點擊我的兩個圖像按鈕(EditButton,SaveButton)之一,我的應用程序崩潰。ImageButton崩潰的應用程序
的錯誤是一樣的,每次:
java.lang.IllegalStateException:找不到在活動課 com.example.groceryrunner.MainActivity爲的onClick處理程序的方法 onSaveClicked(視圖)和視圖 類的android.widget.ImageButton與ID'SaveButton'
事情是我什至沒有onSaveClicked(視圖)方法在任何事件在我的XML。我嘗試了各種組合方式,按鈕應該去點擊(onCreateLGClick),但它不影響任何事情,因爲我的應用程序永遠不會到達那裏。此外,唯一可以工作的按鈕並不一致,很多時候沒有任何反應,或者需要10次點擊才能使其事件停止,即使它之前已經工作(CreateLG按鈕)。
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>
菜單(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>
活動主(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();
}
}
安置自己的createLGMenu.xml,也被稱爲後的main.xml – Ogen
發佈兩個菜單XML。謝謝。 – user2100364
您是否嘗試清潔您的項目? – Ogen