0

我在一个活动中有四个按钮,所以我使用的是 onClick 方法。其中一个(这就是这里的问题Ee应该显示一个内部有两个微调器的对话框。这是代码:

public void onClick(View view) {

        switch (view.getId()) {

        case R.id.bAddNew:

        // first button - not related

            break;

        case R.id.bAdvancedSettings:

            // Spinner 1
            plan = (Spinner) findViewById(R.id.spinner1);
            ArrayAdapter<CharSequence> spinner1Adapter = ArrayAdapter
                    .createFromResource(this, R.array.paymentplan,
                            android.R.layout.simple_spinner_item);
            spinner1Adapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            plan.setAdapter(spinner1Adapter);
            plan.setSelection(0);
            plan.setOnItemSelectedListener(new MyOnItemSelectedListenerPlan());

            // Spinner 2
            display = (Spinner) findViewById(R.id.spinner2);
            ArrayAdapter<CharSequence> spinner2Adapter = ArrayAdapter
                    .createFromResource(this, R.array.quickview,
                            android.R.layout.simple_spinner_item);
            spinner2Adapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            display.setAdapter(spinner2Adapter);
            display.setSelection(0);
            display.setOnItemSelectedListener(new MyOnItemSelectedListenerDisplay());

            final Dialog dialog = new Dialog(ManageDebts.this);
            dialog.setContentView(R.layout.advanced);
            dialog.setTitle("Advanced Details");
            dialog.setCancelable(true);

            Button Submit = (Button) findViewById(R.id.buttonAdvancedSubmit);
            Submit.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {

                    dialog.dismiss();

                }
            });

break;

// next two buttons

}

这是我得到的 LogCat:

11-30 09:50:52.305: E/AndroidRuntime(10747): FATAL EXCEPTION: main
11-30 09:50:52.305: E/AndroidRuntime(10747): java.lang.IllegalStateException: Could not execute method of the activity
11-30 09:50:52.305: E/AndroidRuntime(10747):    at android.view.View$1.onClick(View.java:3597)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at android.view.View.performClick(View.java:4202)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at android.view.View$PerformClick.run(View.java:17340)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at android.os.Handler.handleCallback(Handler.java:725)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at android.os.Looper.loop(Looper.java:137)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at android.app.ActivityThread.main(ActivityThread.java:5039)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at java.lang.reflect.Method.invokeNative(Native Method)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at java.lang.reflect.Method.invoke(Method.java:511)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at dalvik.system.NativeStart.main(Native Method)
11-30 09:50:52.305: E/AndroidRuntime(10747): Caused by: java.lang.reflect.InvocationTargetException
11-30 09:50:52.305: E/AndroidRuntime(10747):    at java.lang.reflect.Method.invokeNative(Native Method)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at java.lang.reflect.Method.invoke(Method.java:511)
11-30 09:50:52.305: E/AndroidRuntime(10747):    at android.view.View$1.onClick(View.java:3592)
11-30 09:50:52.305: E/AndroidRuntime(10747):    ... 11 more
11-30 09:50:52.305: E/AndroidRuntime(10747): Caused by: java.lang.NullPointerException
11-30 09:50:52.305: E/AndroidRuntime(10747):    at com.---.---.ManageDebts.onClick(ManageDebts.java:211)
11-30 09:50:52.305: E/AndroidRuntime(10747):    ... 14 more

第 211 行是

plan.setAdapter(spinner1Adapter);

任何人都可以看到问题是什么?我做过很多对话和微调,但这是第一次在一起。不确定我是否只是不知道该怎么做,或者这只是我忽略的小事。

4

2 回答 2

1

我想这是问题所在

 Button Submit = (Button) findViewById(R.id.buttonAdvancedSubmit);

用。。。来代替

 Button Submit = (Button) dialog .findViewById(R.id.buttonAdvancedSubmit);
于 2012-11-30T18:01:54.837 回答
1

你的问题是你试图Spinner从你的Activity内容视图中找到。

只需致电:

plan = (Spinner) dialog.findViewById(R.id.spinner1);

对于其中的所有其他视图Dialog

dialog.setContentView()在查找这些视图之前,您必须调用。

于 2012-11-30T18:04:11.647 回答