radioGroup上的Java.lang.nullpointerexception

时间:2014-10-29 13:21:50

标签: android radio-group

这是我的代码:

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)

我正在尝试管理radiogroup侦听器以在我的数据库中发送“戳”。 http发布的代码只是一个例子。我必须改变它。 有人能帮我吗? 感谢

2 个答案:

答案 0 :(得分:3)

您需要从新增的母亲视图中找到所需的观看结果,即popupView

所以,改变以下(和类似的):

badChoise = (RadioButton) findViewById(R.id.badChoise);

对此:

badChoise = (RadioButton) popupView.findViewById(R.id.badChoise);

答案 1 :(得分:1)

由于此行

,您将收到NullPointerException
groupRadio = (RadioGroup)  findViewById(R.id.group);

groupRadio是null对象,因此对其进行anthing,例如:

groupRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

会抛出异常。查看您的调查布局并检查您是否正确定义了无线电组ID