我试图通过webservice连接到mysql数据库(所以a。php中的特定url)并使用AsyncTask。
运行应用程序时出错并关闭,告诉我"不幸的是发生了错误"
没有日志读得很好,因此我请求帮助,Aque也开始在android中编程。
感谢您的帮助。
MainActivity.java
package com.example.pruebaenviarvariables;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.example.pruebaenviarvariables.R;
public class MainActivity extends Activity {
private EditText dni;
private EditText nombre;
private EditText telefono;
private EditText email;
private Button insertar;
private Button mostrar;
private ImageButton mas;
private ImageButton menos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
setContentView(R.layout.activity_main);
EditText dni=(EditText)findViewById(R.id.dni);
EditText nombre=(EditText)findViewById(R.id.nombre);
EditText telefono=(EditText)findViewById(R.id.telefono);
EditText email=(EditText)findViewById(R.id.email);
//Insertamos los datos de la persona.
Button insertar=(Button)findViewById(R.id.insertar);
insertar.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Insertar(MainActivity.this).execute();
}
});
}
//Inserta los datos de las Personas en el servidor.
private boolean insertar(){
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
HttpPost httppost;
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://192.168.0.11/pruebaAndroid/insert.php"); // Url del Servidor
//Añadimos nuestros datos
nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("dni",dni.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("nombre",nombre.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("telefono",telefono.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("email",email.getText().toString().trim()));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
return true;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//AsyncTask para insertar Personas
class Insertar extends AsyncTask<String,String,String>{
private Activity context;
Insertar(Activity context){
this.context=context;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
if(insertar())
context.runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(context, "Persona insertada con éxito", Toast.LENGTH_LONG).show();
nombre.setText("");
dni.setText("");
telefono.setText("");
email.setText("");
}
});
else
context.runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(context, "Persona no insertada con éxito", Toast.LENGTH_LONG).show();
}
});
return null;
}
}
}
fragment_main.xml的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@android:color/white" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nombre" />
<EditText
android:id="@+id/nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dni" />
<EditText
android:id="@+id/dni"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tlf" />
<EditText
android:id="@+id/telefono"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="email" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/insertar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="insertar" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
</LinearLayout>
</LinearLayout>
最后,日志:
6 21:20:43.371: D/AndroidRuntime(1163): Shutting down VM
07-06 21:20:43.371: W/dalvikvm(1163): threadid=1: thread exiting with uncaught exception (group=0xb3a5bba8)
07-06 21:20:43.391: E/AndroidRuntime(1163): FATAL EXCEPTION: main
07-06 21:20:43.391: E/AndroidRuntime(1163): Process: com.example.pruebaenviarvariables, PID: 1163
07-06 21:20:43.391: E/AndroidRuntime(1163): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pruebaenviarvariables/com.example.pruebaenviarvariables.MainActivity}: java.lang.NullPointerException
07-06 21:20:43.391: E/AndroidRuntime(1163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-06 21:20:43.391: E/AndroidRuntime(1163): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-06 21:20:43.391: E/AndroidRuntime(1163): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-06 21:20:43.391: E/AndroidRuntime(1163): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-06 21:20:43.391: E/AndroidRuntime(1163): at android.os.Handler.dispatchMessage(Handler.java:102)
07-06 21:20:43.391: E/AndroidRuntime(1163): at android.os.Looper.loop(Looper.java:136)
07-06 21:20:43.391: E/AndroidRuntime(1163): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-06 21:20:43.391: E/AndroidRuntime(1163): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 21:20:43.391: E/AndroidRuntime(1163): at java.lang.reflect.Method.invoke(Method.java:515)
07-06 21:20:43.391: E/AndroidRuntime(1163): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-06 21:20:43.391: E/AndroidRuntime(1163): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-06 21:20:43.391: E/AndroidRuntime(1163): at dalvik.system.NativeStart.main(Native Method)
07-06 21:20:43.391: E/AndroidRuntime(1163): Caused by: java.lang.NullPointerException
07-06 21:20:43.391: E/AndroidRuntime(1163): at com.example.pruebaenviarvariables.MainActivity.onCreate(MainActivity.java:51)
07-06 21:20:43.391: E/AndroidRuntime(1163): at android.app.Activity.performCreate(Activity.java:5231)
07-06 21:20:43.391: E/AndroidRuntime(1163): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-06 21:20:43.391: E/AndroidRuntime(1163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-06 21:20:43.391: E/AndroidRuntime(1163): ... 11 more
07-06 21:20:46.851: I/Process(1163): Sending signal. PID: 1163 SIG: 9
答案 0 :(得分:0)
在类级别定义了所有布局变量(EditText,Button等)。在onCreate()方法中,您没有使用这些对象并重新创建它。因此,您在onCreate()中创建和设置对象不适用于UI线程。 UI线程只访问类级变量。
解决方案:不要在oncreate()中重新定义相同的布局变量,在此方法中使用类级别对象。
MainActivity代码:
private EditText dni;
private EditText nombre;
private EditText telefono;
private EditText email;
private Button insertar;
private Button mostrar;
private ImageButton mas;
private ImageButton menos;
private EditText dni;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
setContentView(R.layout.activity_main);
dni=(EditText)findViewById(R.id.dni);
nombre=(EditText)findViewById(R.id.nombre);
telefono=(EditText)findViewById(R.id.telefono);
email=(EditText)findViewById(R.id.email);
//Insertamos los datos de la persona.
insertar=(Button)findViewById(R.id.insertar);
//其他代码.......
xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@android:color/white" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nombre" />
<EditText
android:id="@+id/nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dni" />
<EditText
android:id="@+id/dni"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tlf" />
<EditText
android:id="@+id/telefono"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="email" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/insertar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="insertar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
</LinearLayout>
</LinearLayout>