代码之家  ›  专栏  ›  技术社区  ›  HelloWorld1

在alertdialog中检索所选单选按钮

  •  -1
  • HelloWorld1  · 技术社区  · 8 年前

    目标:


    无法在SpinnerDialogItemSelected方法中使用findViewByid检索该值,因为我收到一条错误消息:

    java.lang.NullPointerException:尝试调用虚拟方法“int” null对象上的android.widget.RadioGroup.getCheckedRadioButtonId()' 参考

    信息:
    *我是Android新手

    谢谢您!


    活动\u main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
            <Button
                android:id="@+id/alertFormElements2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="alertFormElements2"
                android:text="Button" />
    
    </LinearLayout>
    

    表单\u elements.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <RadioGroup
            android:id="@+id/rgtest1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="50dp">
    
            <RadioButton
                android:id="@+id/test1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:checked="true"
                android:text="test1" />
    
            <RadioButton
                android:id="@+id/test2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="test1" />
    
        </RadioGroup>
    
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="131dp" />
    
    </RelativeLayout>
    

    主活动.java

    package com.jfdimarzio.t1;
    
    import android.annotation.TargetApi;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    
    import java.util.ArrayList;
    
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
    
        private int _data = 0;
    
    
        /*
         * Show AlertDialog with some form elements.
         */
        public void alertFormElements2(View vdf) {
    
            dddd();
        }
    
    
        private void dddd()
        {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View formElementsView = inflater.inflate(R.layout.form_elements2,null, false);
    
            String[] vvalue = new String[]{
                    "7",
                    "3",
                    "6",
                    "3",
                    "8",
                    "9",
                    "15",
                    "11",
                    "57"
            };
    
    
            final Spinner _spinner = formElementsView.findViewById(R.id.spinner);
            ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_item, vvalue);
            spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
            _spinner.setAdapter(spinnerArrayAdapter);
    
            // the alert dialog
            new AlertDialog.Builder(MainActivity.this).setView(formElementsView)
                    .setTitle("Form Elements")
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
    
                        public void onClick(DialogInterface dialog, int id) {
                            SpinnerDialogItemSelected(_spinner.getSelectedItem().toString());
                            dialog.dismiss();
                        }
                    }).show();
    
        }
    
    
        private void SpinnerDialogItemSelected(String value)
        {
            RadioGroup myRadioGroup  = (RadioGroup) findViewById(R.id.rgtest1);
            int selectedId = myRadioGroup.getCheckedRadioButtonId();
        }
    
    
    } // Class
    
    4 回复  |  直到 8 年前
        1
  •  0
  •   shubhamgarg1    8 年前

    请尝试将以编程方式膨胀的视图传递给:

     private void SpinnerDialogItemSelected(String value, View formElementsView)
     { 
        RadioGroup myRadioGroup  = (RadioGroup) formElementsView.findViewById(R.id.rgtest1);
        int selectedId = myRadioGroup.getCheckedRadioButtonId();
     }
    
        2
  •  0
  •   ʍѳђઽ૯ท    8 年前
    RadioGroup myRadioGroup  = (RadioGroup) formElementsView.findViewById(R.id.rgtest1);
    
        3
  •  0
  •   kingston    8 年前

    在里面 activity_main.xml 你没有 @id/rgtest1 RadioGroup formElementsView

    radioGroup  = (RadioGroup) formElementsView.findViewById(R.id.rgtest1);
    

    这个 View 您创建的AlertDialog的层次结构不会附加到 查看 Activity 就像你想的那样。这不是安卓的工作方式。

        4
  •  0
  •   Viddyut Khanvilkar    8 年前

    我认为您应该实现radiogroup的OnCheckedChangeListener()。。然后可以在OnCheckedChangeListener()的onClick()方法中。。保存它(在类似SharedPreferences的地方)并重用它。

        RadioGroup radioGroup;
        SharedPref sharedPref;
        radioGroup = findViewById(R.id.rb1);
        sharedPref = new SharedPref(getContext());
    
    
    radioGroup.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener() 
       {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton radioButton = group.findViewById(checkedId);
        int id=radioButton.getId();
        sharedPref.someMethod(id);
    
    }
    });