0

我的程序从 android 2.1 到 android 2.3 运行良好

但它不适用于更高版本的android

我在清单文件中使用过

<uses-sdk android:minSdkVersion="7"  
      android:targetSdkVersion="10" 
           android:maxSdkVersion="15"/>

在 android 4.0.3 avd 上运行项目时出现 Logcat 输出错误导致

05-09 12:45:12.051: E/AndroidRuntime(530): 致命异常: main` 05-09 12:45:12.051: E/AndroidRuntime(530): java.lang.RuntimeException: 无法启动活动 ComponentInfo{giv .home/giv.home.Start}: android.os.NetworkOnMainThreadException 05-09 12:45:12.051: E/AndroidRuntime(530): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 05-09 12 :45:12.051: E/AndroidRuntime(530): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 android.app.ActivityThread。访问$600(ActivityThread.java:123) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 05-09 12:45:12.051 : E/AndroidRuntime(530): 在 android.os.Handler.dispatchMessage(Handler.java:99) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 android.os.Looper.loop(Looper.java:137) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 android.app.ActivityThread.main(ActivityThread .java:4424) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-09 12:45:12.051: E/AndroidRuntime(530) : 在 java.lang.reflect.Method.invoke(Method.java:511) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit .java:784) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-09 12:45:12.051: E /AndroidRuntime(530): at dalvik.system.NativeStart.main(Native Method) 05-09 12:45:12.051: E/AndroidRuntime(530): 引起:android.os.NetworkOnMainThreadException 05-09 12:45:12.051 :E/AndroidRuntime(530): 在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 java.net.InetAddress.lookupHostByName(InetAddress .java:391) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 05-09 12:45:12.051: E/AndroidRuntime(530 ): 在 java.net.InetAddress.getAllByName(InetAddress.java:220) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator. java:137) 05-09 12:45:12.051: E/AndroidRuntime(530): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 05-09 12:45:12.051: E/AndroidRuntime(530):在 org.apache.http.impl.conn.AbstractPooledConnAdapter。打开(AbstractPooledConnAdapter.java:119)05-09 12:45:12.051:E/AndroidRuntime(530):在 org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)05-09 12: 45:12.051: E/AndroidRuntime(530): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 org .apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 05-09 12:45:12.051: E/AndroidRuntime(530): at org.apache.http.impl.client.AbstractHttpClient.execute( AbstractHttpClient.java:465) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 giv.home.ConnectionClass.connectToServer(ConnectionClass.java:41) 05-09 12:45:12.051: E/AndroidRuntime( 530): 在 giv.home.Start.onCreate(Start.java:64) 05-09 12:45:12.051: E/AndroidRuntime(530):在 android.app.Activity.performCreate(Activity.java:4465) 05-09 12:45:12.051: E/AndroidRuntime(530): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-09 12 :45:12.051: E/AndroidRuntime(530): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-09 12:45:12.051: E/AndroidRuntime(530): ... 还有 11 个

4

2 回答 2

3

I think this is because you're trying to use HTTPConnect in the main thread... Android no longer supports using HTTP in the main thread and requires that you do it in a background thread. Just guessing, of course... but that's what I suspect is happening.

However, if your target SDK version is 10, then why not just skip setting the max? I mean... what is it about ICS that you feel will be the absolute highest level that your app can run on? the min and target are totally sufficient, I believe. The point of setting a Max is so that (for example) your app won't be installable on ICS now that ICS is available and you know there's something about it that your app doesn't support. So, in that case you'd set 13 and only Honeycomb users (and lower) would be able to use it. Since 15 is the highest version available, setting it is premature... 16 might be just as capable of handling your app as all the rest, for all you know! :)

[EDIT]

You don't even need an AsyncTask (though that would be the more Androidy way to do it). For now just use a regular Thread...

 Handler h = new Handler();
 private Runnable stuffToDoAfterPost = new Runnable(){
       public void run(){
            //whatever you wish to happen after you have done your server call
       }
 };

private class CallServer extends Thread{
   public void run(){
      //make your server call here...


      //then...
      h.post(stuffToDoAfterPost);
   }
}

new CallServer().start(); 
于 2012-05-09T07:32:57.427 回答
0

要解决 StrictMode 问题,您需要在活动中使用以下代码 -

static{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();  
    StrictMode.setThreadPolicy(policy);  
    }
于 2013-11-08T07:59:12.277 回答