嘿,我正在创建一个Android应用程序,并希望将一个简单的标签布局作为其中一个屏幕。我按下主活动中的一个按钮,打开此活动。
我在本教程后面写了这封信:http://www.lucazanini.eu/2012/android/tabs-and-swipe-views/?lang=en,它可以自行运行,但是当我将它合并到我的应用程序中时,它会停止并退出。
这是java文件:
package ie.workinprogress.turas;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class IrelandInfo extends FragmentActivity implements
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_ireland_info);
// 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 = 3; // 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.tab1);
break;
case 1:
tabLabel = getString(R.string.tab2);
break;
case 2:
tabLabel = getString(R.string.tab3);
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;
}
View rootView = inflater.inflate(tabLayout, container, false);
return rootView;
}
}
}
LogCat告诉我由java.lang.NullPointerException引起的第5行错误。
final ActionBar actionBar = getActionBar();
我是Android的新手,我不太确定如何解决它。任何建议将不胜感激
编辑: 崩溃日志
12-28 16:08:15.650: E/Trace(2815): error opening trace file: No such file or directory (2)
12-28 16:08:22.560: E/AndroidRuntime(2815): FATAL EXCEPTION: main
12-28 16:08:22.560: E/AndroidRuntime(2815): java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.workinprogress.turas/ie.workinprogress.turas.IrelandInfo}: java.lang.NullPointerException
12-28 16:08:22.560: E/AndroidRuntime(2815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-28 16:08:22.560: E/AndroidRuntime(2815): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-28 16:08:22.560: E/AndroidRuntime(2815): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-28 16:08:22.560: E/AndroidRuntime(2815): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-28 16:08:22.560: E/AndroidRuntime(2815): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 16:08:22.560: E/AndroidRuntime(2815): at android.os.Looper.loop(Looper.java:137)
12-28 16:08:22.560: E/AndroidRuntime(2815): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-28 16:08:22.560: E/AndroidRuntime(2815): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:08:22.560: E/AndroidRuntime(2815): at java.lang.reflect.Method.invoke(Method.java:511)
12-28 16:08:22.560: E/AndroidRuntime(2815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-28 16:08:22.560: E/AndroidRuntime(2815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-28 16:08:22.560: E/AndroidRuntime(2815): at dalvik.system.NativeStart.main(Native Method)
12-28 16:08:22.560: E/AndroidRuntime(2815): Caused by: java.lang.NullPointerException
12-28 16:08:22.560: E/AndroidRuntime(2815): at ie.workinprogress.turas.IrelandInfo.onCreate(IrelandInfo.java:55)
12-28 16:08:22.560: E/AndroidRuntime(2815): at android.app.Activity.performCreate(Activity.java:5008)
12-28 16:08:22.560: E/AndroidRuntime(2815): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-28 16:08:22.560: E/AndroidRuntime(2815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-28 16:08:22.560: E/AndroidRuntime(2815): ... 11 more
12-28 16:12:15.720: E/Trace(2904): error opening trace file: No such file or directory (2)
12-28 16:12:24.020: E/AndroidRuntime(2904): FATAL EXCEPTION: main
12-28 16:12:24.020: E/AndroidRuntime(2904): java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.workinprogress.turas/ie.workinprogress.turas.IrelandInfo}: java.lang.NullPointerException
12-28 16:12:24.020: E/AndroidRuntime(2904): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-28 16:12:24.020: E/AndroidRuntime(2904): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-28 16:12:24.020: E/AndroidRuntime(2904): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-28 16:12:24.020: E/AndroidRuntime(2904): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-28 16:12:24.020: E/AndroidRuntime(2904): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 16:12:24.020: E/AndroidRuntime(2904): at android.os.Looper.loop(Looper.java:137)
12-28 16:12:24.020: E/AndroidRuntime(2904): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-28 16:12:24.020: E/AndroidRuntime(2904): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:12:24.020: E/AndroidRuntime(2904): at java.lang.reflect.Method.invoke(Method.java:511)
12-28 16:12:24.020: E/AndroidRuntime(2904): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-28 16:12:24.020: E/AndroidRuntime(2904): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-28 16:12:24.020: E/AndroidRuntime(2904): at dalvik.system.NativeStart.main(Native Method)
12-28 16:12:24.020: E/AndroidRuntime(2904): Caused by: java.lang.NullPointerException
12-28 16:12:24.020: E/AndroidRuntime(2904): at ie.workinprogress.turas.IrelandInfo.onCreate(IrelandInfo.java:55)
12-28 16:12:24.020: E/AndroidRuntime(2904): at android.app.Activity.performCreate(Activity.java:5008)
12-28 16:12:24.020: E/AndroidRuntime(2904): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-28 16:12:24.020: E/AndroidRuntime(2904): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-28 16:12:24.020: E/AndroidRuntime(2904): ... 11 more
编辑2: 布局文件
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
styles.xml:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
答案 0 :(得分:0)
尝试getSupportActionBar()
。 getACtionBar()不适用于支持库...如果您没有使用新的Android工具栏,请查看:http://android-developers.blogspot.co.il/2014/10/appcompat-v21-material-design-for-pre.html
答案 1 :(得分:0)
最后我又回去创建了项目并在New Android Application屏幕上选择了 “Holo Light with Dark Action Bar” 主题(在Eclipse中) 。在此之后,我再次阅读了教程并复制了其他活动的代码。我认为我最初为应用程序选择了太低的API(我需要更高的动画和其他东西)和一个不包含操作栏的基本主题。
感谢所有帮助:)