我正在尝试传递图像的ID以便使用通知从列表视图中删除条目。但是delGoalId的值显示为“null”。请帮帮我。
错误:NullPointerException:尝试在空对象引用上调用虚方法'int android.os.Bundle.getInt(java.lang.String)'
AlarmReceiver.java
Intent deleteIntent = new Intent(context, MainActivity.class);
deleteIntent.setAction("Delete");
//onReceive(position, mContext, deleteIntent);
deleteIntent.putExtra("ID", MyImageID.get(0));
deleteIntent.putExtra("update", true);
deleteIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainActivity.java
OnCreate()
int delGoalId = getIntent().getExtras().getInt("ID");
Toast.makeText(getApplicationContext(),"delGoalId value :"+delGoalId,Toast.LENGTH_LONG).show();
if (delGoalId >= 0) {
onReceive(delGoalId);
}
的onReceive()
public void onReceive(final int position) {
final SwipeMenuListView swipelist = (SwipeMenuListView) findViewById(R.id.main_list_view);
Toast.makeText(getApplicationContext(), "onReceive", Toast.LENGTH_LONG).show();
final MyImage image = (MyImage) swipelist.getItemAtPosition((int) position);
String action = getIntent().getAction();
if ("Delete".equals(action)) {// to execute delete option
Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_LONG).show();
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
Toast.makeText(getApplicationContext(), image + " " + " is deleted.", Toast.LENGTH_LONG).show();
Log.d("Delete Image: ", "Deleting.....");
adapter.remove(adapter.getItem((int) position));
swipelist.invalidateViews();
File fdelete = new File(image.getTitle());
if (fdelete.exists())
{
if (fdelete.delete()) {
daOdb.deleteImage(image);
daOdb.getImages();
// Gets an instance of the NotificationManager service
myGoalNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
myGoalNotifyMgr.cancel(position);
System.out.println("File Deleted :" + image.getPath());
} else {
daOdb.deleteImage(image);
daOdb.getImages();
System.out.println("File Not Deleted :" + image.getPath());
}
}
swipelist.invalidateViews();
dialog.cancel();
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
dialog.cancel();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Do You Wish To Delete?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
}