0

所以我为使用 Eclipse、PHP 和 MySQL 的应用程序创建了一个登录功能......它在多部手机(Xperia Play、Droidx、HTC Thunderbolt)上运行良好,但是,我无法让它在任何平板电脑上运行我试过了。(Nexus 7,华硕变压器)。单击注册新用户时,我不断收到强制关闭错误。

分辨率有问题吗?还是与我的代码有关?

我使用了本教程:

http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

谢谢!

编辑:这是我的日志:

12-01 18:22:14.896: D/AndroidRuntime(10040): Shutting down VM
12-01 18:22:14.896: W/dalvikvm(10040): threadid=1: thread exiting with uncaught exception (group=0x416f2930)
12-01 18:22:14.946: E/AndroidRuntime(10040): FATAL EXCEPTION: main
12-01 18:22:14.946: E/AndroidRuntime(10040): android.os.NetworkOnMainThreadException
12-01 18:22:14.946: E/AndroidRuntime(10040):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at libcore.io.IoBridge.connect(IoBridge.java:112)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at java.net.Socket.connect(Socket.java:842)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at com.groupone.the.health.effect.library.JSONParser.getJSONFromUrl(JSONParser.java:42)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at com.groupone.the.health.effect.library.UserFunctions.registerUser(UserFunctions.java:61)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at com.groupone.the.health.effect.RegisterActivity$1.onClick(RegisterActivity.java:54)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at android.view.View.performClick(View.java:4202)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at android.view.View$PerformClick.run(View.java:17340)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at android.os.Handler.handleCallback(Handler.java:725)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at android.os.Looper.loop(Looper.java:137)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at java.lang.reflect.Method.invokeNative(Native Method)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at java.lang.reflect.Method.invoke(Method.java:511)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-01 18:22:14.946: E/AndroidRuntime(10040):    at dalvik.system.NativeStart.main(Native Method)

编辑#2:这是“.RegisterActivity”的代码:

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

import com.example.androidhive.library.DatabaseHandler;
import com.example.androidhive.library.UserFunctions;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends Activity {
    Button btnRegister;
    Button btnLinkToLogin;
    EditText inputFullName;
    EditText inputEmail;
    EditText inputPassword;
    TextView registerErrorMsg;

    // 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
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        // Importing all assets like buttons, text fields
        inputFullName = (EditText) findViewById(R.id.registerName);
        inputEmail = (EditText) findViewById(R.id.registerEmail);
        inputPassword = (EditText) findViewById(R.id.registerPassword);
        btnRegister = (Button) findViewById(R.id.btnRegister);
        btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
        registerErrorMsg = (TextView) findViewById(R.id.register_error);

        // Register Button Click event
        btnRegister.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                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);

                // check for login response
                try {
                    if (json.getString(KEY_SUCCESS) != null) {
                        registerErrorMsg.setText("");
                        String res = json.getString(KEY_SUCCESS);
                        if(Integer.parseInt(res) == 1){
                            // user successfully registred
                            // 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));
                            // Launch Dashboard Screen
                            Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
                            // Close all views before launching Dashboard
                            dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard);
                            // Close Registration Screen
                            finish();
                        }else{
                            // Error in registration
                            registerErrorMsg.setText("Error occured in registration");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        // Link to Login Screen
        btnLinkToLogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(),
                        LoginActivity.class);
                startActivity(i);
                // Close Registration View
                finish();
            }
        });
    }
}
4

4 回答 4

2

你得到一个NetworkOnMainThreadException. 这意味着您正在主 UI 线程中执行繁重的网络操作。在低于 11 的 API 级别中,您可以执行此操作,但在 Android HC+ 上不再允许。您可以通过使用Asynctask来避免这种情况。

于 2012-12-01T23:31:13.890 回答
1

您是从另一个线程或异步任务发出 http 请求吗?因为平板电脑高于 api 级别 10。您可能面临严格模式问题。尝试在异步任务中发出您的 http 请求。

于 2012-12-01T23:21:21.527 回答
0

你能在主线程中产生网络流量吗?早期版本的 android 允许您这样做,但从 Honeycomb 开始,所有网络访问都必须在另一个线程中完成。你提到的平板电脑运行冰淇淋三明治,所以这是可能的。

很难解决你的问题。请留下堆栈跟踪和源代码。你的描述很模糊。

于 2012-12-01T23:22:17.490 回答
0

android.os.NetworkOnMainThreadException意味着您正在主线程上进行调用,它有一个非常简单的解决方案:在com.groupone.the.health.effect.RegisterActivity$1.onClick(RegisterActivity.java:54)

使用 AsynTask (http://developer.android.com/reference/android/os/AsyncTask.html) 进行注册。您必须将 UI 代码与那里的网络代码分开。

于 2012-12-01T23:31:34.047 回答