Good day, Ive been having a hard time dealing with android notifications lately so here’s my problem. What my application does is that user would login and if successful it will display the main page. When a user would press the back button(from the main page) it would minimize the app and create a notification rather than go back to the login page. My problem is that when i would press the running notification(resume the application from the notification tab) it would create an error.
Here are some of my codes
public class WelcomeActivity1 extends ListActivity{
private NotificationManager mNM;
Intent notificationIntent;
protected void onCreate(Bundle savedInstanceState) {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationIntent = new Intent(WelcomeActivity1.this, WelcomeActivity1.class);
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK){
showMinimized();
moveTaskToBack(true);
}
return super.onKeyDown(keyCode, event);
}
public void showMinimized(){
CharSequence text = "Zylun Intranet is Minimized";
Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(WelcomeActivity1.this, 0, new Intent(this,WelcomeActivity1.class), 0);
notification.setLatestEventInfo(getApplicationContext(), "Zylun Intranet Main", "Running", contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNM.notify(1,notification);
}
}
I would really appreciate if someone could tell me what is wrong with my code. Thanks!
when login credential is correct
https://i.stack.imgur.com/Shp3W.jpg
minimize app when user would press back button(from main page)
https://i.stack.imgur.com/fjMfa.jpg
here are my errors:
10-29 07:45:01.196: E/AndroidRuntime(733): FATAL EXCEPTION: main
10-29 07:45:01.196: E/AndroidRuntime(733): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.intranetzylun1/com.app.intranetzylun1.WelcomeActivity1}: java.lang.NullPointerException
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.os.Looper.loop(Looper.java:123)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-29 07:45:01.196: E/AndroidRuntime(733): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 07:45:01.196: E/AndroidRuntime(733): at java.lang.reflect.Method.invoke(Method.java:521)
10-29 07:45:01.196: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-29 07:45:01.196: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-29 07:45:01.196: E/AndroidRuntime(733): at dalvik.system.NativeStart.main(Native Method)
10-29 07:45:01.196: E/AndroidRuntime(733): Caused by: java.lang.NullPointerException
10-29 07:45:01.196: E/AndroidRuntime(733): at com.app.intranetzylun1.WelcomeActivity1.onCreate(WelcomeActivity1.java:105)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-29 07:45:01.196: E/AndroidRuntime(733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
Used this method instead, so when the user press the back button it will display the main page and when the user would like to go back to the application he/she would just hold the menu key and a menu will be displayed for the recent apps being used.