如何杀死Android应用onButtonClick

时间:2014-01-23 09:52:15

标签: android

实际上我试图通过点击图片按钮关闭这个应用程序,但是当我为应用程序关闭添加代码时,它会在启动之前崩溃应用程序。这是我的代码。我已经在创建mothod上创建了应用程序密码。

    public class Alphabets extends Activity {

        MediaPlayer mp;
        MediaPlayer BG;
        ImageButton exit;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_numbers);
            ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
            ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
            myPager.setOnPageChangeListener(player);
            myPager.setAdapter(adapter);
            myPager.setCurrentItem(0);
            BG = MediaPlayer.create(this, R.raw.jingle);
            BG.setLooping(true);
            BG.setVolume(100, 100);
            BG.start();
            mp = MediaPlayer.create(Alphabets.this, mAudio[0]);
            mp.start();
            exit.setOnClickListener(new View.OnClickListener() {         
                @Override
                public void onClick(View paramView) 
              {
                    finish();          
                    moveTaskToBack(true);
                }
            });
    }

        @Override
        protected void onPause() {
            super.onPause();
            BG.pause();
            }

        @Override
        protected void onResume() {
        super.onResume();
        BG.start();
        }



        private int[] mAudio = new int[]{R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,
                                         R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,};

        private int imageArra[] =  {R.drawable.ab,R.drawable.bc,R.drawable.cd,R.drawable.de,R.drawable.ef,R.drawable.fg,R.drawable.gh,R.drawable.hi,R.drawable.ij,R.drawable.jk,R.drawable.kl,R.drawable.lm,R.drawable.mn,
                                    R.drawable.no,R.drawable.op,R.drawable.pq,R.drawable.qr,R.drawable.rs,R.drawable.st,R.drawable.tu,R.drawable.uv,R.drawable.vw,R.drawable.wx,R.drawable.xy,R.drawable.yz,R.drawable.zz};

        public  OnPageChangeListener player = new OnPageChangeListener()
        {

            @Override
            public void onPageSelected(int arg1)
            {
            mp = MediaPlayer.create(Alphabets.this, mAudio[arg1]);
            mp.stop();
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
            }
        };

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.numbers, menu);
            return true;
        }

   }

布局: -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/image2"
    android:gravity="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Alphabets" >

     <android.support.v4.view.ViewPager
        android:id="@+id/myfivepanelpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

     <ImageButton
         android:id="@+id/exit"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/myfivepanelpager"
         android:layout_alignLeft="@+id/myfivepanelpager"
         android:layout_marginBottom="170dp"
         android:src="@android:drawable/btn_star" />

</RelativeLayout>

logcat: -

01-23 05:10:01.951: D/dalvikvm(1767): Not late-enabling CheckJNI (already on)
01-23 05:10:03.381: D/dalvikvm(1767): GC_FOR_ALLOC freed 68K, 8% free 2495K/2692K, paused 109ms, total 114ms
01-23 05:10:03.681: I/dalvikvm-heap(1767): Grow heap (frag case) to 11.347MB for 9216012-byte allocation
01-23 05:10:03.781: D/dalvikvm(1767): GC_FOR_ALLOC freed <1K, 2% free 11494K/11696K, paused 95ms, total 95ms
01-23 05:10:08.191: D/dalvikvm(1767): GC_FOR_ALLOC freed <1K, 2% free 11494K/11696K, paused 20ms, total 20ms
01-23 05:10:08.331: I/dalvikvm-heap(1767): Grow heap (frag case) to 31.123MB for 20736012-byte allocation
01-23 05:10:08.361: I/dalvikvm-heap(1767): Clamp target GC heap from 33.123MB to 32.000MB
01-23 05:10:08.381: D/dalvikvm(1767): GC_FOR_ALLOC freed <1K, 1% free 31744K/31948K, paused 45ms, total 45ms
01-23 05:10:10.091: D/dalvikvm(1767): GC_FOR_ALLOC freed 9000K, 29% free 23055K/32260K, paused 26ms, total 27ms
01-23 05:10:10.461: D/libEGL(1767): loaded /system/lib/egl/libEGL_emulation.so
01-23 05:10:10.471: D/(1767): HostConnection::get() New Host Connection established 0xb8cc08f0, tid 1767
01-23 05:10:10.551: D/libEGL(1767): loaded /system/lib/egl/libGLESv1_CM_emulation.so
01-23 05:10:10.551: D/libEGL(1767): loaded /system/lib/egl/libGLESv2_emulation.so
01-23 05:10:10.721: W/EGL_emulation(1767): eglSurfaceAttrib not implemented
01-23 05:10:10.821: D/OpenGLRenderer(1767): Enabling debug mode 0
01-23 05:10:13.591: D/dalvikvm(1767): GC_FOR_ALLOC freed 519K, 26% free 23938K/32260K, paused 23ms, total 23ms
01-23 05:10:13.591: I/dalvikvm-heap(1767): Forcing collection of SoftReferences for 9216012-byte allocation
01-23 05:10:13.631: D/dalvikvm(1767): GC_BEFORE_OOM freed 10K, 26% free 23928K/32260K, paused 34ms, total 34ms
01-23 05:10:13.631: E/dalvikvm-heap(1767): Out of memory on a 9216012-byte allocation.
01-23 05:10:13.631: I/dalvikvm(1767): "main" prio=5 tid=1 RUNNABLE
01-23 05:10:13.631: I/dalvikvm(1767):   | group="main" sCount=0 dsCount=0 obj=0xb2f45480 self=0xb8afe1e0
01-23 05:10:13.631: I/dalvikvm(1767):   | sysTid=1767 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1216602080
01-23 05:10:13.631: I/dalvikvm(1767):   | state=R schedstat=( 0 0 0 ) utm=453 stm=90 core=0
01-23 05:10:13.651: I/dalvikvm(1767):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.content.res.Resources.loadDrawable(Resources.java:2105)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.view.View.<init>(View.java:3364)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.view.View.<init>(View.java:3293)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.view.ViewGroup.<init>(ViewGroup.java:453)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
01-23 05:10:13.651: I/dalvikvm(1767):   at java.lang.reflect.Constructor.constructNative(Native Method)
01-23 05:10:13.651: I/dalvikvm(1767):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.view.LayoutInflater.createView(LayoutInflater.java:594)
01-23 05:10:13.651: I/dalvikvm(1767):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
01-23 05:10:13.651: I/dalvikvm(1767):   at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-23 05:10:13.661: I/dalvikvm(1767):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.app.Activity.setContentView(Activity.java:1895)
01-23 05:10:13.661: I/dalvikvm(1767):   at com.android.toddlers.Alphabets.onCreate(Alphabets.java:21)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.app.Activity.performCreate(Activity.java:5133)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.os.Looper.loop(Looper.java:137)
01-23 05:10:13.661: I/dalvikvm(1767):   at android.app.ActivityThread.main(ActivityThread.java:5103)
01-23 05:10:13.661: I/dalvikvm(1767):   at java.lang.reflect.Method.invokeNative(Native Method)
01-23 05:10:13.661: I/dalvikvm(1767):   at java.lang.reflect.Method.invoke(Method.java:525)
01-23 05:10:13.661: I/dalvikvm(1767):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-23 05:10:13.661: I/dalvikvm(1767):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-23 05:10:13.661: I/dalvikvm(1767):   at dalvik.system.NativeStart.main(Native Method)
01-23 05:10:13.681: D/skia(1767): --- decoder->decode returned false
01-23 05:10:13.691: D/AndroidRuntime(1767): Shutting down VM
01-23 05:10:13.691: W/dalvikvm(1767): threadid=1: thread exiting with uncaught exception (group=0xb2f44648)
01-23 05:10:13.721: E/AndroidRuntime(1767): FATAL EXCEPTION: main
01-23 05:10:13.721: E/AndroidRuntime(1767): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.toddlers/com.android.toddlers.Alphabets}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.os.Looper.loop(Looper.java:137)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at java.lang.reflect.Method.invoke(Method.java:525)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at dalvik.system.NativeStart.main(Native Method)
01-23 05:10:13.721: E/AndroidRuntime(1767): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.app.Activity.setContentView(Activity.java:1895)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at com.android.toddlers.Alphabets.onCreate(Alphabets.java:21)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.app.Activity.performCreate(Activity.java:5133)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-23 05:10:13.721: E/AndroidRuntime(1767):     ... 11 more
01-23 05:10:13.721: E/AndroidRuntime(1767): Caused by: java.lang.reflect.InvocationTargetException
01-23 05:10:13.721: E/AndroidRuntime(1767):     at java.lang.reflect.Constructor.constructNative(Native Method)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
01-23 05:10:13.721: E/AndroidRuntime(1767):     ... 23 more
01-23 05:10:13.721: E/AndroidRuntime(1767): Caused by: java.lang.OutOfMemoryError
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.content.res.Resources.loadDrawable(Resources.java:2105)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.view.View.<init>(View.java:3364)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.view.View.<init>(View.java:3293)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.view.ViewGroup.<init>(ViewGroup.java:453)
01-23 05:10:13.721: E/AndroidRuntime(1767):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
01-23 05:10:13.721: E/AndroidRuntime(1767):     ... 26 more
01-23 05:10:16.981: I/Process(1767): Sending signal. PID: 1767 SIG: 9

2 个答案:

答案 0 :(得分:6)

您忘记初始化exit ImageButton。首先初始化它......

exit = (ImageButton)findViewById(R.id.id_of_image_button)
exit.setOnClickListener(new View.OnClickListener() {         
    @Override
        public void onClick(View paramView) {
            finish();          
            moveTaskToBack(true);
        }
    });

答案 1 :(得分:2)

您尚未初始化ImageButton中的onCreate()并关闭应用只需编写android.os.Process.killProcess(android.os.Process.myPid())即可完全终止您的应用。

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_numbers);
    exit=(ImageButton)findViewById(R.id.yourid);
         ...............
    ...............
     exit.setOnClickListener(new View.OnClickListener() {         
        @Override
        public void onClick(View paramView) 
      {
          android.os.Process.killProcess(android.os.Process.myPid()) ;     

        }
    });

moveTaskToBack(boolean nonRoot)将包含此活动的任务移动到活动堆栈的后面。它不会关闭应用程序,但应用程序将在后台运行。

您可能会因为onResume()方法而导致应用程序崩溃,而您在没有初始化的情况下直接启动MediaPlayer。在onResume()之前调用onCreate()。因此,只需在MediaPlayer BG中初始化onResume(),如下所示:

@Override
protected void onResume() {
super.onResume();
BG=new MediaPlayer();
}