在我的应用程序的开发过程中,我遇到了此错误。 因此,我的应用程序由一个recyclerView和一个浮动操作按钮组成,每个recyclerView上都有一个cardViews列表,然后每个Card视图都有一些文本视图以及一个编辑按钮和一个删除按钮。 我试图使用recyclerViewAdapter类-这是一个非活动类-使用对话框片段来调用编辑按钮,这是完整的错误消息:
2019-05-15 02:21:11.063 12260-12260/com.hitham.miscarros E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hitham.miscarros, PID: 12260
java.lang.NullPointerException: Attempt to invoke interface method 'void com.hitham.miscarros.Fragments.EditPickerFragment$OnInputListener.sendShit(java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at com.hitham.miscarros.Fragments.EditPickerFragment$2.onClick(EditPickerFragment.java:92)
at android.view.View.performClick(View.java:6913)
at android.view.View.performClickInternal(View.java:6890)
at android.view.View.access$3200(View.java:792)
at android.view.View$PerformClick.run(View.java:27158)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:209)
at android.app.ActivityThread.main(ActivityThread.java:7021)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:486)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:872)
这是此错误所涉及的RecyclerViewAdapter类段:
public void editCar(Cars car)
{
DialogFragment fragment = new com.hitham.miscarros.Fragments.EditPickerFragment();
fragment.show(((FragmentActivity)context).getSupportFragmentManager(),"edit car picker fragment");
.
.
.
.
.
}
@Override
public void sendShit(String carManuf, String carName, String carModel, String carImage) {
DatabaseHandler db = new DatabaseHandler(context);
Cars car = new Cars();
car.setCarID(getAdapterPosition());
if(carManuf != null)
car.setCarManuf(carManuf);
if(carName != null)
car.setCarName(carName);
if(carModel != null)
car.setCarModel(carModel);
if(carImage != null)
car.setCarImage(carImage);
db.updateCars(car);
notifyItemChanged(getAdapterPosition(), car);
dialog.dismiss();
Intent myIntent = new Intent(context,ListActivity.class);
context.startActivity(myIntent);
((Activity)context).finish();
((Activity)context).finish();
}
这是editPickerFragment类:
package com.hitham.miscarros.Fragments;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.hitham.miscarros.Model.Cars;
import com.hitham.miscarros.R;
import static android.app.Activity.RESULT_OK;
public class EditPickerFragment extends DialogFragment {
public interface OnInputListener {
void sendShit(String carManuf, String carName, String carModel, String carImage);
}
public OnInputListener onInputListener;
private static final int REQUEST_IMAGE_CAPTURE = 7;
private Button imageBu;
private Button submitBu;
private ImageView carImage;
private EditText carManuf;
private EditText carName;
private EditText carModel;
private String imagePath;
private TextView carDetails;
private Cars car;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View myView = LayoutInflater.from(getActivity()).inflate(R.layout.new_car_popup,null);
carManuf = myView.findViewById(R.id.nCarManuf);
carName = myView.findViewById(R.id.nCarName);
carModel = myView.findViewById(R.id.nCarDOB);
carImage = myView.findViewById(R.id.laImagenaPuta);
carDetails = myView.findViewById(R.id.carDetails);
imageBu = myView.findViewById(R.id.addImageID);
submitBu = myView.findViewById(R.id.addCarID);
imageBu.setText(R.string.editImageText);
submitBu.setText(R.string.editCarBu);
carDetails.setText(R.string.editCarText);
carManuf.setHint(R.string.manufHint);
carName.setHint(R.string.nameHint);
carModel.setHint(R.string.DOBHint);
imageBu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(getActivity(), new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_IMAGE_CAPTURE);
}else {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images
.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent,REQUEST_IMAGE_CAPTURE);
}
}catch (Exception e)
{
e.printStackTrace();
}
}
});
submitBu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String manuf = carManuf.getText().toString();
String name = carName.getText().toString();
String model = carModel.getText().toString();
onInputListener.sendShit(manuf,name,model,imagePath);
}
});
myView.setBackgroundColor(Color.TRANSPARENT);
return myView;
}
// public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
//
// }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case REQUEST_IMAGE_CAPTURE:
if (resultCode == RESULT_OK)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor miCursor = getActivity().getContentResolver().query(selectedImage,filePathColumn,null,null,null);
miCursor.moveToFirst();
int columnIndex = miCursor.getColumnIndex(filePathColumn[0]);
imagePath = miCursor.getString(columnIndex);
miCursor.close();
Bitmap laImagenaPuta = BitmapFactory.decodeFile(imagePath);
carImage.setImageBitmap(laImagenaPuta);
}
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
onInputListener = (OnInputListener) getActivity();
}catch (ClassCastException e){
Log.e("maTag","on Attach : class cast exception");
}
}
}
当我使用添加按钮(FAB)进行所有操作时,一切正常,因此我确定它与保存到数据库或其他任何东西都没有关系,这只是一个空指针异常,但我不知道如何处理解决这个问题。
P.S,我只包含我认为重要的内容,并对此有所参与,