Matt
Matt

Reputation: 49

Java.lang.nullpointerexception on a radioGroup

this is my code :

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);
}
}

i have an Java.lang.nullpointerexception on this line :

groupRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

this is the error log:

 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)

I'm trying to manage the radiogroup listener to send a "poke" in my database. The code for the http post it's only an example. I have to change it. Can someone help me? Thanks

Upvotes: 1

Views: 1946

Answers (2)

Karol Żygłowicz
Karol Żygłowicz

Reputation: 2452

You are getting NullPointerException because of this line

groupRadio = (RadioGroup)  findViewById(R.id.group);

groupRadio is null object so doing anthing on it for example:

groupRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

will throw an exception. Look in your survey layout and check if you define radio group id properly

Upvotes: 1

waqaslam
waqaslam

Reputation: 68187

You need to find your desired views from the newly inflated mother-view i.e. popupView.

So, change the following (and similar):

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

To this:

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

Upvotes: 3

Related Questions