我将如何使用函数中的代码。什么阻止我从函数调用我的回收器视图? 这是我得到的错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
提供错误的函数。
void PopulateList(View view)
{
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
final LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//mLayoutManager.isSmoothScrollbarEnabled();
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerViewAdapter(EventsData);
mRecyclerView.setAdapter(mAdapter);
}
RecyclerViewAdapter Class
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Context;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import jp.wasabeef.picasso.transformations.CropCircleTransformation;
/**
* Created by on 9/23/2015.
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<EventsListData> EventsData;
Context context;
public static class ViewHolder extends RecyclerView.ViewHolder
{
public View view;
public ViewHolder(View v)
{
super(v);
view = v;
}
}
public RecyclerViewAdapter(ArrayList<EventsListData> EventsData)
{this.EventsData = EventsData;}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//ceate a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.events_list_layout, parent, false);
//set the view's size, margins, padding and layout paremters;
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get elements from your dataset at this position
// - replace the contenst of the view with that elements
}
public void setData(int Maxposition)
{
int position=0;
TextView txtDisc = (TextView) vh.view.findViewById(R.id.EventDisc);
TextView txtDistance = (TextView) vh.view.findViewById(R.id.EventsDistance);
FrameLayout Frame = (FrameLayout) vh.view.findViewById(R.id.EventsFrame);
ImageView imgView = (ImageView) vh.view.findViewById(R.id.EventsImageView);
for (int i = 0 ; i <Maxposition ; i++) {
Picasso.with(this.context).load("file://" + EventsData.get(position).art_work_uri)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.centerInside()
.transform(new CropCircleTransformation())
//.resize(200, 200)
.into(imgView);
txtDisc.setText(EventsData.get(position).description);
txtDistance.setText(Float.toString(EventsData.get(position).Distance) + " KM");
Frame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.d("XXX","RecyclerView position =");
}
});
}
position++;
}
@Override
public int getItemCount() { return EventsData.size(); }
}
适配器使用的布局。
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.30"
android:id="@+id/Frame">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.98"
android:id="@+id/EventsFrame">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/EventDisc"
android:layout_gravity="center_vertical"
android:text="random test text" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:paddingLeft="15dp">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/EventsBlip"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:src="@drawable/point" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="KM"
android:id="@+id/EventsDistance"
android:layout_gravity="center_vertical"
android:layout_weight="0.9"
android:paddingLeft="10dp"
android:layout_marginRight="15dp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.02"
android:background="#888888">
</FrameLayout>
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/EventsImageView" />
</FrameLayout>
按要求提供LogCat ......
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
at android.support.v7.widget.RecyclerView.computeVerticalScrollRange(RecyclerView.java:1545)
at android.view.View.onDrawScrollBars(View.java:12866)
at android.view.View.draw(View.java:15154)
at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3037)
at android.view.View.updateDisplayListIfDirty(View.java:14082)
at android.view.View.getDisplayList(View.java:14105)
at android.view.View.draw(View.java:14872)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14077)
at android.view.View.getDisplayList(View.java:14105)
at android.view.View.draw(View.java:14872)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14077)
at android.view.View.getDisplayList(View.java:14105)
at android.view.View.draw(View.java:14872)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14077)
at android.view.View.getDisplayList(View.java:14105)
at android.view.View.draw(View.java:14872)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14077)
at android.view.View.getDisplayList(View.java:14105)
at android.view.View.draw(View.java:14872)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14077)
at android.view.View.getDisplayList(View.java:14105)
at android.view.View.draw(View.java:14872)
at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15151)
at android.widget.FrameLayout.draw(FrameLayout.java:592)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2657)
at android.view.View.updateDisplayListIfDirty(View.java:14082)
at android.view.View.getDisplayList(View.java:14105)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:266)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:272)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:311)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2528)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2366)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1997)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5845)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5274)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
请注意! 我注意到如果我注释掉与RecyclerView有关的一切,它仍然会发生。所以我在布局XML中注释掉它并且它有效。
< android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
所以我在我的RecyclerViewAdapter类中添加了一个函数,但不太确定它是否可行。因为我实际上无法从我的对象中调用它而我无法弄清楚为什么?所以我真的不知道这是否有效。为什么我打电话给它?
public void setData(int Maxposition)
{
int position=0;
TextView txtDisc = (TextView) vh.view.findViewById(R.id.EventDisc);
TextView txtDistance = (TextView) vh.view.findViewById(R.id.EventsDistance);
FrameLayout Frame = (FrameLayout) vh.view.findViewById(R.id.EventsFrame);
ImageView imgView = (ImageView) vh.view.findViewById(R.id.EventsImageView);
for (int i = 0 ; i <Maxposition ; i++) {
Picasso.with(this.context).load("file://" + EventsData.get(position).art_work_uri)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.centerInside()
.transform(new CropCircleTransformation())
//.resize(200, 200)
.into(imgView);
txtDisc.setText(EventsData.get(position).description);
txtDistance.setText(Float.toString(EventsData.get(position).Distance) + " KM");
Frame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.d("XXX","RecyclerView position =");
}
});
}
position++;
}
答案 0 :(得分:0)
好像你打电话给PopulateList
太晚了(或者根本没有)。确保在创建视图后尽快调用它,因此如果您使用的是Activity
,则必须在PopulateList()
中调用onCreate
,如果您使用的是片段,则必须在onCreateView
中调用。
您不必立即设置适配器/数据,但必须在RecyclerView
显示之前设置布局管理器。