使用android单选按钮执行方法

使用android单选按钮执行方法,android,methods,radio-button,Android,Methods,Radio Button,我有两个单选按钮和一个按钮。我希望当我点击普通按钮时,它会检查选中了哪个单选按钮,并基于此执行一个方法。以下是我使用过的代码,但当我单击按钮时应用程序崩溃 @覆盖 公共void onClick(视图v){ 放射组=(放射组)v.findViewById(R.id.radioPost); rdpublic=(RadioButton)v.findViewById(R.id.radioPublic); rdprivate=(RadioButton)v.findViewById(R.id.radioP

我有两个单选按钮和一个按钮。我希望当我点击普通按钮时,它会检查选中了哪个单选按钮,并基于此执行一个方法。以下是我使用过的代码,但当我单击按钮时应用程序崩溃

@覆盖
公共void onClick(视图v){
放射组=(放射组)v.findViewById(R.id.radioPost);
rdpublic=(RadioButton)v.findViewById(R.id.radioPublic);
rdprivate=(RadioButton)v.findViewById(R.id.radioPrivate);
radioGroup.setOnCheckedChangeListener(新的OnCheckedChangeListener()
{
检查更改后的公共无效(RadioGroup组,int checkedId)
{
if(rdpublic.isChecked())
{
新的PostPublic().execute();
}
else if(rdprivate.isChecked())
{
新建PostPrivate()。执行;
}
}
});

}
为什么要使用按钮视图查找单选按钮和组?您不应该使用v.findviewbyd,而应该使用findviewbyd。

试试这个

    normalBtn= (Button) findViewById(R.id.Btn);
    rdpublic=(RadioButton) findViewById(R.id.radioPublic);
    rdprivate=(RadioButton) findViewById(R.id.radioPrivate);

    normalBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            try {
      if(rdpublic.isChecked())
            {
                new PostPublic().execute();
            }
            else if(rdprivate.isChecked())
            {
                new PostPrivate().execute;
            }

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

您的代码假定您的
按钮
单选按钮
是传递的
视图
的子
视图。这实际上是错误的,这就是为什么你得到了NPE。

这是完整的代码

package com.project.bsc.com;
导入android.widget.RadioGroup.OnCheckedChangeListener;
导入android.app.ProgressDialog;
导入android.content.SharedReferences;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.preference.PreferenceManager;
导入android.support.annotation.Nullable;
导入android.support.v4.app.Fragment;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.RadioButton;
导入android.widget.RadioGroup;
导入android.widget.Spinner;
导入android.widget.Toast;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.List;
公共类PostQuery扩展了片段实现OnClickListener{
公共字符串[]sItemType;
公共int位置=0;
私人短信;
JSONParser JSONParser=新的JSONParser();
私人对话;
private静态最终字符串POST_private_URL=”http://192.168.1.102/webservice/privateenquiry.php";
私有静态最终字符串POST\U PUBLIC\U URL=”http://192.168.1.102/webservice/publicenquiry.php";
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
私人按钮BtnSave;
私人放射组放射组;
私人电台按钮和公共电台按钮;
私用无线按钮;
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图v=充气机。充气(右布局。查询,容器,假);
BtnSave=(按钮)v.findViewById(R.id.sendInquiry);
Message=(EditText)v.findViewById(R.id.MessageBox);
微调器spItemType=(微调器)v.findViewById(R.id.SpinerItemType);
sItemType=getResources().getStringArray(R.array.ItemType);
BtnSave.setOnClickListener(这个);
ArrayAdapter dataAdapter=新的ArrayAdapter(getActivity(),android.R.layout.simple\u微调器\u项,sItemType);
dataAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉项);
setAdapter(dataAdapter);
//为微调器设置侦听器
spItemType.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、内部arg2、长arg3){
int index=arg0.getSelectedItemPosition();
位置=索引;
}
@凌驾
未选择公共无效(AdapterView arg0){
}
});
返回v;
}
@凌驾
公共void onClick(视图v){
放射组=(放射组)v.findViewById(R.id.radioPost);
rdpublic=(RadioButton)v.findViewById(R.id.radioPublic);
rdprivate=(RadioButton)v.findViewById(R.id.radioPrivate);
radioGroup.setOnCheckedChangeListener(新的OnCheckedChangeListener()
{
@凌驾
检查更改后的公共无效(RadioGroup组,int checkedId)
{
if(checkedId==rdpublic.getId()){
新的PostPublic().execute();
}
else if(checkedId==rdprivate.getId()){
//新的PostPrivate().execute();
}
}
});
}
类PostPublic扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(getActivity());
pDialog.setMessage(“发布查询…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…args){
//TODO自动生成的方法存根
//检查成功标签
成功;
String strItemType=sItemType[position];;
字符串strMessage=Message.getText().toString();
SharedReferences sp=PreferenceManager.GetDefaultSharedReferences(getActivity());
String strUsername=sp.getString(“用户名”,“用户名”);
试一试{
//建筑参数
List params=new ArrayList()