Android开发崩溃

时间:2012-11-18 18:53:35

标签: android

我使用Eclipse开发应用程序,我使用Android 2.1 API 7,我的应用程序与外部数据库(JSON)交互。当我的模拟器与Android 2.1一起使用时,我没有任何问题,但是当我使用Android 4.0打开我的模拟器时,当我与外部数据库进行交互时,我的应用程序崩溃...

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();
                }
            }
        });
    }
}

你能告诉我修改吗? 谢谢!

3 个答案:

答案 0 :(得分:0)

您正在主UI线程中执行(可能很慢)网络操作。如果您的目标SDK是11(Honeycomb)或更高版本,则会抛出NetworkOnMainThreadException,因为此行为可能会阻止UI并导致应用程序无响应。

您可以使用AsyncTask来解决此问题,方法是在doInBackground(..)中加载数据。

答案 1 :(得分: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());
            }
           }

          }

答案 2 :(得分: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