我正在使用TabLayout和RecyclerView创建一个类似应用程序的联系人,但是在我的代码中的某个地方得到了空对象引用,我只是想知道在哪里修补内容,下面是收到错误的文件:
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.ImageView;
import android.widget.TextView;
import java.util.List;
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context context;
private List<Contact> data;
RecyclerViewAdapter(Context context, List<Contact> data) {
this.context = context;
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(context).inflate(R.layout.contact_fragment,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//logcat error pointing below line
holder.name_tv.setText(data.get(position).getName());
holder.phone_tv.setText(data.get(position).getPhone());
holder.photo_iv.setImageResource(data.get(position).getPhoto());
}
@Override
public int getItemCount() {
return data.size();
}
static class MyViewHolder extends RecyclerView.ViewHolder{
private TextView name_tv;
private TextView phone_tv;
private ImageView photo_iv;
MyViewHolder(View itemView) {
super(itemView);
name_tv = itemView.findViewById(R.id.name_contact);
phone_tv = itemView.findViewById(R.id.contact_number);
photo_iv = itemView.findViewById(R.id.image_contact);
}
}
}
以下是整个调试日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vijay.recyclerview, PID: 2059
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.vijay.recyclerview.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:32)
at com.example.vijay.recyclerview.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:13)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1767)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:3172)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2759)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2460)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1528)
at android.view.ViewRootImpl$TraversalRunnable.
I/Process: Sending signal. PID: 2059 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport:
'socket'
这是contact_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/backgroundFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_contact_frag"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
和item_contact.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_contact"
android:src="@drawable/ic_child_care"
android:layout_width="56dp"
android:layout_height="56dp" />
<LinearLayout
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:text="Contact Name"
android:id="@+id/name_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/contact_number"
android:text="Phone Number Here"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_gravity="center_vertical"
android:src="@drawable/ic_call_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
告诉我是否必须在此处粘贴更多文件。 我总是得到这些类型的例外,如果你们帮助我找到这个,那对我来说将是更大的帮助!谢谢。
答案 0 :(得分:0)
将context
更改为ViewGroup Context
,如下所示
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_fragment,parent,false);
return new MyViewHolder(view);
}
答案 1 :(得分:0)
在我看来,这个问题正在发生,因为&#34; sc.setLogLevel("OFF")
&#34;一片空白。因此,您在第32行的空对象上调用holder.phone_tv
请确保setText(...)
布局中存在ID为contact_number
的视图。
我还怀疑您没有使用回收商项目持有人的正确布局视图。如果是这种情况,请将contact_fragment.xml
更改为包含您希望在适配器中显示的行项的特定布局。
您可以通过编辑此代码块来更改它
contact_fragment.xml
答案 2 :(得分:0)
您没有初始化列表
private List<Contact> data= new ArrayList<>();
或
检查您的布局文件中是否存在与您在findviewbyid中传递的名称相同的所有小部件
view = LayoutInflater.from(context).inflate(R.layout.item_contact,parent,false);