0

当用户打开应用程序时,它的选项卡视图为

public class MainCategory extends TabActivity {
TabHost tabs;
static TabHost mTabHost;

ImageView ivSettings;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.maincategory);

 mTabHost = getTabHost();


    mTabHost.addTab(mTabHost
            .newTabSpec("three")
            .setIndicator("one",
                    getResources().getDrawable(R.drawable.ic_launcher))
            .setContent(new Intent(this, SettingsActivity.class)));


    mTabHost.addTab(mTabHost
            .newTabSpec("three")
            .setIndicator("three",
                    getResources().getDrawable(R.drawable.ic_launcher))
            .setContent(new Intent(this, MainActivity.class)));

    mTabHost.setCurrentTab(0);}

当用户单击第三个选项卡图标时,它会启动片段活动

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

但它使我的应用程序崩溃。这是我的日志猫输出

10-28 06:17:33.036: E/AndroidRuntime(775): FATAL EXCEPTION: main
10-28 06:17:33.036: E/AndroidRuntime(775): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.tabsswipe/info.androidhive.tabsswipe.MainActivity}: java.lang.NullPointerException
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2054)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.widget.TabHost.setCurrentTab(TabHost.java:413)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.view.View.performClick(View.java:4240)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.view.View$PerformClick.run(View.java:17721)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.os.Handler.handleCallback(Handler.java:730)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.os.Looper.loop(Looper.java:137)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-28 06:17:33.036: E/AndroidRuntime(775):  at java.lang.reflect.Method.invokeNative(Native Method)
10-28 06:17:33.036: E/AndroidRuntime(775):  at java.lang.reflect.Method.invoke(Method.java:525)
10-28 06:17:33.036: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-28 06:17:33.036: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-28 06:17:33.036: E/AndroidRuntime(775):  at dalvik.system.NativeStart.main(Native Method)
10-28 06:17:33.036: E/AndroidRuntime(775): **Caused by: java.lang.NullPointerException
10-28 06:17:33.036: E/AndroidRuntime(775):  at info.androidhive.tabsswipe.MainActivity.onCreate(MainActivity.java:31)**
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.app.Activity.performCreate(Activity.java:5133)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-28 06:17:33.036: E/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-28 06:17:33.036: E/AndroidRuntime(775):  ... 18 more

我想要以下输出(都使用选项卡活动和操作栏)

在此处输入图像描述

4

1 回答 1

1

您的活动是否在 Manifest.xml 中声明?

于 2013-10-28T10:30:35.873 回答