DRYRA
DRYRA

Reputation: 47

Radio Buttons Android

I created an activity that , case of the radiobutton selected , sends an information to the database . Everything is right , no error , but when i compile and click the button the application crashes . can't find the problem . This is my code

public class Mlo extends Activity {
private RadioButton vincinq, cinq;
private EditText num;
private RadioGroup tupe;


HttpPost httppost;
StringBuffer buffer;
HttpClient httpclient ;
private void createDialog(String title, String text)
{
    // Création d'une popup affichant un message
    AlertDialog ad = new AlertDialog.Builder(this)
            .setPositiveButton("Ok", null).setTitle(title).setMessage(text)
            .create();
    ad.show();

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mlo);
    vincinq = (RadioButton) findViewById(R.id.conv1);
    cinq = (RadioButton) findViewById(R.id.conv2);
    Button button1 = (Button) findViewById(R.id.button1);
    tupe = (RadioGroup) findViewById(R.id.radioGroup1);
    // Définition du listener du bouton
    button1.setOnClickListener(new View.OnClickListener()
    {

        public void onClick(View v) {
            final String nb = num.getText().toString();


 if (vincinq.isChecked()) {
    final String type ="25";
        final String nbnum = num.getText().toString();
        try {
            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://forma-fun.comli.com/cheq.php");
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
         postParameters.add(new BasicNameValuePair("numcompte", nbnum));
         postParameters.add(new BasicNameValuePair("type", type));

         httppost.setEntity(new UrlEncodedFormEntity(postParameters));                   
         HttpResponse response = httpclient.execute(httppost);
         Log.i("postData", response.getStatusLine().toString());

         createDialog("Merci ", "Votre demande a été effectuée");
         Intent i = new Intent(Mlo.this,VotreCompte.class);
         startActivity(i);
        }
             catch(Exception e)
             {
                 Log.e("log_tag", "Error:  "+e.toString());
             } 
 }
 else 
     if (cinq.isChecked()){ 
         final String type1 ="50";
            final String nbnum = num.getText().toString();
            try {
                httpclient = new DefaultHttpClient();
                httppost = new HttpPost("http://forma-fun.comli.com/cheq.php");
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
             postParameters.add(new BasicNameValuePair("numcompte", nbnum));
             postParameters.add(new BasicNameValuePair("type", type1));

             httppost.setEntity(new UrlEncodedFormEntity(postParameters));                   
             HttpResponse response = httpclient.execute(httppost);
             Log.i("postData", response.getStatusLine().toString());

             createDialog("Merci ", "Votre demande a été effectuée");
             Intent i = new Intent(Mlo.this,VotreCompte.class);
             startActivity(i);
            }
                 catch(Exception e)
                 {
                     Log.e("log_tag", "Error:  "+e.toString());
                 }
 }




        }

    });

  }
}   

Logcat

03-13 12:13:05.282: E/AndroidRuntime(363): FATAL EXCEPTION: main
03-13 12:13:05.282: E/AndroidRuntime(363): java.lang.NullPointerException
03-13 12:13:05.282: E/AndroidRuntime(363):  at    

com.example.attijaribank2.Mlo$1.onClick(Mlo.java:56)
03-13 12:13:05.282: E/AndroidRuntime(363):  at   
android.view.View.performClick(View.java:2485)
03-13 12:13:05.282: E/AndroidRuntime(363):  at   

android.view.View$PerformClick.run(View.java:9080)
03-13 12:13:05.282: E/AndroidRuntime(363):  at   

android.os.Handler.handleCallback(Handler.java:587)
03-13 12:13:05.282: E/AndroidRuntime(363):  at   
android.os.Handler.dispatchMessage(Handler.java:92)
03-13 12:13:05.282: E/AndroidRuntime(363):  at android.os.Looper.loop(Looper.java:130)

03-13 12:13:05.282: E/AndroidRuntime(363):  at    
android.app.ActivityThread.main(ActivityThread.java:3683)
03-13 12:13:05.282: E/AndroidRuntime(363):  at   
java.lang.reflect.Method.invokeNative(Native Method)
03-13 12:13:05.282: E/AndroidRuntime(363):  at  
java.lang.reflect.Method.invoke(Method.java:507)
03-13 12:13:05.282: E/AndroidRuntime(363):  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-13 12:13:05.282: E/AndroidRuntime(363):  at  
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-13 12:13:05.282: E/AndroidRuntime(363):  at dalvik.system.NativeStart.main(Native  
Method)

Upvotes: 0

Views: 366

Answers (2)

Csaba Szugyiczki
Csaba Szugyiczki

Reputation: 456

You should not have any network communication done on the main thread. You have to implement asyncronous calls. To implement such asyncronous calls you must extend the AsyncTask class. Maybe you can start with something like this:

private class HttpTask extends AsyncTask<String, Void, String> {
    private ArrayList<NameValuePair> params;

    public void setParams(ArrayList<NameValuePair> params){
        this.params = params;
    }
    protected String doInBackground(String... urls) {
        int count = requestStrings.length;
        String result = "";
        for (String url : urls) {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://forma-fun.comli.com/cheq.php");
            httppost.setEntity(new UrlEncodedFormEntity(params));                   
            HttpResponse response = httpclient.execute(httppost);

            Log.i("postData", response.getStatusLine().toString());

            InputStream content = response.getEntity().getContent();
            BufferedReader buffer = new BufferedReader(
                new InputStreamReader(content));
            String s = "";
            while ((s = buffer.readLine()) != null) {
                result += s;
            }
        }
        return result;
    }

    protected void onProgressUpdate(Integer... progress) {
// TODO You are on the GUI thread, and the first element in 
// the progress parameter contains the last progress
// published from doInBackground, so update your GUI
    }

    protected void onPostExecute(int result) {
// Processing is complete, result contains the result string 
    }
}

And then you can modify your onClick listener like this:

// Définition du listener du bouton
button1.setOnClickListener(new View.OnClickListener()
{

    public void onClick(View v) {
        final String nb = num.getText().toString();


        if (vincinq.isChecked()) {
            final String type ="25";
            final String nbnum = num.getText().toString();
            try {
                HttpTask task = new HttpTask();
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("numcompte", nbnum));
                postParameters.add(new BasicNameValuePair("type", type1));
                task.setParams(postParameters);
                task.execute("http://forma-fun.comli.com/cheq.php");

                createDialog("Merci ", "Votre demande a été effectuée");
                Intent i = new Intent(Mlo.this,VotreCompte.class);
                startActivity(i);
            }
            catch(Exception e)
            {
                Log.e("log_tag", "Error:  "+e.toString());
            } 
        }
    }
}

Upvotes: 0

Sam
Sam

Reputation: 86948

You forgot to instantiate num which throws the NullPointerException whenever you try to reference it:

final String nb = num.getText().toString();

In onCreate() (or any time before you try to access num) you need to use:

num = (EditText) findViewById(R.id.xxx);

Upvotes: 1

Related Questions