I am trying to create a dummy project to test AsyncTask with TabActivty. Firstly i have created a login screen, after login the tabview screen comes up.. in the tab view screen there are five tabs. the thing that I am trying to do is that when I click on a particular tab then first AsyncTask should run and then after loading all the data the content of that will be shown...
Sorry for my bad English. In simple words what I want to achieve is that:
Clicking on tab goes to AsycTask then loads the data and then show the content of that activity..
The following is my code:
code for the TabView class:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("home").setIndicator("Home").setContent(new Intent(this, TabGroup1.class)));
tabHost.addTab(tabHost.newTabSpec("top5").setIndicator("My Top5").setContent(new Intent(this, TabGroup2.class)));
tabHost.addTab(tabHost.newTabSpec("resv").setIndicator("My Reservation").setContent(new Intent(this, TabGroup3.class)));
tabHost.addTab(tabHost.newTabSpec("srch").setIndicator("Search").setContent(new Intent(this, TabGroup4.class)));
tabHost.addTab(tabHost.newTabSpec("prfl").setIndicator("Profile").setContent(new Intent(this, TabGroup5.class)));
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(this);
}
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
if(tabId.equals("top5"))
{
new LoadingScreen(this).execute("top5");
Log.v("tag", String.valueOf(tabId));
}
}
code for the LoadingScreen:
public class LoadingScreen extends AsyncTask <String, String, String> {
private ProgressDialog progress;
private Context context;
private static String rqst = null;
public LoadingScreen(Context context)
{
this.context = context;
progress = new ProgressDialog(context);
progress.setMessage("Loading...");
progress.setIndeterminate(false);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setCancelable(true);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progress.show();
}
@Override
protected String doInBackground(String... data) {
// TODO Auto-generated method stub
if(rqst.equals("top5"))
{
Log.v("tag", "Loading Screen");
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
progress.dismiss();
super.onPostExecute(result);
}
}
code for the TabGroups class:
public class TabGroups extends ActivityGroup {
ArrayList<String> list;
Window window;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(list == null)
list = new ArrayList<String>();
}
@Override
public void finishFromChild(Activity child)
{
LocalActivityManager manager = getLocalActivityManager();
int index = list.size() -1;
if(index < 1)
{
finish();
return;
}
manager.destroyActivity(list.get(index), true);
list.remove(index);
index--;
String lastId = list.get(index);
Intent in = manager.getActivity(lastId).getIntent();
window = manager.startActivity(lastId, in);
setContentView(window.getDecorView());
}
public void startChildActivity(String Id, Intent intent)
{
window = getLocalActivityManager().startActivity(Id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if(window != null)
{
list.add(Id);
setContentView(window.getDecorView());
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
return true;
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
@Override
public void onBackPressed()
{
int length = list.size();
if(length > 1)
{
Activity current = getLocalActivityManager().getActivity(list.get(length - 1));
current.finish();
}
}
}
code for the TabGroup2 class:
public class TabGroup2 extends TabGroups {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
startChildActivity("MyTop5", new Intent(this, MyTop5.class));
}
}
All the TabGroup 1-5 have same code...
The errors that I am getting are:
08-29 06:38:51.802: E/AndroidRuntime(3485): FATAL EXCEPTION: AsyncTask #1
08-29 06:38:51.802: E/AndroidRuntime(3485): java.lang.RuntimeException: An error occured while executing doInBackground()
08-29 06:38:51.802: E/AndroidRuntime(3485): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-29 06:38:51.802: E/AndroidRuntime(3485): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-29 06:38:51.802: E/AndroidRuntime(3485): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-29 06:38:51.802: E/AndroidRuntime(3485): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-29 06:38:51.802: E/AndroidRuntime(3485): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-29 06:38:51.802: E/AndroidRuntime(3485): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-29 06:38:51.802: E/AndroidRuntime(3485): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-29 06:38:51.802: E/AndroidRuntime(3485): at java.lang.Thread.run(Thread.java:1096)
08-29 06:38:51.802: E/AndroidRuntime(3485): Caused by: java.lang.NullPointerException
08-29 06:38:51.802: E/AndroidRuntime(3485): at com.pnf.gogolf.LoadingScreen.doInBackground(LoadingScreen.java:34)
08-29 06:38:51.802: E/AndroidRuntime(3485): at com.pnf.gogolf.LoadingScreen.doInBackground(LoadingScreen.java:1)
08-29 06:38:51.802: E/AndroidRuntime(3485): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-29 06:38:51.802: E/AndroidRuntime(3485): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-29 06:38:51.802: E/AndroidRuntime(3485): ... 4 more
08-29 06:38:53.253: E/WindowManager(3485): Activity com.pnf.gogolf.TabView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44eb9d18 that was originally added here
08-29 06:38:53.253: E/WindowManager(3485): android.view.WindowLeaked: Activity com.pnf.gogolf.TabView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44eb9d18 that was originally added here
08-29 06:38:53.253: E/WindowManager(3485): at android.view.ViewRoot.<init>(ViewRoot.java:247)
08-29 06:38:53.253: E/WindowManager(3485): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-29 06:38:53.253: E/WindowManager(3485): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-29 06:38:53.253: E/WindowManager(3485): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-29 06:38:53.253: E/WindowManager(3485): at android.app.Dialog.show(Dialog.java:241)
08-29 06:38:53.253: E/WindowManager(3485): at com.pnf.gogolf.LoadingScreen.onPreExecute(LoadingScreen.java:28)
08-29 06:38:53.253: E/WindowManager(3485): at android.os.AsyncTask.execute(AsyncTask.java:391)
08-29 06:38:53.253: E/WindowManager(3485): at com.pnf.gogolf.TabView.onTabChanged(TabView.java:31)
08-29 06:38:53.253: E/WindowManager(3485): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:356)
08-29 06:38:53.253: E/WindowManager(3485): at android.widget.TabHost.setCurrentTab(TabHost.java:341)
08-29 06:38:53.253: E/WindowManager(3485): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
08-29 06:38:53.253: E/WindowManager(3485): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
08-29 06:38:53.253: E/WindowManager(3485): at android.view.View.performClick(View.java:2408)
08-29 06:38:53.253: E/WindowManager(3485): at android.view.View$PerformClick.run(View.java:8816)
08-29 06:38:53.253: E/WindowManager(3485): at android.os.Handler.handleCallback(Handler.java:587)
08-29 06:38:53.253: E/WindowManager(3485): at android.os.Handler.dispatchMessage(Handler.java:92)
08-29 06:38:53.253: E/WindowManager(3485): at android.os.Looper.loop(Looper.java:123)
08-29 06:38:53.253: E/WindowManager(3485): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-29 06:38:53.253: E/WindowManager(3485): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 06:38:53.253: E/WindowManager(3485): at java.lang.reflect.Method.invoke(Method.java:521)
08-29 06:38:53.253: E/WindowManager(3485): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-29 06:38:53.253: E/WindowManager(3485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-29 06:38:53.253: E/WindowManager(3485): at dalvik.system.NativeStart.main(Native Method)
Thanks in advance...
The error message states it clearly, you have a NPE in your AsyncTask class:
Caused by: java.lang.NullPointerException
at com.pnf.gogolf.LoadingScreen.doInBackground(LoadingScreen.java:34)
This must be rqst
variable which is initialized as null and never changed.