I have a listView with an imageButton. When i click on the imageButton, I am trying to call a method in the main class that will display a toast message. However, when i click on the button, i get an error saying:
04-17 18:25:16.293: E/AndroidRuntime(9236): FATAL EXCEPTION: main
04-17 18:25:16.293: E/AndroidRuntime(9236): java.lang.NullPointerException
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.widget.Toast.<init>(Toast.java:94)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.widget.Toast.makeText(Toast.java:240)
04-17 18:25:16.293: E/AndroidRuntime(9236): at com.flamesavor.reme.ReMe.setAlarm(ReMe.java:69)
04-17 18:25:16.293: E/AndroidRuntime(9236): at com.flamesavor.reme.resources.ListViewAdapter.setAlarm(ListViewAdapter.java:62)
04-17 18:25:16.293: E/AndroidRuntime(9236): at com.flamesavor.reme.resources.ListViewAdapter$1.onClick(ListViewAdapter.java:49)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.view.View.performClick(View.java:4222)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.view.View$PerformClick.run(View.java:17343)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.os.Handler.handleCallback(Handler.java:615)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.os.Looper.loop(Looper.java:137)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.app.ActivityThread.main(ActivityThread.java:4895)
04-17 18:25:16.293: E/AndroidRuntime(9236): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 18:25:16.293: E/AndroidRuntime(9236): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 18:25:16.293: E/AndroidRuntime(9236): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-17 18:25:16.293: E/AndroidRuntime(9236): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-17 18:25:16.293: E/AndroidRuntime(9236): at dalvik.system.NativeStart.main(Native Method)
Below is my Arrayadapter where I have created the onClickListener. The listener calls a method in the adapter which then calls the setAlarm method in the Main Class (called 'ReMe').
public class ListViewAdapter extends android.widget.ArrayAdapter<String> {
private Context context;
private List<String> reminders;
private View reminder;
public ListViewAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
this.context = context;
this.reminders = objects;
}
//Change textcolor to black. Everything else remains same
@Override
public View getView(int position, View convertView, ViewGroup parent){
//Set view to convertView if the there are already rows created
reminder = convertView;
if(reminder == null){
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
reminder = inflator.inflate(R.layout.reminder_list_layout, parent, false);
}
//Set imageButton actionListener
ImageButton alarmButton = (ImageButton) reminder.findViewById(R.id.reminder_alarmnavigation);
alarmButton.setTag(reminder);
alarmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setAlarm((View) v.getTag());
}
});
//Initialize and assign reminder title
TextView reminder_title = (TextView) reminder.findViewById(R.id.reminder_title);
reminder_title.setText(reminders.get(position));
return reminder;
}
public void setAlarm(View view){
ReMe reminder = new ReMe();
reminder.setAlarm(view);
}
}
Below is the method in the ReMe class:
public void setAlarm(View view){
Intent intent = new Intent(this, SetReminder.class);
TextView reminder_title = (TextView) view.findViewById(R.id.reminder_title);
Toast.makeText(ReMe.this, "testing", Toast.LENGTH_LONG).show();
//intent.putExtra("com.flamesavor.reme.reminderTitle", reminder_title.getText());
//startActivity(intent);
}
Not sure where I am making a mistake. Any help will be greatly appreciated.
Below is the modified code that tries to trigger an activity instead if creating a toast:
public void setAlarm(View view){
Intent intent = new Intent(view.getContext(), SetReminder.class);
TextView reminder_title = (TextView) view.findViewById(R.id.reminder_title);
intent.putExtra("com.flamesavor.reme.reminderTitle", reminder_title.getText());
startActivity(intent);
}
Below is the error log that i get. Not sure why this is happening:
04-18 22:53:06.381: E/AndroidRuntime(8706): FATAL EXCEPTION: main
04-18 22:53:06.381: E/AndroidRuntime(8706): java.lang.NullPointerException
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.app.Activity.startActivityForResult(Activity.java:3423)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.app.Activity.startActivityForResult(Activity.java:3384)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.app.Activity.startActivity(Activity.java:3594)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.app.Activity.startActivity(Activity.java:3562)
04-18 22:53:06.381: E/AndroidRuntime(8706): at com.flamesavor.reme.ReMe.setAlarm(ReMe.java:67)
04-18 22:53:06.381: E/AndroidRuntime(8706): at com.flamesavor.reme.resources.ListViewAdapter.setAlarm(ListViewAdapter.java:62)
04-18 22:53:06.381: E/AndroidRuntime(8706): at com.flamesavor.reme.resources.ListViewAdapter$1.onClick(ListViewAdapter.java:49)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.view.View.performClick(View.java:4222)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.view.View$PerformClick.run(View.java:17343)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.os.Handler.handleCallback(Handler.java:615)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.os.Looper.loop(Looper.java:137)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.app.ActivityThread.main(ActivityThread.java:4895)
04-18 22:53:06.381: E/AndroidRuntime(8706): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 22:53:06.381: E/AndroidRuntime(8706): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 22:53:06.381: E/AndroidRuntime(8706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-18 22:53:06.381: E/AndroidRuntime(8706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-18 22:53:06.381: E/AndroidRuntime(8706): at dalvik.system.NativeStart.main(Native Method)
=====================================================================================
Resolution (Edited):
Set the FLAG_ACTIVITY_NEW_TASK and called the new activity in the context of the view.
TextView reminder_title = (TextView) view.findViewById(R.id.reminder_title);
Intent intent = new Intent(view.getContext(), com.flamesavor.reme.SetReminder.class);
intent.putExtra("com.flamesavor.reme.reminderTitle", reminder_title.getText());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);