1

I have this code:

       public void onResume()
{
    super.onResume();

      tabHost=getTabHost();



        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("All").setContent(new Intent(this, AllTest.class)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Attempted").setContent(
                new Intent(this, Att_Test.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Unattempted").setContent(
                new Intent(this, SyncAlbum.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("New").setContent(
                new Intent(this, SyncTest.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));



        tabHost.getTabWidget().getChildAt(0).getLayoutParams().height =35;

        tabHost.getTabWidget().getChildAt(1).getLayoutParams().height =35;
        tabHost.getTabWidget().getChildAt(2).getLayoutParams().height =35;
        tabHost.getTabWidget().getChildAt(3).getLayoutParams().height =35;

        if(tabbed.equals("Att_Test")){
        tabHost.setCurrentTab(1);
        }
        else
        {
            tabHost.setCurrentTab(0);
        }
}

after a sequence of method .onResume is again called like this:

                                tabHost.clearAllTabs();

                                onResume();

This code works fine when user is in first tab but as we try to call onResume method in other tab it gives a nullPointer:

       03-27 18:48:05.833: E/AndroidRuntime(18564): FATAL EXCEPTION: main
       03-27 18:48:05.833: E/AndroidRuntime(18564): java.lang.NullPointerException
       03-27 18:48:05.833: E/AndroidRuntime(18564):     at             android.widget.TabWidget.setCurrentTab(TabWidget.java:339)
        03-27 18:48:05.833: E/AndroidRuntime(18564):    at android.widget.TabWidget.focusCurrentTab(TabWidget.java:363)
      03-27 18:48:05.833: E/AndroidRuntime(18564):  at android.widget.TabHost.setCurrentTab(TabHost.java:320)
       03-27 18:48:05.833: E/AndroidRuntime(18564):     at android.widget.TabHost.addTab(TabHost.java:213)
       03-27 18:48:05.833: E/AndroidRuntime(18564):     at com.cuelearn.main.TestTab.onResume(TestTab.java:65)
       03-27 18:48:05.833: E/AndroidRuntime(18564):     at com.cuelearn.main.TestTab$3$1.onClick(TestTab.java:209)
      03-27 18:48:05.833: E/AndroidRuntime(18564):  at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:874)
      03-27 18:48:05.833: E/AndroidRuntime(18564):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
      03-27 18:48:05.833: E/AndroidRuntime(18564):  at android.widget.ListView.performItemClick(ListView.java:3382)
       03-27 18:48:05.833: E/AndroidRuntime(18564):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
      03-27 18:48:05.833: E/AndroidRuntime(18564):  at android.os.Handler.handleCallback(Handler.java:587)
      03-27 18:48:05.833: E/AndroidRuntime(18564):  at android.os.Handler.dispatchMessage(Handler.java:92)
      03-27 18:48:05.833: E/AndroidRuntime(18564):  at android.os.Looper.loop(Looper.java:123)

Please help me solve this problem.I am newbie?

Edited Whole code

These are the other methods:

    void setupView()
{
            Button name=(Button)findViewById(R.id.name);

        name.setText(login.A_students.STUDENT_NAME);

        name.setOnClickListener(logout);

        ((Button)findViewById(R.id.cuelearn)).setOnClickListener(cuelearnClicked );

        courses course_db= new courses(this);
        course_db.open();
        Cursor s=course_db.getrecord(CourseId);


        coursename=(Button)findViewById(R.id.blank);

        setCourseName(s.getString(2));
        coursename.setOnClickListener(course_nameClicked);




}

void setCourseName(String courseid)
{
    coursename.setText(courseid);


}
 private OnClickListener logout = new OnClickListener(){
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(TestTab.this);
            builder.setMessage("Are you sure you want to Logout?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    finish();
                    startActivity(new Intent(TestTab.this,com.cuelearn.main.login.class));

                }
            })
            .setNeutralButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert = builder.create();
            alert.show();

        }};
         private OnClickListener cuelearnClicked = new OnClickListener(){
             @Override
             public void onClick(View v) {
                 // TODO OnClickListener()

                 Intent intent = new Intent(TestTab.this, cuelearntwo.class)

Second Tab Code

public class Att_Test extends Activity{

private testpapertable testpaper_db;
private courses courses_db;
private coursesclass A_courses=new coursesclass();
ArrayList<String> courseid;
ArrayList<String> courseName;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TestTab.tabbed="Att_Test";
    requestWindowFeature(Window.FEATURE_NO_TITLE);


        System.out.println(TestTab.CourseId+"ALLTE");
    setContentView(R.layout.alltest);

    showAllTestPapers();



}
Navdroid
  • 4,453
  • 7
  • 29
  • 47

4 Answers4

2

Try this

 tabHost.setCurrentTabByTag("tab1");

This is work with me. Hope it will help you too..

Thanks...

Never Quit
  • 2,072
  • 1
  • 21
  • 44
1

I think you call the following function from a child activity of the TabActivity.

tabHost.clearAllTabs();

This removes all tabs associated with the tab host. So nothing can be displayed if there is nothing to display.

I'm not totally sure of that, but I'd check in that way.


Regarding to the previous answer, here is a way to call a tabHost method from a child activity:

((TabActivity) getParent()).getTabHost().setCurrentTab(0);

I hope this can help.

Manitoba
  • 8,522
  • 11
  • 60
  • 122
  • But that is working so fine when user is in first Tab..the problem persists only when I change the tab and call onresume() – Navdroid Mar 27 '12 at 13:54
1

Ok, so here is a method to create a new tab:

private static int label = 0;

private void addTab(String name, Class<?> c)
{
    TabHost tabHost = getTabHost();
    Intent intent = new Intent(this, c);
    TabHost.TabSpec spec = tabHost.newTabSpec("tab_" + label++);    

    spec.setIndicator(name);
    spec.setContent(intent);
    tabHost.addTab(spec);
}

You can add all your tabs like that

private void addTabs()
{
    addTab("All", AllTest.class);
    addTab("Attempted", Att_Test.class);
    addTab("Unattempted", SyncAlbum.class);
    addTab("New", R.drawable.tab_indicator_location, SyncTest.class);
}

And finally add that to your onResume method:

public void onResume()
{
    super.onResume();

    setTabs();
}

It should work.

Manitoba
  • 8,522
  • 11
  • 60
  • 122
0

You can't hanble tab activity on Resume method.I have same problem because in tab activity it can used windowsonFoucs method you can't handle onResume method. You can paste your code onCreate method.

Bhargav Panchal
  • 1,159
  • 1
  • 12
  • 27
  • But that is working so fine when user is in first Tab..the problem persists only when I change the tab and call onResume() – Navdroid Mar 27 '12 at 13:55