2013-03-13 68 views
0

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

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) 
+2

任何'logcat'显示? – 2013-03-13 23:07:49

+0

您是否看到NetworkOnMainThreadException?如果您在HoneyComb或更高版本上运行此操作,您将会... – Sam 2013-03-13 23:11:57

+0

您不应该在主线程中执行HTTP调用,它们会给您带来ANR异常 – 2013-03-13 23:12:31

回答

1

你忘了实例num每当你尝试引用它会抛出的NullPointerException异常:

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

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

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

就是这样!我看起来不够努力,失去了一些东西,我的坏处。谢谢 ! – DRYRA 2013-03-13 23:22:07

+0

LogCat是你最好的工具之一,它告诉你异常和它发生的线:'java.lang.NullPointerException ... at com.example.attijaribank2.Mlo $ 1.onClick(Mlo.java:56)'第56行将会引导你。祝你好运! – Sam 2013-03-13 23:23:59

+0

是的,那跳过了我的想法.. – DRYRA 2013-03-13 23:30:31

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