[英]Java.lang.nullpointerexception on a radioGroup
這是我的代碼:
public class Survey extends Activity {
PopupWindow popupWindow;
RadioButton badChoise, mediaChoise, happyChoise;
RadioGroup groupRadio;
Button launchpopup;
Boolean flagBad = false;
Boolean flagMedia=false;
Boolean flagHappy=false;
int checkedButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.survey);
launchpopup = (Button) findViewById(R.id.popup);
launchpopup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showPopup();
}
});
}
public void showPopup(){
LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
badChoise =(RadioButton) findViewById(R.id.badChoise);
mediaChoise =(RadioButton) findViewById(R.id.mediaChoise);
happyChoise =(RadioButton) findViewById(R.id.happyChoise);
groupRadio = (RadioGroup) findViewById(R.id.group);
Button btnDismiss = (Button)popupView.findViewById(R.id.chiudi);
Button btnInvia = (Button)popupView.findViewById(R.id.invia);
btnDismiss.setOnClickListener(new myListener());
btnInvia.setOnClickListener(new myListener());
groupRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
// This will get the radiobutton that has changed in its check state
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
// This puts the value (true/false) into the variable
boolean isChecked = checkedRadioButton.isChecked();
// If the radiobutton that has changed in check state is now checked...
if (isChecked)
{
checkedButton = checkedId;
}
}
});
//mediaChoise.setOnClickListener(new myListener());
//happyChoise.setOnClickListener(new myListener());
popupWindow.showAsDropDown(launchpopup, 50, -30);
}
public class myListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// bottone chiudi
if( v.getId()== R.id.chiudi){
popupWindow.dismiss();
}
// bottone invia
if( v.getId()== R.id.invia){
/*
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/mypage.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("fname", "vinod"));
nameValuePairs.add(new BasicNameValuePair("fphone", "1234567890"));
nameValuePairs.add(new BasicNameValuePair("femail", "abc@gmail.com"));
nameValuePairs.add(new BasicNameValuePair("fcomment", "Help"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}*/
popupWindow.dismiss();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我在這一行上有一個Java.lang.nullpointerexception:
groupRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
這是錯誤日志:
10-29 14:11:51.859: E/AndroidRuntime(21090): FATAL EXCEPTION: main
10-29 14:11:51.859: E/AndroidRuntime(21090): java.lang.NullPointerException
10-29 14:11:51.859: E/AndroidRuntime(21090): at com.example.survey.Survey.showPopup(Survey.java:96)
10-29 14:11:51.859: E/AndroidRuntime(21090): at com.example.survey.Survey$1.onClick(Survey.java:65)
10-29 14:11:51.859: E/AndroidRuntime(21090): at android.view.View.performClick(View.java:4377)
10-29 14:11:51.859: E/AndroidRuntime(21090): at android.view.View$PerformClick.run(View.java:18044)
10-29 14:11:51.859: E/AndroidRuntime(21090): at android.os.Handler.handleCallback(Handler.java:725)
10-29 14:11:51.859: E/AndroidRuntime(21090): at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 14:11:51.859: E/AndroidRuntime(21090): at android.os.Looper.loop(Looper.java:137)
10-29 14:11:51.859: E/AndroidRuntime(21090): at android.app.ActivityThread.main(ActivityThread.java:5306)
10-29 14:11:51.859: E/AndroidRuntime(21090): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 14:11:51.859: E/AndroidRuntime(21090): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 14:11:51.859: E/AndroidRuntime(21090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-29 14:11:51.859: E/AndroidRuntime(21090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-29 14:11:51.859: E/AndroidRuntime(21090): at dalvik.system.NativeStart.main(Native Method)
我正在嘗試管理無線電組偵聽器以在數據庫中發送“戳”。 http帖子的代碼只是一個例子。 我必須改變它。 有人能幫我嗎? 謝謝
您需要從剛剛膨脹的母親視圖(即popupView
找到所需的視圖。
因此,更改以下內容(和類似內容):
badChoise = (RadioButton) findViewById(R.id.badChoise);
對此:
badChoise = (RadioButton) popupView.findViewById(R.id.badChoise);
由於此行,您正在獲取NullPointerException
groupRadio = (RadioGroup) findViewById(R.id.group);
groupRadio是空對象,因此可以在其上進行操作:
groupRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
會拋出異常。 查看調查布局,檢查是否正確定義了無線電組ID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.