0

我有一个 4RadioButton秒的对话框,RadioGroup我试图返回单击的按钮的 ID,但我一直得到一个NullPointerException. 我看过几个例子,看不出我的有什么不同。

    <RadioGroup
             android:id="@+id/radiojqmobdiv"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:checkedButton="0"
             android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/jqpage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/jqpage" 
                android:checked="true" />

            <RadioButton
                android:id="@+id/jqheader"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/jqheader" />

            <RadioButton
                android:id="@+id/jqcontent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/jqcontent" />

            <RadioButton
                android:id="@+id/jqfooter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/jqfooter" />

        </RadioGroup>

代码:

@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        preferences = getSharedPreferences("MYPREFS", 0);

        et = (EditTextLineNumbers) findViewById(R.id.ide);
        et.setTextColor(preferences.getInt("colourChoice", Color.GREEN));
        et.setMaxLines(5000);
        divBtn = (Button) findViewById(R.id.divbutton);
        divGroup = (RadioGroup) findViewById(R.id.radiojqmobdiv);

        exists = false;
        gestureDetector = new GestureDetector(this, new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };
        web = (WebView) findViewById(R.id.webpreview);
        web.setOnClickListener(this);
        web.setOnTouchListener(gestureListener);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        File dir = new File(Environment.getExternalStorageDirectory()
                + "/My Webs");
        currentDirectory = dir;
        ListView lv = getListView();
        registerForContextMenu(lv);
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> a, View v,
                    int position, long id) {
                Show_Alert_box(v.getContext(), "Please select action.",
                        position);
                return false;
            }
        });

        et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                changed = true;
                startPos = et.getSelectionStart();
                endPos = et.getSelectionEnd();
            }
        });
        if (dir.isDirectory()) {
            browseToRoot();
        } else {
            dir.mkdir();
        }

        divBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(MainActivity.this);
                dialog.setContentView(R.layout.divdialog);
                dialog.setTitle("Inserv Div");

                // set the custom dialog components - text, image and button
                // TextView text = (TextView) dialog.findViewById(R.id.text);
                // text.setText("Android custom dialog example!");
                // ImageView image = (ImageView)
                // dialog.findViewById(R.id.image);
                // image.setImageResource(R.drawable.ic_launcher);

                **Button insertButton = (Button) dialog
                        .findViewById(R.id.insertBtn);**
                // if button is clicked, close the custom dialog
                insertButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // int selectedID = divGroup.getCheckedRadioButtonId();

                        // find the radiobutton by returned id
                        divRdoBtn = (RadioButton) findViewById(selectedID);

                        Toast.makeText(MainActivity.this, divRdoBtn.getText(),
                                Toast.LENGTH_LONG).show();
                    }
                });

                Button cancelButton = (Button) dialog
                        .findViewById(R.id.cancelBtn);
                cancelButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }

        });

        gestureDetector = new GestureDetector(this, new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };
        loadPrefs();
    }
4

1 回答 1

1

您没有发布对话框布局和活动布局的完整 xml 文件,所以我在这里做一些假设。

但是,您在标题中说对话框包含您命名为 divGroup 的 RadioGroup。如果这是真的,那么当您执行此行时,您将无法找到 RadioGroup 是有道理的:

divGroup = (RadioGroup) findViewById(R.id.radiojqmobdiv);

您正在 Activity 中搜索 id R.id.radiojqmobdiv,您暗示它仅存在于您尚未创建的对话框中。如果未找到视图,findViewById() 返回 null,因此您将 null 分配给divGroup.

您应该在声明对话框后将该行向下移动,并确保调用findViewById您的dialog对象而不是 Activity。

final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.divdialog);
divGroup = (RadioGroup) dialog.findViewById(R.id.radiojqmobdiv);
于 2013-04-11T01:03:51.953 回答