Issue
error : "Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference"
so what should ı do to fix it if someone help me ı'll be so gratefull here is my codes
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>
activit_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>
MainActivity.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);
}
});
}
}
Solution
I suppose you are getting null object reference
error in class Card
. Because you are using bg = findViewById(R.id.cardbg);
. You can not find cardbg.xml using findViewById
because cardbg.xml have not been attached to you activity layout. You have to inflate your xml by yourself using an inflater.
Use
bg = LayoutInflater.from(context).inflate(R.layout.cardbg, null).findViewById(R.id.cardbg);
Answered By - Nilupul Sandeepa
Answer Checked By - David Goodson (JavaFixing Volunteer)