片段中的android.support.v7.widget.RecyclerView.onMeasure中的java.lang.NullPointerException

时间:2015-02-28 21:51:01

标签: android-fragments android-recyclerview

我正在尝试在片段中创建一个RecyclerView,但我一直在android.support.v7.widget.RecyclerView.onMeasure上获取java.lang.NullPointerException。我仔细查看了其他帖子,然后尝试了所有提示。但是我一直得到同样的错误。这是我的代码。任何帮助将不胜感激。

错误:

java.lang.NullPointerException
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
        at android.view.View.measure(View.java:17633)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5412)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17633)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5412)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5412)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5412)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5412)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5412)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5412)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2588)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2347)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1439)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1653)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1297)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6773)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
        at android.view.Choreographer.doCallbacks(Choreographer.java:613)
        at android.view.Choreographer.doFrame(Choreographer.java:583)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5635)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)

片段:

public class SelectTeamFragment extends Fragment implements AdapterView.OnClickListener {

    private RecyclerView listNFLTeams;
    RecyclerView.LayoutManager layoutManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_select_team, container, false);
        // 1. get a reference to recyclerView
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(true);
        // 2. set layoutManger
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        // this is data for recycler view
        ItemData itemsData[] = { new ItemData("Help",R.drawable.ic_launcher),
                new ItemData("Denver Broncos",R.drawable.ic_launcher),
                new ItemData("Atlantic Falcons",R.drawable.ic_launcher),
                new ItemData("Detroit Lions",R.drawable.ic_launcher),
                new ItemData("Seattle SeaHawks",R.drawable.ic_launcher),
                new ItemData("Miami Dolphins",R.drawable.ic_launcher)};

        // 3. create an adapter
        AdapterTeams mAdapter = new AdapterTeams(itemsData);
        // 4. set adapter
        recyclerView.setAdapter(mAdapter);
        // 5. set item animator to DefaultAnimator
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        return view;
    }

    @Override
    public void onClick(View v) {

    }
}

适配器:

public MyAdapter(ItemData[] itemsData) {
        this.itemsData = itemsData;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_select_team, parent, false);

        // create ViewHolder

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        // - get data from your itemsData at this position
        // - replace the contents of the view with that itemsData

        viewHolder.Title.setText(itemsData[position].gettitle());
        viewHolder.Icon.setImageResource(itemsData[position].getimage());


    }

    // inner class to hold a reference to each item of RecyclerView
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView Title;
        public ImageView Icon;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            Title = (TextView) itemLayoutView.findViewById(R.id.teamName);
            Icon = (ImageView) itemLayoutView.findViewById(R.id.teamLogo);
        }
    }


    // Return the size of your itemsData (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return itemsData.length;
    }
}

itemdata:和

public class ItemData {


    private String title;
    private int image;

    public ItemData(String title,int image){

        this.title = title;
        this.image = image;
    }

    public String gettitle() {
        return title;
    }

    public int getimage() {
        return image;
    }
    // getters & setters
}

Fragment_select_team.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="loserpool.nghiadang.loserpool.SelectTeamFragment">

    <!-- A RecyclerView with some commonly used attributes -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

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

</RelativeLayout>

custom_select_team.xml:

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/teamLogo"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NFL Team"
        android:id="@+id/teamName"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="24dp"/>

</RelativeLayout>

0 个答案:

没有答案