[英]Retrieving the selected radio button in an alertdialog
目標:
檢索警報對話框中選中的單選按鈕的 selectedId。
問題:
您無法通過在 SpinnerDialogItemSelected 方法中使用 findViewByid 來檢索值,因為我收到一條錯誤消息:
java.lang.NullPointerException:嘗試在空對象引用上調用虛擬方法“int android.widget.RadioGroup.getCheckedRadioButtonId()”
如何檢索可用作 SpinnerDialogItemSelected 方法中的參數的值?
信息:
*我是 Android 新手
*我正在使用安卓工作室
謝謝!
活動_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>
表單元素.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
您能否嘗試將您以編程方式膨脹的視圖傳遞給:
private void SpinnerDialogItemSelected(String value, View formElementsView)
{
RadioGroup myRadioGroup = (RadioGroup) formElementsView.findViewById(R.id.rgtest1);
int selectedId = myRadioGroup.getCheckedRadioButtonId();
}
在activity_main.xml
您沒有@id/rgtest1
RadioGroup
這就是findById 返回null 的原因。 創建formElementsView
您應該在其中找到 RadioGroup 並將其存儲並稍后重用。
radioGroup = (RadioGroup) formElementsView.findViewById(R.id.rgtest1);
該View
的創建的AlertDialog的層次結構不會被連接到View
“中的s系列Activity
,因為它看起來像你的假設。 這不是Android的工作方式。
我認為你應該實現 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);
}
});
RadioGroup myRadioGroup = (RadioGroup) formElementsView.findViewById(R.id.rgtest1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.