我有2个radiobuttons和1个按钮。我希望当我单击普通按钮时,它会检查选中哪个单选按钮并执行基于此的方法。以下是我使用的代码,但是当我点击按钮时应用程序崩溃
@Override
public void onClick(View v) {
radioGroup = (RadioGroup) v.findViewById(R.id.radioPost);
rdpublic=(RadioButton) v.findViewById(R.id.radioPublic);
rdprivate=(RadioButton) v.findViewById(R.id.radioPrivate);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if(rdpublic.isChecked())
{
new PostPublic().execute();
}
else if(rdprivate.isChecked())
{
new PostPrivate().execute;
}
}
});
}

答案 0 :(得分:0)
为什么使用按钮视图查找RadioButtons和Group?您应该使用findViewById。而不是v.findViewById。
答案 1 :(得分:0)
试试这个......
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();
}
}
});
答案 2 :(得分:0)
您的代码假定您的Buttons
或RadioButtons
是传递的Views
v 的孩子View
。这实际上是错误的,这就是你获得NPE的原因。
答案 3 :(得分:0)
这是完整的代码。
package com.project.bsc.radianstores;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class PostEnquiry extends Fragment implements OnClickListener {
public String[] sItemType;
public int position =0;
private EditText Message;
JSONParser jsonParser = new JSONParser();
private ProgressDialog pDialog;
private static final String POST_PRIVATE_URL = "http://192.168.1.102/webservice/privateenquiry.php";
private static final String POST_PUBLIC_URL = "http://192.168.1.102/webservice/publicenquiry.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private Button BtnSave;
private RadioGroup radioGroup;
private RadioButton rdpublic;
private RadioButton rdprivate;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.enquiry, container, false);
BtnSave = (Button) v.findViewById(R.id.sendEnquiry);
Message = (EditText)v.findViewById(R.id.MessageBox);
Spinner spItemType = (Spinner) v.findViewById(R.id.spinnerItemType);
sItemType = getResources().getStringArray(R.array.ItemType);
BtnSave.setOnClickListener(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, sItemType);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spItemType.setAdapter(dataAdapter);
//setting up listener for the spinner
spItemType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
position = index;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
return v;
}
@Override
public void onClick(View v) {
radioGroup = (RadioGroup) v.findViewById(R.id.radioPost);
rdpublic=(RadioButton) v.findViewById(R.id.radioPublic);
rdprivate=(RadioButton) v.findViewById(R.id.radioPrivate);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if(checkedId == rdpublic.getId()) {
new PostPublic().execute();
}
else if (checkedId == rdprivate.getId()) {
//new PostPrivate().execute();
}
}
});
}
class PostPublic extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Posting Enquiry...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String strItemType = sItemType[position];;
String strMessage = Message.getText().toString();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
String strUsername = sp.getString("Username", "");
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username", strUsername));
params.add(new BasicNameValuePair("Item_Type", strItemType));
params.add(new BasicNameValuePair("Message", strMessage));
Log.d("request!", "starting");
//Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(
POST_PUBLIC_URL, "POST", params);
// full json response
Log.d("Enquiry Posting attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Enquiry Posted Successful!", json.toString());
return json.getString(TAG_MESSAGE);
}else{
Log.d("Posting Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null)
Toast.makeText(getActivity(), file_url, Toast.LENGTH_LONG).show();
}
}
}
&#13;