用旋转器打开对话框强制关闭

时间:2013-06-27 22:51:45

标签: android android-resources android-dialog

在尝试填充我的微调器之前,我能够让对话框弹出一次,所以我相信错误是将信息加载到微调器中。在按钮上单击我启动一个对话框,它强制关闭此错误:

06-27 18:46:28.101: E/AndroidRuntime(26120): FATAL EXCEPTION: main
06-27 18:46:28.101: E/AndroidRuntime(26120): java.lang.IllegalStateException: Could not execute method of the activity
06-27 18:46:28.101: E/AndroidRuntime(26120):    at android.view.View$1.onClick(View.java:3609)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at android.view.View.performClick(View.java:4102)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at android.view.View$PerformClick.run(View.java:17126)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at android.os.Handler.handleCallback(Handler.java:615)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at android.os.Looper.loop(Looper.java:155)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at android.app.ActivityThread.main(ActivityThread.java:5536)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at java.lang.reflect.Method.invokeNative(Native Method)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at java.lang.reflect.Method.invoke(Method.java:511)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at dalvik.system.NativeStart.main(Native Method)
06-27 18:46:28.101: E/AndroidRuntime(26120): Caused by: java.lang.reflect.InvocationTargetException
06-27 18:46:28.101: E/AndroidRuntime(26120):    at java.lang.reflect.Method.invokeNative(Native Method)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at java.lang.reflect.Method.invoke(Method.java:511)
06-27 18:46:28.101: E/AndroidRuntime(26120):    at android.view.View$1.onClick(View.java:3604)
06-27 18:46:28.101: E/AndroidRuntime(26120):    ... 11 more
06-27 18:46:28.101: E/AndroidRuntime(26120): Caused by: java.lang.NullPointerException
06-27 18:46:28.101: E/AndroidRuntime(26120):    at com.example.beerportfoliopro.TasteTags.addTaste(TasteTags.java:71)

带有onclick方法按钮的页面是:

public class TasteTags extends Activity {

    BeerData e;

    String[] tastes = {
            "Dwight D. Eisenhower",
            "John F. Kennedy",
            "Lyndon B. Johnson",
            "Richard Nixon",
            "Gerald Ford",
            "Jimmy Carter",
            "Ronald Reagan",
            "George H. W. Bush",
            "Bill Clinton",
            "George W. Bush",
            "Barack Obama"
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tastetag_page);

        //get beer data
        Intent intent = getIntent();
        Bundle b = intent.getExtras();
        e = b.getParcelable("myBeerObject");

        TextView beerTitle = (TextView) findViewById(R.id.beerTitleTaste);

        beerTitle.setText(e.beerName + " Taste Profile");

        String url = myURL;

        url = url + "b=" +e.beerId;

        //async task to get beer taste tag percents
        new GetTasteJSON(this).execute(url);




    }

    public void addTaste(View v){

        // custom dialog
        final Dialog dialog = new Dialog(this);

        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.taste_array, android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);


        dialog.setContentView(R.layout.add_taste_dialog);
        dialog.setTitle("Add Taste");
        dialog.show();


    }





}

1 个答案:

答案 0 :(得分:1)

如果假设旋转器在对话框中,则问题是这个

Spinner spinner = (Spinner) findViewById(R.id.spinner1);

spinner为null,因为视图中没有spinner1

此行dialog.setContentView(R.layout.add_taste_dialog);添加

后需要执行的操作
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);

您需要先设置对话框的视图,然后从对话框中获取所有视图元素