[英]Android App crashes on error doInBackground
I am new in Android development. 我是Android开发的新手。 I came across an issue in a particular scenario. 我在特定情况下遇到了一个问题。 When I open my app, it starts AsyncTask
for db query. 当我打开应用程序时,它将启动AsyncTask
进行数据库查询。 As soon as the app opens I tap on the back button to terminate the app, the app crashes with this error: 打开应用程序后,我点击后退按钮以终止应用程序,该应用程序因以下错误而崩溃:
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
Caused by: java.lang.NullPointerException
at sarsat.lokate.Activity.MainActivity.Tab1$LongOperation.doInBackground(Tab1.java:62)
at sarsat.lokate.Activity.MainActivity.Tab1$LongOperation.doInBackground(Tab1.java:58)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
And this is my code: 这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
initImageLoader(getApplicationContext());
// Initializing
dataList = new ArrayList<DrawerItem>();
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setDivider(null);
mDrawerList.setDividerHeight(0);
int width = ((getResources().getDisplayMetrics().widthPixels)) - ((getResources().getDisplayMetrics().widthPixels)/5);
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerList.getLayoutParams();
params.width = width;
mDrawerList.setLayoutParams(params);
addDrawerItems(dataList);
drawerAdapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
dataList);
mDrawerList.setAdapter(drawerAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
//Object o = mDrawerList.getItemAtPosition(position);
//String str=(String)o; //As you are using Default String Adapter
//Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();
com.rey.material.widget.CheckBox checkBox = (com.rey.material.widget.CheckBox) v.findViewById(R.id.checkbox);
if(checkBox.isChecked()){
checkBox.setChecked(false);
}
else{
checkBox.setChecked(true);
}
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
MainActivity.this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
mDrawerToggle.syncState();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
mDrawerToggle.syncState();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);
// getSupportActionBar().setLogo(R.drawable.ic_drawer);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
// Creating The Toolbar and setting it as the Toolbar for the activity
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
//seedData();
LongOperation operation = new LongOperation();
operation.execute();
}
And in this code: 在这段代码中:
LongOperation operation = new LongOperation();
operation.execute();
This is what happening: 这是发生了什么:
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
seedData();
return "Executed";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {
}
}
How can I handle this issue in AsyncTask
so that the app does not crash anymore. 如何在AsyncTask
处理此问题,以使应用程序不再崩溃。
EDITED : 编辑 :
seedData method: seedData方法:
public void seedData(){
CouchBase db = new CouchBase(getApplicationContext());
db.DeleteAllDocs();
//Add J. Ad
doc = new Doc();
doc.vendorTitle = "Wear J. this summer - upto 50% off";
doc.vendorHandler = "@XYZ";
db.createAd(doc);
}
What you will need to do when you figure out where your NullPointerException
lies is design it to break out when the AsyncTask has been cancelled. 当您弄清NullPointerException
位置时,需要做的是将其设计为在取消AsyncTask时中断。
Your calling method for the task must keep a reference to it and within the onPause
call asyncTask.cancel()
. 您对任务的调用方法必须在onPause
调用asyncTask.cancel()
保留对其的引用。
You'll have to handle the break properly within your AsyncTask as well checking if the task has been cancelled before continuing what you are doing. 您必须在AsyncTask中正确处理中断,并在继续执行操作之前检查任务是否已取消。
protected Object doInBackground(Ojbect... objs) {
//...
while (completingYourTask) {
// Do your task but check before your NPException when needed
// Escape early if cancel() is called
if (isCancelled()) break;
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.