2017-06-14 29 views
0

問題: 在我的主要活動onCreate()中,我試圖在ViewPager顯示的片段中設置TextView的文本值。當我嘗試獲取我的片段實例時,它始終返回null。當從ViewPagerAdapter訪問時,Android片段始終爲NULL

我試過的解決方案: 幾乎所有與這個問題有關的解決方案都給了我相同的結果(NPE)。

我的代碼如下,以及一些解釋...

Home.java活動(我的主要活動):

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Show the home layout 
     setContentView(R.layout.home); 

     // Setup the toolbar 
     Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     // Now setup our tab bar 
     TabLayout tabLayout = (TabLayout)findViewById(R.id.homeTabs); 
     tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

     // Setup the view pager 
     ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager); 
     PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager()); 
     viewPager.setAdapter(pagerAdapter); 
     tabLayout.setupWithViewPager(viewPager); 

     // Start our service controller 
     Intent startServiceController = new Intent(this, ServiceController.class); 
     startService(startServiceController); 

     Fragment viewFragment = pagerAdapter.getRegisteredFragment(viewPager.getCurrentItem()); 

     if(viewFragment == null){ 
      Log.v("Fragment", "NULL"); 
     } 
    } 

建立pageAdapter和viewpager後,我嘗試訪問顯示片段。我PagerAdapter的代碼如下...

PagerAdapter:

public class PagerAdapter extends SmartFragmentStatePagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: 
       return new ParkListFragment(); 
      case 1: 
       return new MapFragment(); 
      default: 
       return null; 
     } 
    } 

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

    @Override 
    public CharSequence getPageTitle(int position) { 
     CharSequence title; 

     switch (position){ 
      case 0: 
       title = "Spaces Near You"; 
       break; 
      case 1: 
       title = "Map"; 
       break; 
      default: 
       title = "N/A"; 
       break; 
     } 

     return title; 
    } 
} 

SmartFragmentStatePagerAdapter:

public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter { 
    // Sparse array to keep track of registered fragments in memory 
    private SparseArray<Fragment> registeredFragments = new SparseArray<>(); 

    public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) { 
     super(fragmentManager); 
    } 

    // Register the fragment when the item is instantiated 
    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     Fragment fragment = (Fragment) super.instantiateItem(container, position); 
     registeredFragments.put(position, fragment); 
     return fragment; 
    } 

    // Unregister when the item is inactive 
    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     registeredFragments.remove(position); 
     super.destroyItem(container, position, object); 
    } 

    // Returns the fragment for the position (if instantiated) 
    public Fragment getRegisteredFragment(int position) { 
     return registeredFragments.get(position); 
    } 
} 

我真的不明白是什麼問題。我嘗試了很多東西。我不想用的就是這個:

String tag="android:switcher:" + viewId + ":" + index; 

ParkListFragment.java:

public class ParkListFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.park_list_fragment, container, false); 
    } 
} 

ParkList片段佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_centerInParent="true"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/confidenceText" 
       android:textAlignment="center" 
       android:textSize="24sp" 
       android:textColor="@android:color/primary_text_light" 
       android:padding="10dp"/> 

     </LinearLayout> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_centerInParent="true"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/activityText" 
       android:textAlignment="center" 
       android:textSize="24sp" 
       android:textColor="@android:color/primary_text_light" 
       android:padding="10dp"/> 

     </LinearLayout> 

    </RelativeLayout> 

</LinearLayout> 

堆棧跟蹤:Pastebin

+0

添加您的堆棧跟蹤以及請 –

+0

@ArunShankar我做了一個鏈接到引擎收錄的編輯。 –

+0

您從logact中發現的異常,但是整個異常在哪裏? – Yupi

回答