我创建了一个活动,在选择了radiobutton的情况下,将信息发送到数据库。 一切都是正确的,没有错误,但是当我编译并单击按钮时应用程序崩溃。找不到问题。这是我的代码
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)
答案 0 :(得分:1)
您忘记实例化num
,只要您尝试引用它,就会抛出NullPointerException:
final String nb = num.getText().toString();
在onCreate()
(或尝试访问num
之前的任何时间),您需要使用:
num = (EditText) findViewById(R.id.xxx);
答案 1 :(得分: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());
}
}
}
}