2012-11-18 100 views
-1

我使用Eclipse開發應用程序,我使用Android 2.1 API 7,我的應用程序與外部數據庫(JSON)交互。我沒有任何問題,當我的模擬器是把Android 2.1系統,但是當我把我的仿真器與Android 4.0,我的應用程序崩潰時,我會與外部數據庫交互...Android開發崩潰

logcat的日誌:

11-18 18:44:56.852: D/dalvikvm(601): Not late-enabling CheckJNI (already on) 
11-18 18:44:58.141: D/gralloc_goldfish(601): Emulator without GPU emulation detected. 
11-18 18:44:58.301: W/TextLayoutCache(601): computeValuesWithHarfbuzz -- need to force to single run 
11-18 18:44:58.301: W/TextLayoutCache(601): computeValuesWithHarfbuzz -- need to force to single run 
11-18 18:45:06.601: D/AndroidRuntime(601): Shutting down VM 
11-18 18:45:06.601: W/dalvikvm(601): threadid=1: thread exiting with uncaught exception (group=0x409961f8) 
11-18 18:45:06.631: E/AndroidRuntime(601): FATAL EXCEPTION: main 
11-18 18:45:06.631: E/AndroidRuntime(601): android.os.NetworkOnMainThreadException 
11-18 18:45:06.631: E/AndroidRuntime(601): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084) 
11-18 18:45:06.631: E/AndroidRuntime(601): at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 
11-18 18:45:06.631: E/AndroidRuntime(601): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 
11-18 18:45:06.631: E/AndroidRuntime(601): at java.net.InetAddress.getAllByName(InetAddress.java:220) 
11-18 18:45:06.631: E/AndroidRuntime(601): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 
11-18 18:45:06.631: E/AndroidRuntime(601): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
11-18 18:45:06.631: E/AndroidRuntime(601): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
11-18 18:45:06.631: E/AndroidRuntime(601): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 
11-18 18:45:06.631: E/AndroidRuntime(601): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
11-18 18:45:06.631: E/AndroidRuntime(601): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
11-18 18:45:06.631: E/AndroidRuntime(601): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
11-18 18:45:06.631: E/AndroidRuntime(601): at com.example.library.JSONParser.getJSONFromUrl(JSONParser.java:42) 
11-18 18:45:06.631: E/AndroidRuntime(601): at com.example.library.UserFunctions.loginUser(UserFunctions.java:32) 
11-18 18:45:06.631: E/AndroidRuntime(601): at com.example.lagrandechasse.LoginActivity$1.onClick(LoginActivity.java:54) 
11-18 18:45:06.631: E/AndroidRuntime(601): at android.view.View.performClick(View.java:3480) 
11-18 18:45:06.631: E/AndroidRuntime(601): at android.view.View$PerformClick.run(View.java:13983) 
11-18 18:45:06.631: E/AndroidRuntime(601): at android.os.Handler.handleCallback(Handler.java:605) 
11-18 18:45:06.631: E/AndroidRuntime(601): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-18 18:45:06.631: E/AndroidRuntime(601): at android.os.Looper.loop(Looper.java:137) 
11-18 18:45:06.631: E/AndroidRuntime(601): at android.app.ActivityThread.main(ActivityThread.java:4340) 
11-18 18:45:06.631: E/AndroidRuntime(601): at java.lang.reflect.Method.invokeNative(Native Method) 
11-18 18:45:06.631: E/AndroidRuntime(601): at java.lang.reflect.Method.invoke(Method.java:511) 
11-18 18:45:06.631: E/AndroidRuntime(601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
11-18 18:45:06.631: E/AndroidRuntime(601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
11-18 18:45:06.631: E/AndroidRuntime(601): at dalvik.system.NativeStart.main(Native Method) 
11-18 18:45:08.711: I/Process(601): Sending signal. PID: 601 SIG: 9 

你能幫我嗎?

感謝您的回覆,這是我的登陸類:

public class LoginActivity extends Activity { 

    //My variable 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 

     //initialize button and textwiew 

     btnLogin.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 

       UserFunctions userFunction = new UserFunctions(); 
       JSONObject json = userFunction.loginUser(login,pass); 

       try { 
        if (json.getString(success) != null) { 
         loginErrorMsg.setText(""); 
         String res = json.getString(success); 
         if(Integer.parseInt(res) == 1){ 

          DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 
          JSONObject json_user = json.getJSONObject("user"); 

          userFunction.logoutUser(getApplicationContext()); 
          db.addUser(json_user.getString(ID), json_user.getString(login));       

          Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class); 

          dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(dashboard); 

          finish(); 
         } 
         else{ 
          loginErrorMsg.setText("Error"); 
         } 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
} 

你能告訴我的修改? 謝謝!

回答

0

您正在主UI線程中執行(可能較慢)的網絡操作。如果您的目標SDK爲11(Honeycomb)或更高,則會拋出NetworkOnMainThreadException,因爲此行爲可能會阻止UI並導致應用程序無響應。

您可以使用AsyncTask來解決此問題,方法是將數據加載到doInBackground(..)中。

+0

@John請看http://developer.android.com/training/basics/network-ops/connecting.html瞭解詳情 –

0

使用這種方式..

public class getConnection extends AsyncTask<String, Void, Void> 
    { 


      @Override 
      protected void onPreExecute() 
      { 
       super.onPreExecute(); 

      } 

      @Override 
      protected Void doInBackground(String... arg0) 
      { 

      try 
      { 
       //Your JSON Code 
      } 
      catch (Exception e) 
      { 
      e.printStackTrace(); 
      } 

      return null; 
      } 

      @Override 
      protected void onPostExecute(Void result) 
      { 
       super.onPostExecute(result); 


      } 
      catch (Exception e) 
      { 
      Log.v("log", e.toString()); 
      } 
      } 

      } 
+0

但是我的課程擴展了Activity? –

+0

這是通過這種方式在活動類中調用的... new getConnection()。execute(); –

0

你不能在主線程上做網絡業務,這是該日誌跟蹤告訴你的。

11-18 18:45:06.631: E/AndroidRuntime(601): FATAL EXCEPTION: main 11-18 18:45:06.631: E/AndroidRuntime(601): android.os.NetworkOnMainThreadException

永遠做一個單獨的線程聯網。您可以用不同的方式完成此操作,可以使用asyncTask或自己開始新線程。有很多關於SO的文章以及網絡上的很多例子。 Take a look at this SO post