3.0 の Android アプリケーションと、タブとスワイプを使用するアクティビティがあります。タイトル バーを削除したいのですがandroid:theme="@android:style/Theme.NoTitleBar
、アプリケーションを書き込んで実行すると、ログ キャットに 1 つの NullPointerException チェックが表示され、この行を削除すると、アプリケーションは非常にうまく動作します。
これは私のコードです:
public class Estructura extends FragmentActivity implement 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): 致命的な例外: メイン 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): at 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(ルーパー) .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) ): java.lang.reflect.Method.invokeNative(Native Method) 06-14 12:31:55.960: E/AndroidRuntime(21882): 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(ネイティブ メソッド) 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): com.lirioandroid.museocaldemoron.Estructura.onCreate(Estructura.java:35)