检索警报对话框中的选定单选按钮

时间:2018-09-22 11:49:55

标签: java android android-alertdialog

目标:
检索alertdialog中选中的单选按钮的selectedId。

问题:
您无法通过在SpinnerDialogItemSelected方法中使用findViewByid来检索值,因为我收到以下错误消息:

  

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

如何检索方法SpinnerDialogItemSelected中可用作参数的值?

信息:
*我是Android新手
*我正在使用android studio

谢谢!


activity_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>

form_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>

MainActivity.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 个答案:

答案 0 :(得分:0)

能否请您尝试将以编程方式放大的视图传递给:

 private void SpinnerDialogItemSelected(String value, View formElementsView)
 { 
    RadioGroup myRadioGroup  = (RadioGroup) formElementsView.findViewById(R.id.rgtest1);
    int selectedId = myRadioGroup.getCheckedRadioButtonId();
 }

答案 1 :(得分:0)

activity_main.xml中您没有@id/rgtest1 RadioGroup,这就是findById返回null的原因。创建formElementsView之后,您应该在其中找到RadioGroup并将其存储并稍后再使用。

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

您创建的AlertDialog的View层次结构不会像您假设的那样被附加到View的{​​{1}}层次结构中。那不是Android的工作方式。

答案 2 :(得分:0)

我认为您应该实现radiogroup的OnCheckedChangeListener()。然后可以在OnCheckedChangeListener()的onClick()方法中进行保存。(以类似SharedPreferences之类的方式保存)并重复使用。

尝试以下代码段:

#profileController
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ProfileController extends Controller
{
    public function index($slug){
        return view('profile.index')->with('data', Auth::user()->profile);
    }

    public function uploadPhoto(Request $request) {
        $file = $request->file('pic');
        $filename = $file->getClientOriginalName();
        $path = 'storage/img';

        $file->move($path, $filename);
        $user_id = Auth::user()->id;

        DB::table('users')->where('id',$user_id)->update(['pic' =>$filename]);

        return redirect('/editProfile')->withSuccess('Your image was successful.');
    }
}

答案 3 :(得分:0)

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