gpt4 book ai didi

java - ArrayAdapter 中 onCreateViewHolder 中的 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 08:29:29 25 4
gpt4 key购买 nike

我正在尝试制作 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。这是显示 RecyclerView

activity
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>

我想我要展示的还不止这些,如果还有其他的,请告诉我。问题是我是从我以前制作的应用程序中复制它的,它在那里工作得很好,所以我什至不知道在代码中的什么地方寻找问题。

最佳答案

在你的布局中,你有这个标签:

<view
android:id="@+id/devider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/txtNumber"/>

view 更改为 View(大写 V)。

关于java - ArrayAdapter 中 onCreateViewHolder 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49501004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com