0

我创建了一个活动,在选择单选按钮的情况下,将信息发送到数据库。一切正常,没有错误,但是当我编译并单击按钮时,应用程序崩溃了。找不到问题。这是我的代码

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




        }

    });

  }
}   

日志猫

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)
4

2 回答 2

1

num每当您尝试引用它时,您忘记实例化抛出 NullPointerException :

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

onCreate()(或您尝试访问之前的任何时间num)您需要使用:

num = (EditText) findViewById(R.id.xxx);
于 2013-03-13T23:18:01.760 回答
0

您不应该在主线程上进行任何网络通信。您必须实现异步调用。要实现这样的异步调用,您必须扩展 AsyncTask 类。也许你可以从这样的事情开始:

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

然后你可以像这样修改你的 onClick 监听器:

// 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());
            } 
        }
    }
}
于 2013-03-13T23:40:43.110 回答