2015-04-02 49 views
-1

我是新來的Android UI設計幫助動作條,我正在開發一個選項卡式界面有兩個標籤添加標籤用的FragmentPagerAdapter

請幫我解決..

謝謝

這裏是我的MainActivity

import android.app.ActionBar; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.ActionBarActivity; 

public class MainSwipeActivity extends ActionBarActivity implements ActionBar.TabListener { 

private ViewPager viewPager; 
private TabsPagerAdapter mAdapter; 
private ActionBar actionBar; 
// Tab titles 
private String[] tabs = { "One", "Two", "Three" }; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_swipe); 
    ////////// 
    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)); 
    } 


    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 onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { 
    viewPager.setCurrentItem(tab.getPosition()); 
} 

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

} 

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

} 

}

哪個公頃代碼S中的佈局像這樣

<?xml version="1.0" encoding="utf-8"?> 
<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> 

我有2個片段,即一,二 其中包含簡單的UI元素

和我FragmentPagerAdapter類包含以下代碼

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
public class TabsPagerAdapter extends FragmentPagerAdapter { 
    public TabsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int index) { 

     switch (index) { 
      case 0: 
       // Top Rated fragment activity 
       return new One(); 
      case 1: 
       // Games fragment activity 
       return new Two(); 
      case 2: 
       // Movies fragment activity 
       return new One(); 
     } 

     return new One(); 
    } 

    @Override 
    public int getCount() { 
     // get item count - equal to number of tabs 
     return 3; 
    } 

} 

logcat中包含

3160-3160/envoyone.ak.ace.sampleer E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: envoyone.ak.ace.sampleer, PID: 3160 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{envoyone.ak.ace.sampleer/envoyone.ak.ace.sampleer.MainSwipeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference 
      at envoyone.ak.ace.sampleer.MainSwipeActivity.onCreate(MainSwipeActivity.java:41) 
      at android.app.Activity.performCreate(Activity.java:5937) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
04-02 17:34:17.522 3160-3172/envoyone.ak.ace.sampleer I/art﹕ Background sticky concurrent mark sweep GC freed 3145(284KB) AllocSpace objects, 0(0B) LOS objects, 33% free, 757KB/1135KB, paused 3.061ms total 194.971ms 
04-02 17:34:34.300 3160-3160/envoyone.ak.ace.sampleer I/Process﹕ Sending signal. PID: 3160 SIG: 9 

替換後actionbar= getactionbar();actionBar = getSupportActionBar();我的日誌弄成這樣

Process: envoyone.ak.ace.sampleer, PID: 3332 
    java.lang.ClassCastException: [email protected] must implement OnFragmentInteractionListener 
      at envoyone.ak.ace.sampleer.One.onAttach(One.java:84) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:905) 
      at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:844) 
      at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:859) 
      at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130) 
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1071) 
      at android.support.v4.view.ViewPager.populate(ViewPager.java:919) 
      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441) 
      at android.view.View.measure(View.java:17430) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
      at android.view.View.measure(View.java:17430) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
      at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453) 
      at android.view.View.measure(View.java:17430) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
      at android.view.View.measure(View.java:17430) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 
      at android.view.View.measure(View.java:17430) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560) 
      at android.view.View.measure(View.java:17430) 
      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001) 
      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166) 
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372) 
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054) 
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779) 
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
      at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
      at android.view.Choreographer.doFrame(Choreographer.java:550) 
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
04-02 18:03:57.438 3332-3344/envoyone.ak.ace.sampleer I/art﹕ Background partial concurrent mark sweep GC freed 744(142KB) AllocSpace objects, 0(0B) LOS objects, 53% free, 896KB/1920KB, paused 34.582ms total 117.072ms 

這裏是與One.Line84

@Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mListener = (OnFragmentInteractionListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 
+0

可以請你發佈完整的日誌跟蹤,你可以請求告訴你在哪一行得到空指針 – Keshav1234 2015-04-02 12:30:39

+0

嗨肯定與我的日誌我認爲錯誤是關係到操作欄(我認爲) – 2015-04-02 12:40:53

+0

什麼是84行? – Keshav1234 2015-04-02 12:45:53

回答

0

代碼嗨原來,錯誤是由自動生成的方法之間的衝突,我創建時引起的在Android Studio中一個新的片段

我固定這通過去除自動化方法和通過只保留

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState){ 
} 

這是唯一需要的功能。

我跟着這裏所說[http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/][1]

感謝您的幫助球員步驟。