這是我的代碼:顯示java.lang.NullPointerException在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", "[email protected]"));
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)
我試圖管理radiogroup偵聽器在我的數據庫中發送「戳」。 http後的代碼只是一個例子。我必須改變它。 有人可以幫我嗎? 謝謝
groupRadio =(RadioGroup)findViewById(R.id.group);這一行返回一個空值。試着看看你想要的佈局和ID。 – BlaShadow 2014-10-29 13:23:59