2015-09-26 32 views
-2

我試圖在我的ViewPagers片段之一中使用RecyclerView來獲得交錯網格佈局。當我啓動應用程序,它會立即崩潰,並拋出這個錯誤:NullPointerException,在空對象上設置適配器

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference at xxx.HomeActivity.onCreate(HomeActivity.java:39) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)

我首先想到的是,這可能是由其中的觀點被加載,但我不知道該命令所造成的。我有一個ViewPager與2片段。我在第一個片段XML聲明的RecyclerView,像這樣:

RES /佈局/ fragment_home.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/home_grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

我的繼承人活動的相對部分:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 
    ButterKnife.bind(this); 

    setupToolbar(); 
    setupTabs(); 

    HomeGridAdapter adapter = new HomeGridAdapter(Contacts.createFakeContacts(20)); 

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.home_grid); 

    recyclerView.setAdapter(adapter); 
    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); 


} 

這裏第一個片段Java文件:

public class HomeFragment extends Fragment { 

public static final String ARG_PAGE = "ARG_PAGE"; 

private int mPage; 

public static HomeFragment newInstance(int page) { 
    Bundle args = new Bundle(); 
    args.putInt(ARG_PAGE, page); 
    HomeFragment fragment = new HomeFragment(); 
    fragment.setArguments(args); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mPage = getArguments().getInt(ARG_PAGE); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    return inflater.inflate(R.layout.fragment_home, container, false); 
} 


} 

我不認爲我需要發佈RecyclerViews適配器代碼,因爲我不認爲這是問題的一部分,但如果需要我可以。

對這個問題的任何意見,表示讚賞!

編輯:按照要求,我的繼承人activitys XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      tools:context=".HomeActivity"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:minHeight="?attr/actionBarSize" 
    android:layout_width="match_parent" 
    android:layout_height="75dp" 
    style="@style/ToolbarStyle" 
    > 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:tabMode="scrollable"/> 

</android.support.v7.widget.Toolbar> 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"/> 

</LinearLayout> 
+0

你在哪裏實例化的ViewPager?你可以發佈你的活動佈局xml嗎?看起來你的Activity沒有加載ViewPager,所以ViewPager中的RecyclerView將不會在onCreate()中存在,導致null。 – JTY

+0

添加了我的活動xml文件。 viewpager用butterknife綁定更高,'setupTabs()'方法設置TabLayout和ViewPager。我沒有包括它,因爲我不認爲它是問題的一部分。 – Orbit

回答

-1

您指定activity_home.xml佈局Activity

,而你的RecyclerView標籤是fragment_home.xml

嘗試在片段0中初始化或加RecyclerView標籤在activity_home.xml

1

您在活動

setContentView(R.layout.activity_home); 

加載activity_home和你home_grid位於first_fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/home_grid" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</RelativeLayout> 
+0

我不確定你在這裏得到什麼。 – Orbit

+0

您在活動中提到了錯誤的xml –

+0

您是否嘗試在片段中創建recyclerview?嘗試將recyclerview代碼移動到活動的片段中。 –

1

中的活動的onCreate(),先找到您的ViewPager

ViewPager pager = (ViewPager) findViewById(R.id.viewpager); 
    MyFragmentPagerAdaptor adaptor = new 
     MyFragmentPagerAdapter(getSupportFragmentManager()); 
    pager.setAdapter(adaptor); 

在活動分類中,創建一個新的FragmentPagerAdapter嵌套類,稱爲MyFragmentPagerAdapter,它返回getItem()方法中的片段。

當前,您的ViewPager沒有任何內容,並且您沒有將該片段加載到ViewPager中。 (純粹來自我看到的代碼,我可能是錯的)

既然您的ViewPager裏面有片段內容,您需要一種方法來訪問片段內的子視圖(在Viewpager內)。

這會不會是直接可能的,所以你需要創建你正在尋找使用您的適配器getItem()方法片段的一個實例:

Fragment f = (Fragment) adaptor.getItem(pager.getCurrentItem()); 

雖然getCurrentItem()可以與指數直接指向被替換您的片段,應該已經在getItem()內定義。

最後,你可以找到片段中的子視圖像這樣:

RecyclerView theViewINeed = (RecyclerView)f.getView().findViewById(R.id.home_grid); 

編輯:好像片段的OnCreateView()過程中父活動的OnCreate()不叫。

嘗試將下面的代碼移動到OnStart。 (把片段引用您onCreate()獲得作爲一個領域,並獲得它在onStart()

RecyclerView theViewINeed = (RecyclerView)f.getView().findViewById(R.id.home_grid); 
+0

感謝您的回覆。我不關心設置ViewPager,正如我在我給你的評論中所說的那樣,我已經在輔助方法-setupTabs()中做了這個。 Tabs和ViewPager工作正常。無論如何,我試着直接使用這個獲得片段:'HomeFragment f =(HomeFragment)pagerAdapter.getItem(0);'然後調用f.getView ......但它仍然拋出相同的錯誤。 – Orbit

+0

這將意味着getItem(0)返回null。也許你可以發佈適配器的代碼?我仍然認爲,雖然你的getItem()返回一個不正確的片段,也許片段本身沒有用你的xml佈局進行初始化。如果可能,請發佈片段構造函數。 – JTY

+0

嗯,我不這麼認爲。我的viewPager再一次完美地工作。我只是添加了一個快速的if/else日誌語句,並檢查了「if(pagerAdapter.getItem(0)!= null)」,它說它不是空的,所以..我不認爲這是適配器的問題。我在實例化recyclerView之後添加了一條日誌語句,以檢查它是否爲null,並且它是。所以問題是這行在我的活動'RecyclerView recyclerView =(RecyclerView)findViewById(R.id.home_grid);'無論出於何種原因'提出'recyclerView'爲空。任何想法爲什麼? – Orbit

相關問題