2013-09-04 51 views
5

Tôi đang cố thiết lập FragmentStatePagerAdapter để tạo giao diện tab có thể cuộn trên một trong các đoạn của tôi tương tự như this example. Trong hoạt động của tôi onCreate:NullPointerException khi gọi hàm findViewById() để xem Fragment's View

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.base); 

    setVersionString(); 
    setupActionBar(); 
    setupNavigationDrawer(); 
    setupFragments(); 
} 

tôi đặt xem nội dung đến cách bố trí sau (R.layout.base):

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".BaseActivity" > 

    <!-- The main content view --> 
    <FrameLayout 
     android:id="@+id/main_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <!-- The navigation drawer --> 
    <ListView 
     android:id="@+id/nav_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#FFF" /> 

</android.support.v4.widget.DrawerLayout> 

sau đó tôi gọi hàm sau:

private void setupFragments() { 
    // Check that the activity is using the correct layout 
    if (findViewById(R.id.main_frame) != null) { 
     // Initialize the first fragment 
     MainFragment firstFrag = new MainFragment(); 

     // Pass any extras from an intent to the Fragment 
     firstFrag.setArguments(getIntent().getExtras()); 

     // Add the first Fragment to the screen 
     getSupportFragmentManager().beginTransaction() 
      .add(R.id.main_frame, firstFrag).commit(); 

     tabsAdapter = firstFrag.getAdapter(); 
     Log.d("Base.LOG", "tabsAdapter = " + tabsAdapter); 
     tabsAdapter.addTab(DummyFragment.class, null); 
    } 
} 

Trong một nỗ lực để đặt FragmentStatePagerAdapter làm bộ điều hợp cho chế độ xem trong bố cục của đoạn (R.layout.main):

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_page" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainFragment" > 

    <android.support.v4.view.PagerTitleStrip 
     android:id="@+id/pager_title_strip" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:background="#33b5e5" 
     android:textColor="#fff" 
     android:paddingTop="4dp" 
     android:paddingBottom="4dp" /> 
</android.support.v4.view.ViewPager> 

Và đoạn mã của tôi trông dối này:

lực
private View mView; 
private TabbedPagerAdapter mTabPageAdapter; 
private ViewPager mTabPager; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    Log.d("Main.LOG", "onCreate called"); 
    mView = inflater.inflate(R.layout.main, container, false); 
    Log.d("Main.LOG", "Layout inflated"); 
    setupTabs(); 
    Log.d("Main.LOG", "Tabs setup"); 

    return mView; 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    Log.d("Main.LOG", "onResume called"); 
} 

/** Set up the ViewPager and Adapter to handle the primary tabs */ 
private void setupTabs() { 
    // Initialize the adapter 
    mTabPageAdapter = new TabbedPagerAdapter(getActivity(), 
      getActivity().getSupportFragmentManager()); 

    // Initialize the view pager 
    mTabPager = (ViewPager) mView.findViewById(R.id.main_page); 
    Log.d("Main.LOG", "mTabPager = " + mTabPager); 
    Log.d("Main.LOG", "mTabPageAdapter = " + mTabPageAdapter); 
    mTabPager.setAdapter(mTabPageAdapter); 
} 

/* Accessor for TabbedPagerAdapter */ 
public TabbedPagerAdapter getAdapter() { 
    return mTabPageAdapter; 
} 

Ứng dụng đóng khi tôi cố gắng thêm một tab với một NullPointerException. Các thông điệp debug trong chương trình ghi rằng:

D/Base.LOG (27.173): tabsAdapter = null

được hiển thị trước khi bất kỳ các thông điệp debug của đoạn. Làm thế nào tôi có thể đảm bảo rằng bố cục được tăng cao trước khi cố truy cập vào bất kỳ Chế độ xem hoặc lớp con nào?

+0

Bạn có thể hiển thị xml cho R.layout.main không? cũng nếu bạn muốn biết nếu một cái gì đó được thực hiện lạm phát, chỉ cần đặt một onResume trong đoạn, và kiểm tra xem lạm phát được thực hiện, bởi vì onResume sẽ luôn luôn được gọi sau khi onCreateView – Raigex

+0

@Raigex Đó là thứ hai. Tôi đã chỉnh sửa để bao gồm cái nào. Bạn đề nghị tôi làm gì trong phương thức 'onResume'? –

+0

Không có gì thực sự, nhưng bạn có thể sử dụng phương pháp tiếp tục trên để xem nếu đã được thổi phồng (ví dụ như fragView! = Null) thì bạn có thể đăng nhập và thu hẹp vấn đề. – Raigex

Trả lời

2

Theo nhận xét của tôi

public class MainFragment extends Fragment 
{ 
    private MainInterface mInterface = null; 
    //make sure to set the above somehow 

    public interface MainInterface{ 
     public void onFragmentReady(.....); 
    } 

    ... //Do regular stuff here 

    public void onResume() 
    { 
     //Do what you need to do in onResume 
     if(mInterface != null) 
     { 
      mInteface.onFragmentReady(); 
     } 
    } 
} 

này sẽ cho phép bạn biết khi phân đoạn sẵn sàng và bạn có thể làm getTabAdapter của bạn gọi sau đó.

1

Bạn có đang gọi setContentView(...) bên trong phương thức hoạt động onCreate(...) trước khi gọi setupFragments(...)?

Nếu có: Bố cục bạn đang gán cho Hoạt động có chứa ViewPager không?

Tôi đề nghị: Vì bạn đang sử dụng một rõ ràng ViewPager bên trong một Fragment, thực hiện khởi của ViewPager bên trong mảnh vỡ onCreateView(...) phương pháp.

Ví dụ:

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

    // ASSUMING your layout.main contains the ViewPager 
    View v = inflater.inflate(R.layout.main, container, false); 

    ViewPager pager = (ViewPager) v.findViewById(R.id.pager); 
    // and so on ... 

    return v; 
} 
+0

Không, tôi không, xem chỉnh sửa. Tôi nghĩ nó được gọi bởi nhà xây dựng. Tôi có cần tự gọi phương pháp này không? Tôi đã nghĩ về việc khởi tạo 'ViewPager' bên trong đoạn. Nhưng tôi muốn có thể thêm các tab từ hoạt động chính. Làm thế nào tôi sẽ nhận được bộ điều hợp cho phương thức 'onCreateView'? –

+0

Bạn cũng có thể quản lý các tab từ Phân đoạn. Ý bạn là làm thế nào để lấy Adapter? Cũng giống như cách bạn làm trong Hoạt động. Cũng lưu ý rằng lớp Fragment có phương thức getActivity(). –

+0

Tôi đã suy nghĩ ban đầu rằng tôi sẽ phải vượt qua các mảnh tab từ hoạt động chính đến đoạn chính đến bộ điều hợp và điều đó dường như rườm rà. Suy nghĩ mới của tôi là tạo phương thức 'getAdapter' để trả lại bộ điều hợp cho hoạt động chính để tôi có thể thêm các tab từ đó. Nhưng hãy xem bản chỉnh sửa của tôi. Tôi đã đưa ra lời khuyên của bạn và thêm một số thông báo gỡ lỗi, theo đề xuất của Raigex, vào nhật ký và có vẻ như nếu 'onCreateView' không bao giờ được gọi cả. –

Các vấn đề liên quan