Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:
I am trying to pass the Id of the image inorder to delete the entry from the listview using a notification. But the value of the delGoalId is showing "null". Help me please.

Error: NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference


What I have tried:

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();

}
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900