Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无线组上的空指针异常_Java_Android_Radio Button_Popupwindow - Fatal编程技术网

Java 无线组上的空指针异常

Java 无线组上的空指针异常,java,android,radio-button,popupwindow,Java,Android,Radio Button,Popupwindow,我正在尝试在活动中创建一个弹出窗口。当我们单击活动中的按钮时,弹出窗口显示。弹出窗口的布局是显示包含4个单选按钮、一个按钮和一个搜索栏的单选组,当按下弹出窗口中的按钮时,必须根据所选单选按钮向主活动返回一个值 当我运行应用程序并打开弹出窗口时,会出现以下错误: “java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widge

我正在尝试在活动中创建一个弹出窗口。当我们单击活动中的按钮时,弹出窗口显示。弹出窗口的布局是显示包含4个单选按钮、一个按钮和一个搜索栏的单选组,当按下弹出窗口中的按钮时,必须根据所选单选按钮向主活动返回一个值

当我运行应用程序并打开弹出窗口时,会出现以下错误: “java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)'”

我的主要活动代码是:

public class CustomMenuActivity extends Activity
{

String ingredientName;
String ingredientQuantity;
private PopupWindow pw;
Button setQuantity;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_menu);

    setQuantity = (Button) findViewById(R.id.btnSetQty);

    setQuantity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            initiatePopupWindow();
        }
    });

}

private void initiatePopupWindow()
{

    try {
        //We need to get the instance of the LayoutInflater, use the context of this activity
        LayoutInflater inflater = (LayoutInflater) CustomMenuActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //Inflate the view from a predefined XML layout
        View layout = inflater.inflate(R.layout.set_quantity_popup,
                (ViewGroup) findViewById(R.id.popupElementid));
        // create a 600px width and 570px height PopupWindow
        pw = new PopupWindow(layout, 600, 570, true);
        // display the popup in the center
        pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

        RadioGroup radioGroup;

        radioGroup = (RadioGroup) findViewById(R.id.radioGroupid);

        final String[] tempQtyVar = new String[1];


        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rbqty30id:
                        tempQtyVar[0] = "30";
                        break;
                    case R.id.rbqty60id:
                        tempQtyVar[0] = "60";
                        break;
                    case R.id.rbqty90id:
                        tempQtyVar[0] = "90";
                        break;
                    case R.id.rbqtycutomid:
                        tempQtyVar[0] = "120";
                        break;
                }
            }
        });

        Button setQtyBtn = (Button) layout.findViewById(R.id.buttonOkSetQtyid);
        setQtyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ingredientQuantity = tempQtyVar[0];
                Toast.makeText(getApplicationContext(),ingredientQuantity,Toast.LENGTH_LONG).show();
                pw.dismiss();
            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }
}


}
我的弹出窗口的layout.xml是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:background="#fd050505"
    android:padding="30dp"
    android:id="@+id/popupElementid">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/relativeLayout">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SET QUANTITY"
            android:textColor="#84e9e6"
            android:textSize="20sp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:textStyle="bold"
            android:id="@+id/popupTitleid"/>
        <LinearLayout
            android:layout_width="600dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/popupTitleid"
            android:orientation="horizontal"
            android:background="@drawable/btn_rounded_boarder"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp">
            <RadioGroup
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/radioGroupid"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="20dp"
                android:orientation="horizontal"
                >
                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text=" 30ml "
                    android:textColor="#84e9e6"
                    android:id="@+id/rbqty30id"
                    android:checked="true" />
                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textColor="#84e9e6"
                    android:text=" 60ml "
                    android:id="@+id/rbqty60id" />
                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textColor="#84e9e6"
                    android:text=" 90ml "
                    android:id="@+id/rbqty90id" />
                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textColor="#84e9e6"
                    android:text=" Custom Value "
                    android:id="@+id/rbqtycutomid"
                    />
            </RadioGroup>
        </LinearLayout>
        <LinearLayout
            android:layout_width="600dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:layout_centerHorizontal="true">
            <SeekBar
                android:id="@+id/customqtySBid"
                android:layout_height="fill_parent"
                android:layout_width="600dp"
                android:textColor="#84e9e6"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginTop="150dp"
                android:paddingBottom="10dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_centerHorizontal="true">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/buttonOkSetQtyid"
                android:textColor="#84e9e6"
                android:text="OK"
                android:background="@drawable/btn_rounded_boarder"
                android:layout_marginTop="275dp"/>
        </LinearLayout>

    </RelativeLayout>
</RelativeLayout>

我试着在网上寻找答案,但并没有真正找到解决我问题的方法。请提出解决方案。
提前谢谢

您似乎在夸大错误的布局。在中更改正确的layout.xml

setContentView(R.layout.activity_custom_menu);

尝试使用radioGroup=(radioGroup)布局;这起作用了。。。非常感谢@Fahid Nadeemi,我很高兴它能帮上忙。活动\自定义\菜单是活动的布局,我必须在这里显示我的弹出窗口。。。我不认为这是错的