MainActivity of my code
package com.example.recycleview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<ContactModel>arrContact= new ArrayList<>();
RecyclerContactAdapter adapter;
RecyclerView recyclerView;
FloatingActionButton btnOpndlg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= findViewById(R.id.recycleContact);
btnOpndlg = findViewById(R.id.btnopndlg);
btnOpndlg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.add_update);
EditText edtname= dialog.findViewById(R.id.edtname);
EditText edtno= dialog.findViewById(R.id.edtno);
Button btnActn= dialog.findViewById(R.id.addbtn);
btnActn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name=" ", number="";
if(!edtname.getText().toString().equals("")){
name= edtname.getText().toString();
}
else{
Toast.makeText(MainActivity.this, "Please Enter the name ", Toast.LENGTH_SHORT).show();
}
if(!edtno.getText().toString().equals("")){
number= edtno.getText().toString();
}
else{
Toast.makeText(MainActivity.this, "Please Enter the number ", Toast.LENGTH_SHORT).show();
}
arrContact.add(new ContactModel(name,number));
adapter.notifyItemInserted(arrContact.size()-1);
recyclerView.scrollToPosition(arrContact.size()-1);
dialog.dismiss();
}
});
dialog.show();
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(this));
arrContact.add(new ContactModel(R.drawable.ap,"Shank","908784836"));
arrContact.add(new ContactModel(R.drawable.lp,"Luffy","908784836"));
arrContact.add(new ContactModel(R.drawable.kl,"Aurther","908784836"));
arrContact.add(new ContactModel(R.drawable.ss,"madara","908784836"));
arrContact.add(new ContactModel(R.drawable.gp,"mob","908784836"));
arrContact.add(new ContactModel(R.drawable.hp,"Sakuna","908784836"));
arrContact.add(new ContactModel(R.drawable.gp,"mob","908784836"));
arrContact.add(new ContactModel(R.drawable.ap,"Shank","908784836"));
arrContact.add(new ContactModel(R.drawable.lp,"luffy","908784836"));
arrContact.add(new ContactModel(R.drawable.ss,"madra","908784836"));
arrContact.add(new ContactModel(R.drawable.ss,"madra","908784836"));
arrContact.add(new ContactModel(R.drawable.ap,"shank","908784836"));
arrContact.add(new ContactModel(R.drawable.gp,"mob","908784836"));
RecyclerContactAdapter adapter= new RecyclerContactAdapter(this, arrContact);
recyclerView.setAdapter(adapter);
}
}
FATAL EXCEPTION: main Process: com.example.recycleview, PID: 2731 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.recycleview.RecyclerContactAdapter.notifyItemInserted(int)' on a null object reference at com.example.recycleview.MainActivity$1$1.onClick(MainActivity.java:59) at android.view.View.performClick(View.java:7448) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1219) at android.view.View.performClickInternal(View.java:7425) at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28305) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
please check the above code mainacitvity
1条答案
按热度按时间oiopk7p51#
因此,在代码的开头,您已经声明了一个变量:
但是在代码的末尾,您定义了另一个
adapter
:上面的代码只会创建一个新的局部变量,并保持
adapter
为null
。所以如果你想在同一个类中的任何函数中访问
adapter
,你应该给最上面声明的adapter
赋值,因此你应该删除最下面声明的RecyclerContactAdapter
: