0

我正在构建一个由用户登录和注册组成的应用程序,但每次我在模拟器上测试它时都会收到强制关闭。以下是我在日志猫中收到的错误:

08-14 14:06:28.853: D/dalvikvm(828): GC_FOR_ALLOC freed 108K, 3% free 8262K/8455K, paused 89ms, total 92ms
08-14 14:06:29.273: I/Choreographer(828): Skipped 72 frames!  The application may be doing too much work on its main thread.
08-14 14:06:29.373: D/gralloc_goldfish(828): Emulator without GPU emulation detected.
08-14 14:06:29.373: D/dalvikvm(828): GC_CONCURRENT freed 5K, 3% free 8660K/8839K, paused 110ms+28ms, total 365ms
08-14 14:06:29.902: I/Choreographer(828): Skipped 85 frames!  The application may be doing too much work on its main thread.
08-14 14:06:32.533: D/dalvikvm(828): GC_CONCURRENT freed 32K, 3% free 9027K/9223K, paused 81ms+111ms, total 343ms
08-14 14:06:32.813: I/Choreographer(828): Skipped 71 frames!  The application may be doing too much work on its main thread.
08-14 14:06:33.303: I/Choreographer(828): Skipped 38 frames!  The application may be doing too much work on its main thread.
08-14 14:06:39.854: D/InputEventConsistencyVerifier(828): KeyEvent: ACTION_UP but key was not down.
08-14 14:06:39.854: D/InputEventConsistencyVerifier(828):   in android.widget.EditText@412b2f68
08-14 14:06:39.854: D/InputEventConsistencyVerifier(828):   0: sent at 1614282000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_M, scanCode=50, metaState=0, flags=0x8, repeatCount=0, eventTime=1614282, downTime=1614282, deviceId=0, source=0x301 }
08-14 14:06:39.874: D/InputEventConsistencyVerifier(828): KeyEvent: ACTION_UP but key was not down.
08-14 14:06:39.874: D/InputEventConsistencyVerifier(828):   in android.widget.EditText@412b2f68
08-14 14:06:39.874: D/InputEventConsistencyVerifier(828):   0: sent at 1614392000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=1614392, downTime=1614282, deviceId=0, source=0x301 }
08-14 14:06:39.874: D/InputEventConsistencyVerifier(828):   -- recent events --
08-14 14:06:39.874: D/InputEventConsistencyVerifier(828):   1: sent at 1614282000000, (unhandled) KeyEvent { action=ACTION_UP, keyCode=KEYCODE_M, scanCode=50, metaState=0, flags=0x80000008, repeatCount=0, eventTime=1614282, downTime=1614282, deviceId=0, source=0x301 }
08-14 14:07:02.362: D/AndroidRuntime(828): Shutting down VM
08-14 14:07:02.362: W/dalvikvm(828): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
**08-14 14:07:02.472: E/AndroidRuntime(828): FATAL EXCEPTION: main
08-14 14:07:02.472: E/AndroidRuntime(828): android.os.NetworkOnMainThreadException
08-14 14:07:02.472: E/AndroidRuntime(828):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-14 14:07:02.472: E/AndroidRuntime(828):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
08-14 14:07:02.472: E/AndroidRuntime(828):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
08-14 14:07:02.472: E/AndroidRuntime(828):  at libcore.io.IoBridge.connect(IoBridge.java:112)
08-14 14:07:02.472: E/AndroidRuntime(828):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-14 14:07:02.472: E/AndroidRuntime(828):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
08-14 14:07:02.472: E/AndroidRuntime(828):  at java.net.Socket.connect(Socket.java:842)
08-14 14:07:02.472: E/AndroidRuntime(828):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
08-14 14:07:02.472: E/AndroidRuntime(828):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
08-14 14:07:02.472: E/AndroidRuntime(828):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-14 14:07:02.472: E/AndroidRuntime(828):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-14 14:07:02.472: E/AndroidRuntime(828):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-14 14:07:02.472: E/AndroidRuntime(828):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-14 14:07:02.472: E/AndroidRuntime(828):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-14 14:07:02.472: E/AndroidRuntime(828):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-14 14:07:02.472: E/AndroidRuntime(828):  at library.JSONParser.getJSONFromUrl(JSONParser.java:41)
08-14 14:07:02.472: E/AndroidRuntime(828):  at library.UserFunctions.loginUser(UserFunctions.java:40)
08-14 14:07:02.472: E/AndroidRuntime(828):  at com.thryfting.www.LoginActivity.onClick(LoginActivity.java:66)
08-14 14:07:02.472: E/AndroidRuntime(828):  at android.view.View.performClick(View.java:4084)
08-14 14:07:02.472: E/AndroidRuntime(828):  at android.view.View$PerformClick.run(View.java:16966)
08-14 14:07:02.472: E/AndroidRuntime(828):  at android.os.Handler.handleCallback(Handler.java:615)
08-14 14:07:02.472: E/AndroidRuntime(828):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 14:07:02.472: E/AndroidRuntime(828):  at android.os.Looper.loop(Looper.java:137)
08-14 14:07:02.472: E/AndroidRuntime(828):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-14 14:07:02.472: E/AndroidRuntime(828):  at java.lang.reflect.Method.invokeNative(Native Method)
08-14 14:07:02.472: E/AndroidRuntime(828):  at java.lang.reflect.Method.invoke(Method.java:511)
08-14 14:07:02.472: E/AndroidRuntime(828):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 14:07:02.472: E/AndroidRuntime(828):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 14:07:02.472: E/AndroidRuntime(828):  at dalvik.system.NativeStart.main(Native Method)**

注册活动的更新代码:

    package com.thryfting.www;

import org.json.JSONException;
import org.json.JSONObject;

import library.DatabaseHandler;
import library.UserFunctions;
import android.content.Intent;
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.TextView;

import com.actionbarsherlock.app.SherlockActivity;

public class RegisterActivity extends SherlockActivity implements OnClickListener{

    Button btnRegister;
    EditText inputFullName;
    EditText inputEmail;
    EditText inputPassword;
    TextView registerErrorMsg;
    TextView loginScreen;

    //JSON Response node names
    private static String KEY_SUCCESS    = "success";
    private static String KEY_ERROR      = "error";
    private static String KEY_ERROR_MSG  = "error_msg";
    private static String KEY_UID        = "uid";
    private static String KEY_NAME       = "name";
    private static String KEY_EMAIL      = "email";
    private static String KEY_CREATED_AT = "created_at";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        loginScreen      = (TextView) findViewById(R.id.link_to_login);
        inputEmail       = (EditText) findViewById(R.id.etemailSignup);
        inputPassword    = (EditText) findViewById(R.id.etPasswordSignup);
        inputFullName    = (EditText) findViewById(R.id.etFullnameSignup);
        btnRegister      = (Button) findViewById(R.id.btnRegister);
        registerErrorMsg = (TextView) findViewById(R.id.register_error);


        loginScreen.setOnClickListener(this);
        btnRegister.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.btnRegister:

        new register().execute(KEY_SUCCESS);

        break;

    case R.id.link_to_login:
        Intent i = new Intent(getApplicationContext(), LoginActivity.class);
        startActivity(i);

        //Close Registration View
        finish();

        break;

        }

    }

    public class register extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            String name = inputFullName.getText().toString();
            String email = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();

            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.registerUser(name, email, password);

            try{

                if(json.getString(KEY_SUCCESS) != null){
                    registerErrorMsg.setText("");

                    String res = json.getString(KEY_SUCCESS);
                    if(Integer.parseInt(res) == 1){
                        //user successfully registered
                        // Store user details in SQLite Database
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        //Clear all previous data in database
                        userFunction.logoutUser(getApplicationContext());
                        db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));



                    }else{
                        //Error in registration
                        registerErrorMsg.setText("Error occured in registration");
                    }
                }
            } catch(JSONException e){
                e.printStackTrace();
            } finally{

                //Launch Dashboard 
                Intent dashboard = new Intent (getApplicationContext(), Timeline.class);

                //Close all views before launching Dashboard
                dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                //Close registration screen
                finish();
            } 

            return null;
        }

    }

}

新的错误信息如下:

08-15 14:53:32.940: W/dalvikvm(865): threadid=11: thread exiting with uncaught exception (group=0x40a13300)
08-15 14:53:33.060: E/AndroidRuntime(865): FATAL EXCEPTION: AsyncTask #1
08-15 14:53:33.060: E/AndroidRuntime(865): java.lang.RuntimeException: An error occured while executing doInBackground()
08-15 14:53:33.060: E/AndroidRuntime(865):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-15 14:53:33.060: E/AndroidRuntime(865):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-15 14:53:33.060: E/AndroidRuntime(865):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-15 14:53:33.060: E/AndroidRuntime(865):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-15 14:53:33.060: E/AndroidRuntime(865):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-15 14:53:33.060: E/AndroidRuntime(865):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-15 14:53:33.060: E/AndroidRuntime(865):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-15 14:53:33.060: E/AndroidRuntime(865):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-15 14:53:33.060: E/AndroidRuntime(865):  at java.lang.Thread.run(Thread.java:856)
08-15 14:53:33.060: E/AndroidRuntime(865): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=register
08-15 14:53:33.060: E/AndroidRuntime(865):  at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
08-15 14:53:33.060: E/AndroidRuntime(865):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
08-15 14:53:33.060: E/AndroidRuntime(865):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-15 14:53:33.060: E/AndroidRuntime(865):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-15 14:53:33.060: E/AndroidRuntime(865):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-15 14:53:33.060: E/AndroidRuntime(865):  at library.JSONParser.getJSONFromUrl(JSONParser.java:41)
08-15 14:53:33.060: E/AndroidRuntime(865):  at library.UserFunctions.registerUser(UserFunctions.java:63)
08-15 14:53:33.060: E/AndroidRuntime(865):  at com.thryfting.www.RegisterActivity$register.doInBackground(RegisterActivity.java:89)
08-15 14:53:33.060: E/AndroidRuntime(865):  at com.thryfting.www.RegisterActivity$register.doInBackground(RegisterActivity.java:1)
08-15 14:53:33.060: E/AndroidRuntime(865):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-15 14:53:33.060: E/AndroidRuntime(865):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-15 14:53:33.060: E/AndroidRuntime(865):  ... 5 more
08-15 14:53:34.812: I/Choreographer(865): Skipped 93 frames!  The application may be doing too much work on its main thread.
08-15 14:53:36.512: I/Process(865): Sending signal. PID: 865 SIG: 9




public JSONObject registerUser(String name, String email, String password){
        //building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", register_tag));
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));

        //Getting JSON object
        JSONObject json = jsonParser.getJSONFromUrl(register_tag, params);

        //return json
        return json;

    }
4

3 回答 3

3

您违反了 StrictMode。这意味着您正在 UI 线程上做一些阻塞。因此,原因

android.os.NetworkOnMainThreadException

从您的 LOG 的其余部分看来,您正在执行一个 Web 请求。确保 HTTP 请求包含在 AsyncTask(或类似的东西)中

于 2012-08-14T18:27:34.810 回答
1

您似乎正在尝试在主线程上进行登录和注册。首先,这是一种不好的做法,并且不能在最近的 API 级别上完成。您需要做的就是在单独的线程中建立网络连接,无论是 AsyncTask 还是普通的旧 Thread()

举个例子 :

用这个

public void login()
{
   new Thread(new Runnable() {

        @Override
        public void run() {
    //your network connection goes here
    }
   }).start();
}

代替 :

public void login()
{
    //your network connection goes here
}
于 2012-08-14T18:29:56.790 回答
0

尝试在具有互联网连接的手机中检查相同的内容。logcat 还显示模拟器没有 GPS 连接。

于 2012-08-14T18:29:47.380 回答