每当我尝试为我的应用程序(android运行时)运行注册活动时,我就会一直关闭。以下是我得到的错误:
08-16 23:58:41.146: W/dalvikvm(4017): threadid=10: thread exiting with uncaught exception (group=0x4001d5a0)
08-16 23:58:41.156: E/AndroidRuntime(4017): FATAL EXCEPTION: AsyncTask #1
08-16 23:58:41.156: E/AndroidRuntime(4017): java.lang.RuntimeException: An error occured while executing doInBackground()
08-16 23:58:41.156: E/AndroidRuntime(4017): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-16 23:58:41.156: E/AndroidRuntime(4017): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-16 23:58:41.156: E/AndroidRuntime(4017): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-16 23:58:41.156: E/AndroidRuntime(4017): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-16 23:58:41.156: E/AndroidRuntime(4017): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-16 23:58:41.156: E/AndroidRuntime(4017): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-16 23:58:41.156: E/AndroidRuntime(4017): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-16 23:58:41.156: E/AndroidRuntime(4017): at java.lang.Thread.run(Thread.java:1027)
08-16 23:58:41.156: E/AndroidRuntime(4017): Caused by: java.lang.NullPointerException
08-16 23:58:41.156: E/AndroidRuntime(4017): at com.thryfting.www.RegisterActivity$register.doInBackground(RegisterActivity.java:124)
08-16 23:58:41.156: E/AndroidRuntime(4017): at com.thryfting.www.RegisterActivity$register.doInBackground(RegisterActivity.java:1)
08-16 23:58:41.156: E/AndroidRuntime(4017): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-16 23:58:41.156: E/AndroidRuntime(4017): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-16 23:58:41.156: E/AndroidRuntime(4017): ... 4 more
08-16 23:58:41.416: E/WindowManager(4017): Activity com.thryfting.www.RegisterActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4058b4c8 that was originally added here
08-16 23:58:41.416: E/WindowManager(4017): android.view.WindowLeaked: Activity com.thryfting.www.RegisterActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4058b4c8 that was originally added here
08-16 23:58:41.416: E/WindowManager(4017): at android.view.ViewRoot.<init>(ViewRoot.java:277)
08-16 23:58:41.416: E/WindowManager(4017): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-16 23:58:41.416: E/WindowManager(4017): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-16 23:58:41.416: E/WindowManager(4017): at android.view.Window$LocalWindowManager.addView(Window.java:433)
08-16 23:58:41.416: E/WindowManager(4017): at android.app.Dialog.show(Dialog.java:265)
08-16 23:58:41.416: E/WindowManager(4017): at com.thryfting.www.RegisterActivity$register.onPreExecute(RegisterActivity.java:106)
08-16 23:58:41.416: E/WindowManager(4017): at android.os.AsyncTask.execute(AsyncTask.java:391)
08-16 23:58:41.416: E/WindowManager(4017): at com.thryfting.www.RegisterActivity.onClick(RegisterActivity.java:78)
08-16 23:58:41.416: E/WindowManager(4017): at android.view.View.performClick(View.java:2532)
08-16 23:58:41.416: E/WindowManager(4017): at android.view.View$PerformClick.run(View.java:9293)
08-16 23:58:41.416: E/WindowManager(4017): at android.os.Handler.handleCallback(Handler.java:587)
08-16 23:58:41.416: E/WindowManager(4017): at android.os.Handler.dispatchMessage(Handler.java:92)
08-16 23:58:41.416: E/WindowManager(4017): at android.os.Looper.loop(Looper.java:150)
08-16 23:58:41.416: E/WindowManager(4017): at android.app.ActivityThread.main(ActivityThread.java:4263)
08-16 23:58:41.416: E/WindowManager(4017): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 23:58:41.416: E/WindowManager(4017): at java.lang.reflect.Method.invoke(Method.java:507)
08-16 23:58:41.416: E/WindowManager(4017): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 23:58:41.416: E/WindowManager(4017): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 23:58:41.416: E/WindowManager(4017): at dalvik.system.NativeStart.main(Native Method)
这是我的AsyncTask代码,你可以告诉我,如果我做错了吗?
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnRegister:
String userEmail = inputEmail.getText().toString();
String userPassword = inputPassword.getText().toString();
String userFullName = inputPassword.getText().toString();
//Check if all fields have been completed.
if (userEmail.equals("") || userPassword.equals("") || userFullName.equals("")){
Toast.makeText(getApplicationContext(),
"You must fill in all of the fields.", Toast.LENGTH_SHORT).show();
return;
}
new register().execute();
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, String, String>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(RegisterActivity.this);
pDialog.setMessage("Joining Thryfting...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
// Register Button Click event
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(), Timeline.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");
Toast.makeText(getApplicationContext(), KEY_ERROR_MSG, Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
pDialog.dismiss();
}
}
答案 0 :(得分:1)
在doInBackground
方法中,您需要返回String
而不是null
。
您还应将新的Intent
移至onPostExecute
。
答案 1 :(得分:0)
您不能从AsyncTask的doInBackground()
(或UI线程以外的任何线程)访问UI组件。
示例:Toast.makeText(...).show();
。
相反,您应该在onProgressUpdate()
或onPostExecute()
中使用它。