绝对应该初始化的 TextView 的 NullPointerException [关闭]
Posted
技术标签:
【中文标题】绝对应该初始化的 TextView 的 NullPointerException [关闭]【英文标题】:NullPointerException by a TextView that definitely should be initialised [closed] 【发布时间】:2021-09-28 05:57:59 【问题描述】:几周前我开始在 android Studio 中编码。我现在想学习如何使用 RecyclerViews 并尝试实现功能。我现在面临一些必须存在但不存在的 TextView 的麻烦。代码如下:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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_
android:layout_
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_
android:layout_
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
items.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_
android:layout_>
<TextView
android:id="@+id/name"
android:layout_
android:layout_
android:layout_weight="1"
android:fontFamily="sans-serif-black"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="20dp" />
<TextView
android:id="@+id/description"
android:layout_
android:layout_
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:text="TextView"
android:textColor="@android:color/darker_gray"
android:textSize="14dp" />
<Button
android:id="@+id/button"
style="@android:style/Widget.DeviceDefault.Button"
android:layout_
android:layout_
android:layout_weight="1"
android:text="Abfrage"
app:backgroundTint="@android:color/darker_gray" />
</LinearLayout>
MainActivity.java:
package com.example.recyclerviewexercise;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
RecyclerView contactsR;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactsR = findViewById(R.id.recyclerView);
ArrayList<Contact> contacts = new ArrayList<Contact>();
for (int i = 0; i < 20; i++) contacts.add(new Contact());
contacts.add(new Contact());
contactsR.setAdapter(new ListAdapter(contacts));
contactsR.setLayoutManager(new LinearLayoutManager(this));
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>
private ArrayList<Contact> contacts;
public ListAdapter (ArrayList<Contact> contacts)
this.contacts = (ArrayList<Contact>) contacts.clone();
public class ViewHolder extends RecyclerView.ViewHolder
private TextView nameV;
private TextView descV;
private Button btn;
public ViewHolder (View itemView)
super(itemView);
nameV = itemView.findViewById(R.id.name);
descV = itemView.findViewById(R.id.description);
btn = itemView.findViewById(R.id.button);
public void setData(Contact contact)
this.nameV.setText(contact.name);
this.descV.setText(contact.description);
/*try
nameV.setText("Name");
descV.setText("contact.description");
catch(Exception e) Log.i("setData", "failed: " + e.toString());*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
@Override
public void onBindViewHolder(ListAdapter.ViewHolder holder, int position)
Log.i("bindHolder", "started " + Integer.toString(position));
try
holder.setData(contacts.get(position));
Log.i("bindHolder", "ended.");
catch (Exception e)
Log.i("bindHolder", "failed: " + e.toString());
@Override
public int getItemCount()
return contacts.size();
联系.java:
package com.example.recyclerviewexercise;
public class Contact
public String name;
public String description;
private static int counter = 1;
public Contact ()
this.name = "Person " + Integer.toString(counter);
this.description = "ist die " + Integer.toString(counter) + ". Person";
counter++;
和我开始时的日志:
2021-07-21 13:36:34.574 15222-15222/? I/zygote: Not late-enabling -Xcheck:jni (already on)
2021-07-21 13:36:34.579 15222-15222/com.example.recyclerviewexercise W/zygote: Unexpected CPU variant for X86 using defaults: x86
2021-07-21 13:36:34.668 15222-15252/com.example.recyclerviewexercise D/OpenGLRenderer: HWUI GL Pipeline
2021-07-21 13:36:34.708 15222-15252/com.example.recyclerviewexercise I/OpenGLRenderer: Initialized EGL, version 1.4
2021-07-21 13:36:34.708 15222-15252/com.example.recyclerviewexercise D/OpenGLRenderer: Swap behavior 1
2021-07-21 13:36:34.709 15222-15252/com.example.recyclerviewexercise W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-07-21 13:36:34.709 15222-15252/com.example.recyclerviewexercise D/OpenGLRenderer: Swap behavior 0
2021-07-21 13:36:34.721 15222-15252/com.example.recyclerviewexercise D/EGL_emulation: eglCreateContext: 0xa23850c0: maj 3 min 0 rcv 3
2021-07-21 13:36:34.742 15222-15252/com.example.recyclerviewexercise D/EGL_emulation: eglMakeCurrent: 0xa23850c0: ver 3 0 (tinfo 0xa2383210)
2021-07-21 13:36:34.744 15222-15252/com.example.recyclerviewexercise E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2021-07-21 13:36:34.744 15222-15252/com.example.recyclerviewexercise E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2021-07-21 13:36:34.744 15222-15252/com.example.recyclerviewexercise E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2021-07-21 13:36:34.744 15222-15252/com.example.recyclerviewexercise E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2021-07-21 13:36:34.770 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 0
2021-07-21 13:36:34.770 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.793 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 1
2021-07-21 13:36:34.793 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.809 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 2
2021-07-21 13:36:34.809 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.824 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 3
2021-07-21 13:36:34.824 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.839 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 4
2021-07-21 13:36:34.840 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.851 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 5
2021-07-21 13:36:34.851 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.856 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 6
2021-07-21 13:36:34.856 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.865 15222-15233/com.example.recyclerviewexercise I/zygote: Do partial code cache collection, code=10KB, data=20KB
2021-07-21 13:36:34.866 15222-15233/com.example.recyclerviewexercise I/zygote: After code cache collection, code=10KB, data=20KB
2021-07-21 13:36:34.866 15222-15233/com.example.recyclerviewexercise I/zygote: Increasing code cache capacity to 128KB
2021-07-21 13:36:34.867 15222-15233/com.example.recyclerviewexercise I/zygote: Do partial code cache collection, code=10KB, data=37KB
2021-07-21 13:36:34.867 15222-15233/com.example.recyclerviewexercise I/zygote: After code cache collection, code=10KB, data=37KB
2021-07-21 13:36:34.867 15222-15233/com.example.recyclerviewexercise I/zygote: Increasing code cache capacity to 256KB
2021-07-21 13:36:34.868 15222-15233/com.example.recyclerviewexercise I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2021-07-21 13:36:34.868 15222-15233/com.example.recyclerviewexercise I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2021-07-21 13:36:34.870 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 7
2021-07-21 13:36:34.870 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.886 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 8
2021-07-21 13:36:34.886 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.897 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 9
2021-07-21 13:36:34.897 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.907 15222-15233/com.example.recyclerviewexercise I/zygote: Do full code cache collection, code=86KB, data=54KB
2021-07-21 13:36:34.907 15222-15233/com.example.recyclerviewexercise I/zygote: After code cache collection, code=84KB, data=40KB
2021-07-21 13:36:34.908 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 10
2021-07-21 13:36:34.909 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.921 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 11
2021-07-21 13:36:34.921 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.933 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 12
2021-07-21 13:36:34.933 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.942 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 13
2021-07-21 13:36:34.942 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.946 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 14
2021-07-21 13:36:34.946 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.951 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 15
2021-07-21 13:36:34.951 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.968 15222-15252/com.example.recyclerviewexercise D/EGL_emulation: eglMakeCurrent: 0xa23850c0: ver 3 0 (tinfo 0xa2383210)
希望你能帮上忙。
【问题讨论】:
创建您的ListAdapter
static
,您应该会看到错误。
此代码与此日志不匹配。您从未在代码中使用标签“NameView”记录“失败:”,但它在您的日志中。如果没有准确的堆栈跟踪,很难调试崩溃。请参阅Unfortunately MyApp has stopped. How can I solve this? 获取特定于 Android 的建议,并查看 What is a stack trace, and how can I use it to debug my application errors? 获取有关在获得堆栈跟踪后该怎么做的建议。如果您仍然需要帮助,请编辑您的问题以包含完整的堆栈跟踪,以及您的代码的哪一行堆栈跟踪指向。
【参考方案1】:
您的 textview 名称在 items.xml 中,您正试图在 activity_main.xml 中找到它
尝试 nameV = (TextView)items.findViewById(R.id.name);
相同的 desV 和 btn。
【讨论】:
谢谢你,这有帮助。但现在我有另一个问题。视图从第二个人开始。我更改了 MainActivity.java 中的一些代码(在 onBindViewHolder 中),这次我发布了带有正确日志的新代码;D @SebastianRimbach 这是一个完全不同的问题。您需要为此发布一个新问题。请每个问题一个具体问题。 @SebastianRimbach 如果您之前的问题已解决,请接受答案。以上是关于绝对应该初始化的 TextView 的 NullPointerException [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
可以将TextView放在RadioGroup中。但是,这是好的做法吗?
如果要求是 Textview 应该显示最多 4 行的完整文本,那么如何设置 textview 的高度,然后它应该在 swift 3 中滚动?