0

Another problem in my android application. When i click on home button, it exits from the application but saying Sorry, the application has stopped unexpectedly

The code for my home button is

ImageView home  =   (ImageView)findViewById(R.id.home1);
    home.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            kalma1.stop();
            Intent i =new Intent(Kalma1.this, null);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            finish();
        }
    });

and the console says the following

GC_EXTERNAL_ALLOC freed 1110 objects / 70984 bytes in 77ms
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x4001d800)
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.content.ComponentName.<init>(ComponentName.java:76)
at android.content.Intent.<init>  (Intent.java:2678)
at com.kalmas.Kalma1$5.onClick(Kalma1.java:74)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at  android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at  java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Sending signal. PID: 368 SIG: 9
GAMA
  • 5,958
  • 14
  • 79
  • 126
baig772
  • 3,404
  • 11
  • 48
  • 93

3 Answers3

3

If you want to finish only that activity then dont use any intent just write finish()

home.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            finish();
        }
    });
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
0
Intent i =new Intent(Kalma1.this, null);

is the prime cause of this NullPointerException.

You have to mention the name of the activity to which you want to navigate as second parameter.

EDIT

Android's design does not favor exiting an application by choice, but rather manages it by the OS. You can bring up the Home application by its corresponding Intent:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

instead of

Intent i =new Intent(Kalma1.this, null);

Refer this question on SO

Community
  • 1
  • 1
GAMA
  • 5,958
  • 14
  • 79
  • 126
0

You should call it as below to finish your current activity:

home.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            kalma1.stop();
            //Intent i =new Intent(Kalma1.this, null);
            //i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            finish();
        }
    });
waqaslam
  • 67,549
  • 16
  • 165
  • 178