Android 许多带有单选按钮的自定义对话框

Android 许多带有单选按钮的自定义对话框,android,Android,我有一个简单的应用程序,使用对话框进行除法、减法等运算,并带有循环条件 我的错误 02-08 17:29:23.271: W/KeyCharacterMap(744): No keyboard for id 0 02-08 17:29:23.271: W/KeyCharacterMap(744): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 02-08 17:29:24.011: D/dalvikvm(744): GC_EX

我有一个简单的应用程序,使用对话框进行除法、减法等运算,并带有循环条件

我的错误

02-08 17:29:23.271: W/KeyCharacterMap(744): No keyboard for id 0
02-08 17:29:23.271: W/KeyCharacterMap(744): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-08 17:29:24.011: D/dalvikvm(744): GC_EXTERNAL_ALLOC freed 72K, 52% free 2611K/5379K, external 914K/1038K, paused 59ms
02-08 17:29:26.101: W/KeyCharacterMap(744): No keyboard for id 0
02-08 17:29:26.101: W/KeyCharacterMap(744): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-08 17:29:30.311: D/AndroidRuntime(744): Shutting down VM
02-08 17:29:30.311: W/dalvikvm(744): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-08 17:29:30.321: E/AndroidRuntime(744): FATAL EXCEPTION: main
02-08 17:29:30.321: E/AndroidRuntime(744): java.lang.NullPointerException
02-08 17:29:30.321: E/AndroidRuntime(744):  at com.example.dialog_looping.MainActivity$1$1$1.onClick(MainActivity.java:72)
02-08 17:29:30.321: E/AndroidRuntime(744):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
02-08 17:29:30.321: E/AndroidRuntime(744):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 17:29:30.321: E/AndroidRuntime(744):  at android.os.Looper.loop(Looper.java:123)
02-08 17:29:30.321: E/AndroidRuntime(744):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-08 17:29:30.321: E/AndroidRuntime(744):  at java.lang.reflect.Method.invokeNative(Native Method)
02-08 17:29:30.321: E/AndroidRuntime(744):  at java.lang.reflect.Method.invoke(Method.java:507)
02-08 17:29:30.321: E/AndroidRuntime(744):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-08 17:29:30.321: E/AndroidRuntime(744):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-08 17:29:30.321: E/AndroidRuntime(744):  at dalvik.system.NativeStart.main(Native Method)
02-08 17:34:30.490: I/Process(744): Sending signal. PID: 744 SIG: 9
这是我的完整代码使用对话框循环减法,除法等

public class MainActivity extends Activity {
    int result=0,i;
    int [] arr;
    int indexArray = 0;
    String Oprtr;
    RadioGroup operator_mode;
    RadioButton rb;
    @Override  
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        final LayoutInflater inflater = this.getLayoutInflater();
        final View v=inflater.inflate(R.layout.dialoginput, null);

        operator_mode=(RadioGroup)findViewById(R.id.operator);


        //operator_mode.setOnClickListener(l)(null);

        new AlertDialog.Builder(MainActivity.this)
            .setIcon(R.drawable.ic_launcher)
            .setTitle("Total Operator")
            .setView(v)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                 public void onClick(final DialogInterface dialog, int whichButton) {
                      EditText op = (EditText)v.findViewById(R.id.operator);
                      arr=new int [Integer.valueOf(op.getText().toString())+1];
                      for (i = 0;i<arr.length;i++){


                            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
                            final View v2=inflater.inflate(R.layout.input, null) ;

                            new AlertDialog.Builder(MainActivity.this)
                                .setIcon(R.drawable.ic_launcher)
                                .setTitle("number:")
                                .setView(v2)
                                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dlg, int sumthin) {

                                        EditText number = (EditText)v2.findViewById(R.id.number);
                                        arr[indexArray]= Integer.valueOf(number.getText().toString());
                                        //Log.v ("aa",Integer.toString(arr[indexArray]));

                                        LayoutInflater inflater = MainActivity.this.getLayoutInflater();
                                        final View v3=inflater.inflate(R.layout.operator, null) ;

                                        new AlertDialog.Builder(MainActivity.this)
                                        .setIcon(R.drawable.ic_launcher)
                                        .setTitle("select Operator:")
                                        .setView(v3)
                                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dlg, int sumthin) {
                                                sumthin = operator_mode.getCheckedRadioButtonId();
                                                rb = (RadioButton) findViewById(sumthin);
                                                switch(sumthin){
                                                case R.id.plus:
                                                    Oprtr = "+";
                                                    Log.v ("aa",Oprtr);
                                                    /*
                                                    if (indexArray==0){
                                                        result = arr[indexArray];
                                                        if (result<0){
                                                            new AlertDialog.Builder(MainActivity.this)
                                                                .setTitle("Error")
                                                                .setMessage("result can't negative value")
                                                                .setNeutralButton("Close", new DialogInterface.OnClickListener() {
                                                                    public void onClick(DialogInterface dlg, int sumthin) {
                                                                        finish();
                                                                    }
                                                                })
                                                                .show();
                                                            } else if (result>9999999){
                                                                new AlertDialog.Builder(MainActivity.this)
                                                                .setTitle("Error")
                                                                .setMessage("result can't > 9999999")
                                                                .setNeutralButton("Close", new DialogInterface.OnClickListener() {
                                                                    public void onClick(DialogInterface dlg, int sumthin) {
                                                                        finish();
                                                                    }
                                                                })
                                                                .show();
                                                            }else {
                                                                new AlertDialog.Builder(MainActivity.this)
                                                                .setTitle("result")
                                                                .setMessage(Integer.toString(result))
                                                                .setNeutralButton("Close", new DialogInterface.OnClickListener() {
                                                                    public void onClick(DialogInterface dlg, int sumthin) {
                                                                    }
                                                                })
                                                                .show();
                                                            }
                                                    } else {
                                                            result = result + arr[indexArray];
                                                            if (result<0){
                                                            new AlertDialog.Builder(MainActivity.this)
                                                                .setTitle("Error")
                                                                .setMessage("result can't negative value")
                                                                .setNeutralButton("Close", new DialogInterface.OnClickListener() {
                                                                    public void onClick(DialogInterface dlg, int sumthin) {
                                                                        finish();
                                                                    }
                                                                })
                                                                .show();
                                                            } else if (result>9999999){
                                                                new AlertDialog.Builder(MainActivity.this)
                                                                .setTitle("Error")
                                                                .setMessage("result can't > 9999999")
                                                                .setNeutralButton("Close", new DialogInterface.OnClickListener() {
                                                                    public void onClick(DialogInterface dlg, int sumthin) {
                                                                        finish();
                                                                    }
                                                                })
                                                                .show();
                                                            }else {
                                                                new AlertDialog.Builder(MainActivity.this)
                                                                .setTitle("result")
                                                                .setMessage(Integer.toString(result))
                                                                .setNeutralButton("Close", new DialogInterface.OnClickListener() {
                                                                    public void onClick(DialogInterface dlg, int sumthin) {
                                                                    }
                                                                })
                                                                .show();
                                                            }
                                                    }                                   
                                                    indexArray++;
                                                    */
                                                    break;
                                                case R.id.minus:
                                                    //str="Meeting Mode";
                                                    break;
                                                case R.id.substract:
                                                    //str="Silent Mode";
                                                    break;
                                                case R.id.divide:
                                                    //str="Offline Mode";
                                                    break;
                                            }
                                            }
                                        })
                                        .show();
                                    }
                                })
                                .show();
                        }
                 }
            })

            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                 finish();
              }
            })
            .show();
    }
}
公共类MainActivity扩展活动{
int结果=0,i;
int[]arr;
int indexArray=0;
字符串Oprtr;
射线组操作员\u模式;
无线电按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
最终LayoutInflater充气机=this.getLayoutInflater();
最终视图v=充气机充气(R.layout.dialoginput,空);
操作符\模式=(放射组)findViewById(R.id.operator);
//运算符_mode.setOnClickListener(l)(空);
新建AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_启动器)
.setTitle(“总操作员”)
.setView(v)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(最终对话框界面对话框,int whichButton){
EditText op=(EditText)v.findviewbyd(R.id.operator);
arr=newint[Integer.valueOf(op.getText().toString())+1];

对于(i=0;i我认为您的
RadioGroup
为空。您需要将视图设置为

setContentView(R.layout.your\u视图)

然后,如果需要为视图充气,请使用:

operator\u mode=(放射组)v.findViewById(R.id.operator);

而不是

operator\u mode=(放射组)findViewById(R.id.operator);

编辑

您可以使用充气机,但必须执行以下操作:

Window window = getWindow();

    window.setContentView(v);`

但是您仍然在调用第72行是什么?sumshin=operator\u mode.getCheckedRadioButtonId();我已经更改了operator\u mode=(RadioGroup)findViewById(R.id.operator);to operator\u mode=(RadioGroup)v.findViewById(R.id.operator);但在02-08 18:11:54.470:E/AndroidRuntime(816)时仍然出错:java.lang.ClassCastException:android.widget.EditText 02-08 18:11:54.470:E/AndroidRuntime(816):在com.example.dialog_looping.MainActivity$1$1.onClick(MainActivity.java:65)运算符是布局还是
RadioGroup
?布局和RadioGroup…我的布局名称是运算符,然后我的radiorub名称是运算符第65行是什么…可能我看错了?看起来您试图将某些内容强制转换到不是第65行的
EditText
是new AlertDialog.Builder(MainActivity.this)