0

我有 3.0 的 android 应用程序和一个带有标签和滑动的活动。我想删除标题栏,但是当编写android:theme="@android:style/Theme.NoTitleBar并运行我的应用程序时,日志猫说一个 NullPointerException 检查我,当我删除这一行时,应用程序运行得很好。

这是我的代码:

公共类 Estructura 扩展 FragmentActivity 实现 ActionBar.TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the three primary sections of the app. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
CollectionPagerAdapter mCollectionPagerAdapter;

/**
 * The {@link android.support.v4.view.ViewPager} that will display the
 * object collection.
 */
ViewPager mViewPager;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_estructura);

    // Create an adapter that when requested, will return a fragment
    // representing an object in
    // the collection.
    //
    // ViewPager and its adapters use support library fragments, so we must
    // use
    // getSupportFragmentManager.
    mCollectionPagerAdapter = new CollectionPagerAdapter(
            getSupportFragmentManager());

    // Set up action bar.
    final ActionBar actionBar = getActionBar();

    // Specify that the Home/Up button should not be enabled, since there is
    // no hierarchical
    // parent.
    // actionBar.setHomeButtonEnabled(false);

    // Specify that we will be displaying tabs in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener
    // for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // When swiping between different app sections, select
                    // the corresponding tab.
                    // We can also use ActionBar.Tab#select() to do this if
                    // we have a reference to the
                    // Tab.
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter.
        // Also specify this Activity object, which implements the
        // TabListener interface, as the
        // listener for when this tab is selected.
        actionBar.addTab(actionBar.newTab()
                .setText(mCollectionPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
}

public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the primary sections of the app.
 */
public class CollectionPagerAdapter extends FragmentPagerAdapter {

    final int NUM_ITEMS = 6; // number of tabs

    public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new TabFragment();
        Bundle args = new Bundle();
        args.putInt(TabFragment.ARG_OBJECT, i);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String tabLabel = null;
        switch (position) {
        case 0:
            tabLabel = getString(R.string.inicio);
            break;
        case 1:
            tabLabel = getString(R.string.fotos);
            break;
        case 2:
            tabLabel = getString(R.string.videos);
            break;
        case 3:
            tabLabel = getString(R.string.visitas);
            break;
        case 4:
            tabLabel = getString(R.string.cursos);
            break;
        case 5:
            tabLabel = getString(R.string.contacto);
            break;
        }

        return tabLabel;
    }
}

/**
 * A fragment that launches other parts of the demo application.
 */
public static class TabFragment extends Fragment {

    public static final String ARG_OBJECT = "object";

    // @Override
    // public View onCreateView(LayoutInflater inflater, ViewGroup
    // container,
    // Bundle savedInstanceState) {
    //
    // Bundle args = getArguments();
    // int position = args.getInt(ARG_OBJECT);
    //
    // int tabLayout = 0;
    // switch (position) {
    // case 0:
    // tabLayout = R.layout.tab1;
    // break;
    // case 1:
    // tabLayout = R.layout.tab2;
    // break;
    // case 2:
    // tabLayout = R.layout.tab3;
    // break;
    // case 3:
    // tabLayout = R.layout.tab4;
    // break;
    // case 4:
    // tabLayout = R.layout.tab5;
    // break;
    // case 5:
    // tabLayout = R.layout.tab6;
    // break;
    // }
    //
    // View rootView = inflater.inflate(tabLayout, container, false);
    //
    // return rootView;
    // }
}

}

和日志猫:

06-14 12:31:55.960: E/AndroidRuntime(21882): 致命异常: main 06-14 12:31:55.960: E/AndroidRuntime(21882): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. lirioandroid.museocaldemoron/com.lirioandroid.museocaldemoron.Estructura}:android.util.AndroidRuntimeException:requestFeature() 必须在添加内容之前调用 06-14 12:31:55.960:E/AndroidRuntime(21882):在 android.app.ActivityThread .performLaunchActivity(ActivityThread.java:1967) 06-14 12:31:55.960: E/AndroidRuntime(21882): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 06-14 12:31:55.960: E /AndroidRuntime(21882): 在 android.app.ActivityThread.access$600(ActivityThread.java:127) 06-14 12:31:55.960: E/AndroidRuntime(21882): 在 android.app.ActivityThread$H.handleMessage(ActivityThread .java:1158) 06-14 12:31:55.960: E/AndroidRuntime(21882): 在 android.os.Handler.dispatchMessage(Handler.java:99) 06-14 12:31:55.960: E/AndroidRuntime(21882): 在 android.os.Looper.loop(Looper .java:137) 06-14 12:31:55.960: E/AndroidRuntime(21882): 在 android.app.ActivityThread.main(ActivityThread.java:4441) 06-14 12:31:55.960: E/AndroidRuntime(21882) ): at java.lang.reflect.Method.invokeNative(Native Method) 06-14 12:31:55.960: E/AndroidRuntime(21882): at java.lang.reflect.Method.invoke(Method.java:511) 06 -14 12:31:55.960: E/AndroidRuntime(21882): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-14 12:31:55.960: E/AndroidRuntime(21882 ): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-14 12:31:55.960: E/AndroidRuntime(21882): 在 dalvik.system.NativeStart.main(Native Method) 06 -14 12:31:55.960:E/AndroidRuntime(21882):原因:android.util.AndroidRuntimeException:在添加内容之前必须调用 requestFeature() 06-14 12:31:55.960:E/AndroidRuntime(21882):在 com.android。 internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229) 06-14 12:31:55.960: E/AndroidRuntime(21882): at com.lirioandroid.museocaldemoron.Estructura.onCreate(Estructura.java:35)

4

1 回答 1

0

在 com.lirioandroid.museocaldemoron.Estructura.onCreate(Estructura.java:35) 上查看您的代码,我假设它是任何触及 actionBar 变量的东西:

final ActionBar actionBar = getActionBar();

当你设置 noTitleBar 时,那个 actionBar 就不再存在了!您可能想在操作栏中尝试空白标题

于 2013-06-24T00:46:14.510 回答