将一个微调器放在onClick中

时间:2012-11-30 17:58:44

标签: android dialog android-spinner

我在一个活动中有四个按钮,所以我使用的是onClick方法。其中之一(这就是问题,她只需要显示一个内部有两个微调器的Dialog。这是代码:

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

有人能看出问题所在吗?我做了很多对话和旋转器,但这是第一次在一起。不确定我是否只是不知道该怎么做,或者它是否只是我忽略的小事。

2 个答案:

答案 0 :(得分:1)

我想这是问题

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

替换为

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

答案 1 :(得分:1)

您的问题是,您正试图从Spinner内容视图中找到Activity

请致电:

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

对于Dialog

的所有其他观点

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