我试图使用类系统制作一个todo应用程序,但我遇到了这个错误



错误:"尝试在null对象引用上调用虚拟方法"void android.widget.LinearLayout.addView(android.view.view(";

那么,如果有人帮我,我该怎么办呢?我会非常感激的这是我的代码

cardbg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="@color/purple_500"
android:id="@+id/cardbg">
</LinearLayout>

activity_ymain.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns: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"
tools:context=".MainActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical" />
</androidx.core.widget.NestedScrollView>

<androidx.constraintlayout.widget.constraintlayout>

主要活动.java

package com.thehuman.todo;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = findViewById(R.id.main);
card first = new card(MainActivity.this);
main.addView(first);
}

}

class card extends LinearLayout{
card self;
LinearLayout bg,main;
Button clon,delete;
@SuppressLint("ResourceAsColor")
public card(Context context) {
super(context);
self=this;
main = findViewById(R.id.main);
bg = findViewById(R.id.cardbg);
clon = new Button(context);
clon.setBackgroundColor(R.color.white);
clon.setPadding(20,20,20,20);
clon.setTextColor(R.color.teal_200);
clon.setText("clon me");
delete = new Button(context);
delete.setBackgroundColor(R.color.black);
delete.setTextColor(R.color.teal_200);
delete.setPadding(20,20,20,20);
delete.setText("delete me");
bg.addView(clon);
bg.addView(delete);
clon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
card cloncard = new card(context);
main.addView(cloncard);
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
main.removeView(self);
}
});
}

}

我想您在class Card中得到了null object reference错误。因为您使用的是bg = findViewById(R.id.cardbg);。使用findViewById找不到cardbg.xml,因为cardbg.xml尚未附加到活动布局中。您必须使用充气器自行对xml进行充气。

使用

bg = LayoutInflater.from(context).inflate(R.layout.cardbg, null).findViewById(R.id.cardbg);

最新更新