我正在尝试制作RecyclerView,我遇到了这个奇怪的错误。
以下是信息:
android.view.InflateException: Binary XML file line #0: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:761)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.example.eddie.songs3.AdapterRegularItem.onCreateViewHolder(AdapterRegularItem.java:34)
at com.example.eddie.songs3.AdapterRegularItem.onCreateViewHolder(AdapterRegularItem.java:15)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
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:17745)
at android.view.ViewGroup.layout(ViewGroup.java:5645)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
at android.view.View.layout(View.java:17745)
at android.view.ViewGroup.layout(ViewGroup.java:5645)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17745)
at android.view.ViewGroup.layout(ViewGroup.java:5645)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17745)
at android.view.ViewGroup.layout(ViewGroup.java:5645)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1172)
at android.view.View.layout(View.java:17745)
at android.view.ViewGroup.layout(ViewGroup.java:5645)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17745)
at android.view.ViewGroup.layout(ViewGroup.java:5645)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17745)
at android.view.ViewGroup.layout(ViewGroup.java:5645)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17745)
at android.view.ViewGroup.layout(ViewGroup.java:5645)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
E/AndroidRuntime: at android.view.View.layout(View.java:17745)
at android.view.ViewGroup.layout(ViewGroup.java:5645)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:759)
at android.view.View.layout(View.java:17745)
at android.view.ViewGroup.layout(ViewGroup.java:5645)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2491)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2206)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1348)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6858)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:887)
at android.view.Choreographer.doCallbacks(Choreographer.java:699)
at android.view.Choreographer.doFrame(Choreographer.java:634)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:873)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6351)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
Application terminated.
这是我的SearchActivity
。这是显示activity
RecyclerView
NavigationView navView;
boolean startedBySplashAct = false;
RecyclerView recyclerView;
ArrayList<RegularItem> regularItemList;
AdapterRegularItem arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//see if this activity was started by splash activity
Intent intent = getIntent();
String intentStr = intent.getStringExtra("parent_activity");
if(intentStr != null && intentStr.equals("splash"))
startedBySplashAct = true;
navView = findViewById(R.id.nav_view);
regularItemList = new ArrayList<>();
arrayAdapter = new AdapterRegularItem(R.layout.regular_item_layout, regularItemList, SearchActivityReg.this);
//set the corresponding item checked
navView.getMenu().getItem(0).setChecked(true);
//set needed properties for recuclerView
recyclerView = findViewById(R.id.RecyclerViewReg);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(arrayAdapter);
String number = "67";
String line = "I have decided, I am gonna fight it";
RegularItem item = new RegularItem(number, line);
regularItemList.add(item);
arrayAdapter = new AdapterRegularItem(R.layout.regular_item_layout, regularItemList, SearchActivityReg.this);
recyclerView.setAdapter(arrayAdapter);
}
我没有展示其余部分,因为它与问题无关。
这是错误所在的ArrayAdapter
。
package com.example.eddie.songs3;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class AdapterRegularItem extends RecyclerView.Adapter<AdapterRegularItem.ViewHolder>
{
int listItemLayout;
ArrayList<RegularItem> itemsList;
static Context c;
public AdapterRegularItem(int layoutID, ArrayList<RegularItem> itemList,
Context c)
{
this.listItemLayout = layoutID;
this.itemsList = itemList;
this.c = c;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
//this is where the error is
.inflate(
R.layout.regular_item_layout,
parent,
false);
ViewHolder myViewHolder = new ViewHolder(v);
return myViewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
TextView number = holder.number;
TextView firstLine = holder.firstLine;
number.setText(itemsList.get(position).getNumber());
firstLine.setText(itemsList.get(position).getFirstLine());
}
@Override
public int getItemCount() {
return itemsList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener
{
public TextView number;
public TextView firstLine;
public ViewHolder(View itemView)
{
super(itemView);
itemView.setOnClickListener(this);
number = itemView.findViewById(R.id.txtNumber);
firstLine = itemView.findViewById(R.id.txtFirstLine);
}
@Override
public void onClick(View view)
{
}
}
}
这里是RegualrItem.java
package com.example.eddie.songs3;
public class RegularItem
{ String number;
String firstLine;
public RegularItem(String number, String firstLine)
{
this.firstLine = firstLine;
this.number = number;
}
public String getNumber()
{
return number;
}
public String getFirstLine()
{
return firstLine;
}
}
regular_item_layout.xml这是列表中每个项目的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="120"
android:layout_marginStart="16dp"
android:textColor="@color/textItemNumber"
android:textSize="16sp"/>
<TextView
android:id="@+id/txtFirstLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginStart="72dp"
android:text="Just testing to see"
android:textSize="16sp"
android:textColor="@color/textColorMain"
android:layout_marginEnd="16dp"/>
<view
android:id="@+id/devider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/txtNumber"/>
</RelativeLayout>
我认为我必须展示,如果还有其他内容,请告诉我。 问题是我从之前制作的应用程序中复制了这个,并且它在那里工作得很好,所以我甚至不知道在代码中找到问题的位置。不要投票,只要你需要其他任何东西就告诉我。提前致谢。
答案 0 :(得分:3)
在您的布局中,您有以下标记:
<view android:id="@+id/devider" android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@id/txtNumber"/>
将view
更改为View
(大写V
)。