android无法从一个选项卡切换到另一个选项卡

时间:2012-10-04 10:12:02

标签: android tabs tabactivity android-tabactivity

我想从一个标签切换到另一个标签。为此我使用下面的代码:

public void switchTabInActivity(int indexTabToSwitchTo){
    AccidentTabActivity ParentActivity;
    ParentActivity = (AccidentTabActivity) getParent(); // here i am getting exception
    ParentActivity.switchTab(indexTabToSwitchTo);
    }

我正在调用我想要导航的方法。

但我除了演员类外。

Log cat:

10-04 15:39:22.327: ERROR/AndroidRuntime(1016): FATAL EXCEPTION: main
10-04 15:39:22.327: ERROR/AndroidRuntime(1016): java.lang.ClassCastException: com.project.AccidentApp.TabGroupHomeActivity
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at com.project.AccidentApp.KnowWhatToDoMenuActivity.switchTabInActivity(KnowWhatToDoMenuActivity.java:189)
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at com.project.AccidentApp.KnowWhatToDoMenuActivity$3.onClick(KnowWhatToDoMenuActivity.java:155)
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at android.os.Looper.loop(Looper.java:130)
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at java.lang.reflect.Method.invoke(Method.java:507)
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-04 15:39:22.327: ERROR/AndroidRuntime(1016):     at dalvik.system.NativeStart.main(Native Method)

已更新

TabGroupHomeActivity.java

public class TabGroupHomeActivity extends TabGroupActivity{ 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startChildActivity("KnowWhatToDoMenuActivity", new Intent(this,KnowWhatToDoMenuActivity.class));
    }


}

AccidentTabActivity.java

public class AccidentTabActivity extends TabActivity 
{
    private TabHost tabHost;
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tab);

        tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        /////////////////////////////
        intent = new Intent().setClass(this,TabGroupHomeActivity.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("Menu").setIndicator(prepareTabView("Menu",R.drawable.menul)).setContent(intent);
        tabHost.addTab(spec); // error at this line

        intent = new Intent().setClass(this,TabGroupStepsActivity.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("Steps").setIndicator(prepareTabView("Steps",R.drawable.tab_step)).setContent(intent);
        tabHost.addTab(spec); 

        intent = new Intent().setClass(this,TabGroupProfileActivity.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("Profile").setIndicator(prepareTabView("Profile",R.drawable.tab_profile)).setContent(intent);
        tabHost.addTab(spec);       

        intent = new Intent().setClass(this,TabGroupContactUsActivity.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("Contact").setIndicator(prepareTabView("Menu",R.drawable.tab_contact)).setContent(intent);
        tabHost.addTab(spec);


        tabHost.setCurrentTab(0);




    }

     private View prepareTabView(String text, int resId) {
         View view = LayoutInflater.from(this).inflate(R.layout.tabs, null);
         ImageView iv = (ImageView) view.findViewById(R.id.tabImageView);
         TextView tv = (TextView) view.findViewById(R.id.tabTextView);
         iv.setImageResource(resId);
         tv.setText(text);
         return view;
    }

     public void switchTab(int tab){

         //getLocalActivityManager().getActivity(tab);
            //String strTab = ""+tab;
            tabHost.setCurrentTab(tab);


    }
}

2 个答案:

答案 0 :(得分:1)

尝试将tabhost声明为静态并调用子活动,如下所示。

取而代之的是私人TabHost tabHost;

尝试静态TabHost tabHost;

并在您的儿童活动中

 AccidentTabActivity.tabHost.setCurrentTab(2);

然后您可以根据需要设置当前标签。

答案 1 :(得分:0)

从您的例外情况getParent()未提供AccidentTabActivity的实例。 你能告诉我们AccidentTabActivityTabGroupHomeActivity班吗?我不认为我们能够在没有课程的情况下帮助您进行调试。

Ps:Java约定规定不要在代码AccidentTabActivity ParentActivity;中使用大写字母来启动变量名称,请参阅http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html。不是批评,但你的代码会更具可读性。