-1

我正在尝试在我的活动中创建 RecycleView,但应用程序在启动时崩溃并且日志显示

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                   at com.punk.home.snoopwatches.MainActivity.onCreate(MainActivity.java:55)

我不能整天解决这个问题,所以我希望你能我的课程:MainActivity.class:

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imagesIdList=new ArrayList<>();
    imagesIdList.add(R.drawable.palm);
    imagesIdList.add(R.drawable.aliens);
    imagesIdList.add(R.drawable.palm);
    imagesIdList.add(R.drawable.aliens);
    imagesIdList.add(R.drawable.palm);
    imagesIdList.add(R.drawable.aliens);
    imagesIdList.add(R.drawable.palm);
    imagesIdList.add(R.drawable.aliens);

    final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    layoutManager.generateDefaultLayoutParams();
    recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view);
    recyclerView.setLayoutManager(layoutManager);
    //        recyclerView.setHasFixedSize(true);
    adapter=new ImageAdapter(MainActivity.this, imagesIdList);
    recyclerView.setAdapter(adapter);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(this);

}
RecyclerView recyclerView;
ArrayList<Integer> removedItems;
ArrayList<Integer> imagesIdList;
ImageAdapter adapter;

ImageAdapter.class

  ImageView imageView;

    public ImageViewHolder(View itemView) {
        super(itemView);
        imageView = (ImageView) itemView.findViewById(R.id.image);
    }
}

List<Integer> imageId;

ImageAdapter(Context context, List<Integer> imageId) {
    this.imageId = imageId;
    this.context = context;
}

Context context;

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_image_activity, viewGroup, false);
    ImageViewHolder ivh = new ImageViewHolder(view);
    return ivh;
}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    holder.imageView.setImageResource(imageId.get(position));
}


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

和 content_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.punk.home.snoopwatches.MainActivity"
    tools:showIn="@layout/activity_main">

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

</RelativeLayout>

item_image_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    tools:showIn="@layout/content_main"
    android:id="@+id/item_layout">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image"/>

</LinearLayout>

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">


</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/add" />

4

2 回答 2

0

RecyclerView setLayoutManager 期望 RecyclerView.LayoutManager 类型作为输入所以改变

final LinearLayoutManager layoutManager = new LinearLayoutManager(this);

final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);

并删除

layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.generateDefaultLayoutParams();

查看开发人员页面以了解正确使用 http://developer.android.com/training/material/lists-cards.html

于 2016-03-23T23:37:21.380 回答
0

如果要使用并且不让它返回 null,则需要放入一个部分<include>contnent_main.xmlactivity_main.xmlfindViewById

类似于<include layout="@layout/contnent_main"/>AppBarLayout 下面的东西。

于 2016-03-24T13:52:22.410 回答