绑定视图时收到此错误 当我不添加任何标题时,代码工作正常 视图正在初始化,数据将显示出来 但每当我尝试添加标题时,我都会遇到此异常 我已经初始化了所有的视图,但它不能仅仅获得参考 并且突出显示的可能问题并没有解决问题,因为我在传递引用后绑定了Views 所以我在传递引用之前没有调用Views。
FATAL EXCEPTION: main
Process: com.example.shashankdc.learn, PID: 8615
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.shashankdc.learn.Medals.Medals_Adapter.onBindViewHolder(Medals_Adapter.java:62)
at com.example.shashankdc.learn.Medals.Medals_Adapter.onBindViewHolder(Medals_Adapter.java:17)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5138)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4433)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1364)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:556)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2713)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3011)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1009)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:719)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:984)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:732)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15695)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2186)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1920)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:557)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
at android.os.Handler.handleCallback(Handler.java:739)
这是我的适配器
package com.example.shashankdc.learn.Medals;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.shashankdc.learn.R;
import java.util.List;
public class Medals_Adapter extends RecyclerView.Adapter<Medals_Adapter.MyHolder> {
private static final int TYPE_HEAD = 0;
private static final int TYPE_LIST = 1;
private List<Medals_Model> mylist;
private Context context;
public Medals_Adapter(List<Medals_Model> mylist, Context context) {
this.mylist = mylist;
this.context = context;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_LIST) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.medals_card, parent, false);
return new MyHolder(v, viewType);
} else if (viewType == TYPE_HEAD) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_img, parent, false);
return new MyHolder(v, viewType);
}
return null;
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
if (holder.view_type == TYPE_LIST) {
Medals_Model model = mylist.get(position + 1);
holder.name.setText(model.getName());
holder.requirements.setText(model.getRequirements());
holder.bronze.setText(String.valueOf(model.getBronze()));
holder.silver.setText(String.valueOf(model.getSilver()));
holder.gold.setText(String.valueOf(model.getGold()));
} else if (holder.view_type == TYPE_HEAD) {
holder.header.setText("HEADER");
}
}
@Override
public int getItemCount() {
return mylist.size() + 1;
}
public void add(Medals_Model model) {
mylist.add(model);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_HEAD;
} else {
return TYPE_LIST;
}
}
public class MyHolder extends RecyclerView.ViewHolder {
public TextView name, requirements, gold, silver, bronze;
//Header
public TextView header;
int view_type;
public MyHolder(View itemView, int view_type) {
super(itemView);
if (view_type == TYPE_LIST) {
name = (TextView) itemView.findViewById(R.id.medals_title);
requirements = (TextView) itemView.findViewById(R.id.medals_requirements);
gold = (TextView) itemView.findViewById(R.id.medals_gold);
silver = (TextView) itemView.findViewById(R.id.medals_silver);
bronze = (TextView) itemView.findViewById(R.id.medals_bronze);
view_type = 1;
} else if (view_type == TYPE_HEAD) {
//Header_config
header = (TextView) itemView.findViewById(R.id.my_header);
view_type = 0;
}
}
}
}
这是我的header_img.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<android.support.v7.widget.CardView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
card_view:cardCornerRadius="3dp">
<TextView
android:layout_width="match_parent"
android:id="@+id/my_header"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
还有content_medals(Medal).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.example.shashankdc.learn.Medals.Medals"
tools:showIn="@layout/medals">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView_medals"
android:background="@drawable/linearlayour_gradient"
/>
</RelativeLayout>
答案 0 :(得分:0)
这些行正在更改参数,即shadowing字段。
view_type = 1;
view_type = 0;
请在ViewHolder构造函数中更新以限定您要设置的正确变量
public MyHolder(View itemView, int view_type) {
super(itemView);
this.view_type = view_type; // Do it here
if (view_type == TYPE_LIST) {
// Remove view_type = 1;
} else if ( ... )
// Remove view_type = 0;
您的错误是因为holder.view_type
始终为0而导致类似于此情况的错误。
if (holder.view_type == TYPE_LIST) {
Medals_Model model = mylist.get(position + 1);
// Error because holder.name is not defined for a header
holder.name.setText(model.getName());
或者,使用getItemViewType(position)
而不是依赖holder.view_type