ImageButton崩溃的应用程序

时间:2014-01-24 02:23:16

标签: android image

每当我点击我的两个图像按钮之一(EditButton,SaveButton)时,我的应用程序崩溃了。

每次错误都是一样的:

  

java.lang.IllegalStateException:找不到方法   活动类中的onSaveClicked(视图)   查看onClick处理程序的com.example.groceryrunner.MainActivity   类id为'SaveButton'的android.widget.ImageButton类

问题是我在xml中的任何事件中都没有onSaveClicked(view)方法。我已经尝试了各种组合,单击按钮时应该按下的实际方法(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>

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

  }

}

3 个答案:

答案 0 :(得分:1)

在你的

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

你已经夸大了main.xml菜单。你确定它不是你需要膨胀的createlgmenu.xml吗?

答案 1 :(得分:0)

尝试低于它在我身边的工作正常。希望对你也有用。

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

    }

}

答案 2 :(得分:0)

好吧,我发现了我的修复!首先,由于我遇到其他错误,我只是保存了我的代码并删除了Eclipse并重新安装/重新从Android网站中提取。该按钮不再崩溃我的应用程序。

此外,我没有被识别的xml文件是我在AdamM的帮助下修复的另一个重大障碍:

1)我删除了R import

2)注释掉了识别我的xml文件时出错的行

3)重启Eclipse&amp;全部建造