所以我为使用 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();
}
});
}
}