我得到错误[试图调用虚拟方法'void android. widgets . button .setOnClickListener(android.view.View$OnClickListener)'上的空对象引用]在运行下面的代码和从Logcat我检查了错误是在调用setOnClickListener()上的按钮id "问题mbtn& quot;在第47行
package com.royslab.recview;
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.util.Log;
import android.view.View;
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> list = new ArrayList<>();
RecyclerView recyclerView;
ContactRecyclerAdapter adapter;
FloatingActionButton btnOpenDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
btnOpenDialog = findViewById(R.id.btnOpenDialog);
btnOpenDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.update_contact_dialog_layout);
EditText addTxtName = findViewById(R.id.addTxtName);
EditText addTxtNumber = findViewById(R.id.addTxtNumber);
Button btnAction = findViewById(R.id.problmBtn);
btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = "", number = "";
if (!addTxtName.getText().toString().equals("")){
name = addTxtName.getText().toString();
} else{
Toast.makeText(MainActivity.this, "Please add a valid name", Toast.LENGTH_SHORT).show();
}
if (!addTxtNumber.getText().toString().equals("")){
number = addTxtNumber.getText().toString();
} else {
Toast.makeText(MainActivity.this, "Please add valid contact number", Toast.LENGTH_SHORT).show();
}
list.add(new ContactModel(name, number));
adapter.notifyItemInserted(list.size() - 1);
recyclerView.scrollToPosition(list.size() - 1);
}
});
dialog.show();
}
});
list.add(new ContactModel(R.drawable.a, "Kalyan","9999999999"));
list.add(new ContactModel(R.drawable.b, "Uttam","8888888888"));
list.add(new ContactModel(R.drawable.c, "Bulu","99999999999"));
list.add(new ContactModel(R.drawable.d, "Pampa","00000000000"));
list.add(new ContactModel(R.drawable.a, "Kalyan","9999999999"));
list.add(new ContactModel(R.drawable.b, "Uttam","8888888888"));
list.add(new ContactModel(R.drawable.c, "Bulu","99999999999"));
list.add(new ContactModel(R.drawable.d, "Pampa","00000000000"));
adapter = new ContactRecyclerAdapter(this, list);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
XML代码如下-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="300dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="center_vertical"
android:background="@color/teal_700">
<TextView
android:layout_width="270dp"
android:layout_height="3dp"
android:background="@color/black"
android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_new_contact"
android:gravity="center"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="@color/white"/>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="7dp"
android:layout_margin="5dp"
app:cardBackgroundColor="@color/teal_200">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:id="@+id/llRow"
android:padding="11dp">
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="50dp">
<ImageView
android:id="@+id/addImgContact"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/contact_image"
android:src="@drawable/ic_user"
android:background="@color/white"
/>
</androidx.cardview.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="11dp"
android:orientation="vertical"
tools:ignore="UselessLeaf"
android:background="@color/white">
<EditText
android:id="@+id/addTxtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/contact"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/material_dynamic_primary40"
android:textColorHint="@color/black"
android:layout_marginBottom="3dp"
android:background="@color/white"
android:inputType="text"
android:padding="3dp"
android:autofillHints="name" />
<EditText
android:id="@+id/addTxtNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/contact_number"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:padding="3dp"
android:background="@color/material_dynamic_neutral30"
android:inputType="number"
android:autofillHints="phone" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<Button
android:id="@+id/problmBtn"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/add_new_contact"
android:textStyle="bold"
android:backgroundTint="@color/material_dynamic_primary20"
android:layout_marginBottom="10dp"
/>
<TextView
android:layout_width="310dp"
android:layout_height="3dp"
android:background="@color/white"
android:layout_gravity="center"/>
</LinearLayout>
我认为你的actionButton没有正确初始化。为什么?因为发生空指针异常。始终将构造代码的方法作为最佳实践。学习下面的代码并修改你的代码。
private void showDialog(){
View v = LayoutInfilator.from(this).infilate(R.layout.update_contact_dialog_layout,null);
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomDialogImage);
builder.setView(v);
Dialog dialog = builder.create();
EditText addTxtName = v.findViewById(R.id.addTxtName);
EditText addTxtNumber = v.findViewById(R.id.addTxtNumber);
Button btnAction = v.findViewById(R.id.problmBtn);
btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = "", number = "";
if (!addTxtName.getText().toString().equals("")){
name = addTxtName.getText().toString();
} else{
Toast.makeText(MainActivity.this, "Please add a valid name", Toast.LENGTH_SHORT).show();
}
if (!addTxtNumber.getText().toString().equals("")){
number = addTxtNumber.getText().toString();
} else {
Toast.makeText(MainActivity.this, "Please add valid contact number", Toast.LENGTH_SHORT).show();
}
list.add(new ContactModel(name, number));
adapter.notifyItemInserted(list.size() - 1);
recyclerView.scrollToPosition(list.size() - 1);
}
});
dialog.show();
}
在btnOpenDialog onclicklistener中调用这个方法。像风箱一样。
btnOpenDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog();
}});
并且不要忘记在style.xml文件中添加bellow样式。
<style name="CustomDialogImage" parent="Theme.AppCompat.Dialog">
<item name="colorAccent">@color/color_primary</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>