2014-12-28 119 views
0

嘿,我正在創建一個android應用程序,並希望將一個簡單的選項卡布局作爲其中一個屏幕。我按下了打開此活動的主要活動中的一個按鈕。Android Tabs活動在打開時停止

我正在關注本教程的內容: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

你能告訴我們你的佈局文件和樣式文件嗎?你使用toolBar嗎? – royB

+0

我還沒有使用ToolBar。我現在要研究它,謝謝 – Cian

回答

0

在我回去和再創造的項目結束和選擇的「河洛Light with Dark Action Bar「新Android應用程序屏幕上的主題(在Eclipse中)。在此之後,我再次閱讀教程並從其他活動的代碼中複製。我認爲我最初選擇的應用程序API太低(我需要更高的動畫和其他東西),並且沒有包含操作欄的基本主題。

感謝您的所有幫助:)