主要活动中的Android错误“无法启动活动”

时间:2013-06-17 19:17:21

标签: java php android

我正在关注第一个答案on this question,但它不起作用!我不知道为什么;如果有人可以帮助我,我们将不胜感激。

这是我的代码:

package com.example.enviarphp;

//All the import is here

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://appandroid.comuv.com/index.php");

    try {
        // Add your data
        Log.i( "Anda el try", "anda el try");
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("dato", "Probandooo"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        Log.i( "se esta por ejecutar", "se esta por ejecutar");
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        Log.i( "se ejecutar", "se ejecutar");
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        throw new AssertionError("IO Error");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        throw new AssertionError("Client Protocol Error");

    }
}

}

这是我的PHP文件:

<?php

$conexion = mysql_connect("base","name","password") or die ("Error al conectar a la base de datos.");

 mysql_select_db("name2",$conexion);

 $dato = $_POST['dato'];

 $sql="INSERT INTO Mensajes(id,mensaje) VALUES(null,$dato)";

 mysql_query($sql);

 ?>

LogCat错误:

06-17 16:09:24.779: I/Anda el try(9916): anda el try
06-17 16:09:24.829: I/se esta por ejecutar(9916): se esta por ejecutar
06-17 16:09:24.919: D/AndroidRuntime(9916): Shutting down VM
06-17 16:09:24.919: W/dalvikvm(9916): threadid=1: thread exiting with uncaught exception (group=0x40a4b1f8)
06-17 16:09:24.999: E/AndroidRuntime(9916): FATAL EXCEPTION: main
06-17 16:09:24.999: E/AndroidRuntime(9916): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.enviarphp/com.example.enviarphp.MainActivity}: android.os.NetworkOnMainThreadException
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread.access$600(ActivityThread.java:132)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.os.Looper.loop(Looper.java:137)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread.main(ActivityThread.java:4575)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at java.lang.reflect.Method.invokeNative(Native Method)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at java.lang.reflect.Method.invoke(Method.java:511)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at dalvik.system.NativeStart.main(Native Method)
06-17 16:09:24.999: E/AndroidRuntime(9916): Caused by: android.os.NetworkOnMainThreadException
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at com.example.enviarphp.MainActivity.onCreate(MainActivity.java:39)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.Activity.performCreate(Activity.java:4465)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-17 16:09:24.999: E/AndroidRuntime(9916):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
06-17 16:09:24.999: E/AndroidRuntime(9916):     ... 11 more

非常感谢!我希望有人可以帮助我解决我的问题,我已经尝试了所有我知道的事情。

1 个答案:

答案 0 :(得分:1)

  

引起:android.os.NetworkOnMainThreadException

您正在主应用程序线程上执行网络I / O.您需要将其移动到后台线程,可能由AsyncTask管理。