Using AsyncTask with Sqlite crashed app during database installation on android?
I am trying to to AsyncTask to install a fairly large database using this code:
package samples.employeedirectory;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListAdapter;
import android.widget.ListView;
public class EmployeeList extends ListActivity {
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
protected Context context;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
grabURL();
}
public void grabURL() {
new GrabURL().execute();
}
public void onListItemClick(ListView parent, View view, int position, long id) {
Intent intent = new Intent(this, displayE.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("eID", cursor.getInt(cursor.getColumnIndex("eID")));
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.pref:
// Launch Preference activity
Intent intent = new Intent(this, Preferences.class);
startActivity(intent);
break;
}
return true;
}
private class GrabURL extends AsyncTask<Object, Void, Void> {
private ProgressDialog Dialog = new ProgressDialog(EmployeeList.this);
protected void onPreExecute() {
Dialog.setMessage("Downloading source..");
Dialog.show();
}
protected Void doInBackground(Object ... string) {
db = (new DatabaseHelper(EmployeeList.this)).getWritableDatabase();
cursor = db.rawQuery("SELECT * FROM employee", new String[]{});
return null;
}
protected void onPostExecute(Void unused) {
adapter = new qCursorAdapter(EmployeeList.this, cursor);
Dialog.dismiss();
setListAdapter(adapter);
}
}
}
This is the main activity for the app and the issue is that every time I run for the first time the app crashes. I am not sure why, but this is the message that I get.
08-16 01:07:27.783: ERROR/WindowManager(8908): android.view.WindowLeaked: Activity samples.employeedirectory.EmployeeList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2afd0298 that was originally added here
There are number of topics on SO discussing this error but I could not exactly understand how they are related to this particular case.
EDIT:
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): FATAL EXCEPTION: AsyncTask #1
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): java.lang.RuntimeException: An error occured while executing doInBackground()
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-开发者_运维百科16 01:24:31.073: ERROR/AndroidRuntime(8995): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at java.lang.Thread.run(Thread.java:1019)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at android.os.Handler.<init>(Handler.java:121)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at android.widget.Toast.<init>(Toast.java:68)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at android.widget.Toast.makeText(Toast.java:231)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at samples.employeedirectory.DatabaseHelper.onCreate(DatabaseHelper.java:42)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at samples.employeedirectory.EmployeeList$GrabURL.doInBackground(EmployeeList.java:79)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at samples.employeedirectory.EmployeeList$GrabURL.doInBackground(EmployeeList.java:1)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): ... 4 more
08-16 01:24:31.083: ERROR/WindowManager(8995): Activity samples.employeedirectory.EmployeeList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2afd1760 that was originally added here
08-16 01:24:31.083: ERROR/WindowManager(8995): android.view.WindowLeaked: Activity samples.employeedirectory.EmployeeList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2afd1760 that was originally added here
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.view.ViewRoot.<init>(ViewRoot.java:265)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.app.Dialog.show(Dialog.java:241)
08-16 01:24:31.083: ERROR/WindowManager(8995): at samples.employeedirectory.EmployeeList$GrabURL.onPreExecute(EmployeeList.java:75)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.os.AsyncTask.execute(AsyncTask.java:391)
08-16 01:24:31.083: ERROR/WindowManager(8995): at samples.employeedirectory.EmployeeList.grabURL(EmployeeList.java:40)
08-16 01:24:31.083: ERROR/WindowManager(8995): at samples.employeedirectory.EmployeeList.onCreate(EmployeeList.java:30)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.os.Looper.loop(Looper.java:123)
08-16 01:24:31.083: ERROR/WindowManager(8995): at android.app.ActivityThread.main(ActivityThread.java:3701)
08-16 01:24:31.083: ERROR/WindowManager(8995): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 01:24:31.083: ERROR/WindowManager(8995): at java.lang.reflect.Method.invoke(Method.java:507)
08-16 01:24:31.083: ERROR/WindowManager(8995): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
08-16 01:24:31.083: ERROR/WindowManager(8995): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
08-16 01:24:31.083: ERROR/WindowManager(8995): at dalvik.system.NativeStart.main(Native Method)
The issue is in the onCreate
method of your DatabaseHelper
where you are attempting to display a Toast
outside of the UI thread (it is being executed within doInBackground
).
You can spot this here:
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at android.os.Handler.<init>(Handler.java:121)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at android.widget.Toast.<init>(Toast.java:68)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at android.widget.Toast.makeText(Toast.java:231)
08-16 01:24:31.073: ERROR/AndroidRuntime(8995): at samples.employeedirectory.DatabaseHelper.onCreate(DatabaseHelper.java:42)
I think you are trying to update the Ui in the onPostExecute by setting the adapter. So,for doing that you have use a thread, so try this.
EmployeeList.this.runOnUiThread(new Runnable() {
@Override
public void run() {
setListAdapter(adapter);
}
});
If you are trying to update anything that is outside from the UI use runOnUiThread to update.
精彩评论