android.support.v7.widget.RecyclerView.onMeasure中的java.lang.NullPointerException,交错网格布局

时间:2015-10-12 15:43:14

标签: java android android-layout

htis null指针异常的原因是什么,在我的代码中初始化交错网格布局时抛出。我无法理解原因。可以帮助我吗?

logcat的

FATAL EXCEPTION: main
E/AndroidRuntime: java.lang.NullPointerException
E/AndroidRuntime:     at  android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
E/AndroidRuntime:     at android.view.View.measure(View.java:15504)
E/AndroidRuntime:     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
E/AndroidRuntime:     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
E/AndroidRuntime:     at android.view.View.measure(View.java:15504)
E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime:     at android.view.View.measure(View.java:15504)
E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime:     at android.view.View.measure(View.java:15504)
E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:13910-12                     E/AndroidRuntime:           at   

android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
E/AndroidRuntime:     at       android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime:     at android.view.View.measure(View.java:15504)
E/AndroidRuntime:     at 

交错的网格布局

 public void stagGridMethod(int i,int j)
{

    try {


        setTitle(null);
        Toolbar topToolBar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(topToolBar);
        topToolBar.setLogo(R.drawable.ic_launcher);
        topToolBar.setLogoDescription(getResources().getString(R.string.logo_desc));

        // List<ItemObject> rowListItem = getAllItemList();
        lLayout = new GridLayoutManager(MainActivity.this, 1);

        RecyclerView rView = (RecyclerView) findViewById(R.id.recycler_view);
        rView.setHasFixedSize(true);


        StaggeredGridLayoutManager stagGrid = new StaggeredGridLayoutManager(i, j);
        rView.setLayoutManager(stagGrid);
       // StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.HORIZONTAL);
        RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, allItems);

        rView.setAdapter(rcAdapter);
    }
    catch (Exception e){
        Log.e(e.getClass().getName(), e.getMessage(),e);
    }
}

main.xml中

<LinearLayout 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"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="@color/color_primary"
    android:minHeight="?attr/actionBarSize" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    tools:context=".MainActivity">

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

</RelativeLayout>

cardview.xml

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="220dp"
android:layout_height="180dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="8dp"
android:layout_marginBottom="16dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/recipe_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/action_settings"
        android:src="@drawable/three"

        android:scaleType="centerCrop" />

    <ImageView
        android:id="@+id/outside_imageview"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"

        android:src="@drawable/varified"
        android:layout_alignBottom="@id/recipe_image"

        android:layout_alignRight="@id/recipe_image"
        android:scaleType="fitEnd" />
</RelativeLayout>

Recyclerview适配器

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

private ArrayList<String> itemList;
private List<ItemObject> imageList;
private Context context;
ImageLoader imageLoader;

public RecyclerViewAdapter(Context context, ArrayList<String> itemList) {
    this.itemList = itemList;
    this.context = context;


    imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
            .cacheOnDisc(true).resetViewBeforeLoading(true).build();
    //ASYNCTASK EXECUTION

    // .showImageForEmptyUri(fallback)
    //  .showImageOnFail(fallback)
    // .showImageOnLoading(fallback).build();

    //imageList =  imageLoader.displayImage(itemList,);
}

@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
    RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
    return rcv;
}

@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {



    imageLoader.displayImage(itemList.get(position),holder.recipeImage);

}

@Override
public int getItemCount() {
    return this.itemList.size();
}

}

2 个答案:

答案 0 :(得分:1)

在onCreate方法中定义这些并启动适配器,在其中分配&#34; allitems&#34;的值,希望这将解决您的问题。

RecyclerView rView = (RecyclerView) findViewById(R.id.recycler_view);
    rView.setHasFixedSize(true);
  StaggeredGridLayoutManager staggeredGridLayoutManager = new  StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.HORIZONTAL);
rView.setLayoutManager(stagGrid);

答案 1 :(得分:0)