我使用 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();
}
}
});
}
}
你能告诉我修改吗?谢谢 !