Android应用,从菜单项启动tabActivity

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

标签: java android xml

我正在尝试从我的主tabActivity的菜单窗格中启动activity(我的意思是启动时加载的那个)。

尽管应用程序编译得很好,甚至运行,但只要按下菜单按钮,tabActivity的相关图标就会出现运行时错误:

$ 04-10 03:00:50.374: E/AndroidRuntime(6531): FATAL EXCEPTION: main
04-10 03:00:50.374: E/AndroidRuntime(6531): java.lang.RuntimeException: Unable to start activity ComponentInfo{Impek.Gen/Impek.Gen.Impekcals}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2753)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.app.ActivityThread.access$2500(ActivityThread.java:129)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2117)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.os.Looper.loop(Looper.java:143)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.app.ActivityThread.main(ActivityThread.java:4717)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at java.lang.reflect.Method.invoke(Method.java:521)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at dalvik.system.NativeStart.main(Native Method)
04-10 03:00:50.374: E/AndroidRuntime(6531): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.app.TabActivity.onContentChanged(TabActivity.java:105)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:229)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.app.Activity.setContentView(Activity.java:1677)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at Impek.Gen.Impekcals.onCreate(Impekcals.java:34)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-10 03:00:50.374: E/AndroidRuntime(6531):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2717)
04-10 03:00:50.374: E/AndroidRuntime(6531):     ... 11 more

代码:

     $  main activitys oncreate and menu functions
     public class ImpekActivity extends Activity {
/** Called when the activity is first created. */
private Planner p;
public static Context curr;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    curr = this;
    GeoLocation.setup_GeoLocation();
    p = new Planner(curr);
    update();
    //p.addNewEntry("", latitude, longitude, arrival, postcode)
} 

        @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.item2:
            Intent f = new Intent(curr, Impeksettings.class);
            startActivity(f);
            return true;
        case R.id.item1:
            Intent t = new Intent(curr,Impekadd.class);
            startActivity(t);
            return true;
        case R.id.calendars:
            Intent c = new Intent(curr,Impekcals.class);
            startActivity(c);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    return true;
}`

此标签活动在按下时调用:

public class Impekcals extends TabActivity{

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendars);

            Resources res = getResources(); // Resource object to get Drawables
            TabHost tabHost = getTabHost();  // The activity TabHost
            TabHost.TabSpec spec;  // Resusable TabSpec for each tab
            Intent intent;  // Reusable Intent for each tab

            // Create an Intent to launch an Activity for the tab (to be reused)
            intent = new Intent().setClass(this, GlobalCal.class);

            // Initialize a TabSpec for each tab and add it to the TabHost
            spec = tabHost.newTabSpec("global").setIndicator("Global",
                              res.getDrawable(R.drawable.globalcal))
                          .setContent(intent);
            tabHost.addTab(spec);

            // Do the same for the other tabs
            intent = new Intent().setClass(this, GoogleCal.class);
            spec = tabHost.newTabSpec("Googlecal").setIndicator("Google",
                              res.getDrawable(R.drawable.googlecal))
                          .setContent(intent);
            tabHost.addTab(spec);

            intent = new Intent().setClass(this, LocalCal.class);
            spec = tabHost.newTabSpec("Local").setIndicator("Local",
                              res.getDrawable(R.drawable.localcal))
                          .setContent(intent);
            tabHost.addTab(spec);


            intent = new Intent().setClass(this, SearchEvent.class);
            spec = tabHost.newTabSpec("search").setIndicator("Search",
                              res.getDrawable(R.drawable.searchcal))
                          .setContent(intent);
            tabHost.addTab(spec);

     tabHost.setCurrentTab(1);

    }



}




    `

任何帮助表示赞赏:D

1 个答案:

答案 0 :(得分:0)

我想知道this questionthis one是否解决了您的问题?

没有源代码,特别是看你是否有:

setContentView(R.layout.main);

setContentView(R.layout.your_resource_id);

很难提供任何进一步的帮助。