2011-02-07 103 views
0

我的應用程序有三個按鈕,每創建一個新的意圖是這樣的:我的Android應用程序意外終止

Button button3 = (Button) findViewById(R.id.Button03); 
    button3.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      Intent myIntent = new Intent(view.getContext(), Shout.class); 
      startActivityForResult(myIntent, 0); 
     } 

    }); 

所以基本上我的其他類項目,當我點擊一個按鈕啓動。當我想回到第一個屏幕時,我只需按後退鍵即可。 這個效果很好,但是當我按下按鈕和後退鍵約5次的應用程序崩潰。爲什麼?我注意到,我回去從類後,也將繼續sensorlistener全速喜歡這裏運行:

02-07 16:51:13.034: DEBUG/sensor(5354): onSensorChanged: 2 
02-07 16:51:13.094: DEBUG/sensor(5354): onSensorChanged: 2 
02-07 16:51:13.154: DEBUG/sensor(5354): onSensorChanged: 2 
02-07 16:51:13.214: DEBUG/sensor(5354): onSensorChanged: 2 
02-07 16:51:13.274: DEBUG/sensor(5354): onSensorChanged: 2 
02-07 16:51:13.334: DEBUG/sensor(5354): onSensorChanged: 2 
02-07 16:51:13.394: DEBUG/sensor(5354): onSensorChanged: 2 
02-07 16:51:13.444: INFO/ActivityManager(156): Starting: Intent { cmp=com.bengaard/.Shout } from pid 5354 
02-07 16:51:13.504: DEBUG/sensor(5354): onSensorChanged: 2 
02-07 16:51:13.564: DEBUG/dalvikvm(5354): GC_EXTERNAL_ALLOC freed 394K, 60% free 3179K/7943K, external 24861K/26873K, paused 34ms 
02-07 16:51:13.574: ERROR/dalvikvm-heap(5354): 32980-byte external allocation too large for this process. 
02-07 16:51:13.604: ERROR/GraphicsJNI(5354): VM won't let us allocate 32980 bytes 
02-07 16:51:13.604: DEBUG/dalvikvm(5354): GC_FOR_MALLOC freed <1K, 60% free 3179K/7943K, external 24861K/26873K, paused 21ms 
02-07 16:51:13.614: DEBUG/skia(5354): --- decoder->decode returned false 
02-07 16:51:13.614: DEBUG/AndroidRuntime(5354): Shutting down VM 
02-07 16:51:13.614: WARN/dalvikvm(5354): threadid=1: thread exiting with uncaught exception (group=0x40169560) 
02-07 16:51:13.614: DEBUG/webviewglue(5354): nativeDestroy view: 0x4dc7e0 
02-07 16:51:13.614: DEBUG/webviewglue(5354): nativeDestroy view: 0x47a310 
02-07 16:51:13.614: DEBUG/webviewglue(5354): nativeDestroy view: 0x4339e8 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354): FATAL EXCEPTION: main 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bengaard/com.bengaard.Shout}: android.view.InflateException: Binary XML file line #21: Error inflating class <unknown> 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.os.Looper.loop(Looper.java:123) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.app.ActivityThread.main(ActivityThread.java:3647) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at java.lang.reflect.Method.invoke(Method.java:507) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at dalvik.system.NativeStart.main(Native Method) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class <unknown> 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.view.LayoutInflater.createView(LayoutInflater.java:518) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.view.LayoutInflater.inflate(LayoutInflater.java:386) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.widget.Toast.makeText(Toast.java:235) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at com.bengaard.Shout.onCreate(Shout.java:57) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  ... 11 more 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354): Caused by: java.lang.reflect.InvocationTargetException 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at java.lang.reflect.Constructor.constructNative(Native Method) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.view.LayoutInflater.createView(LayoutInflater.java:505) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  ... 20 more 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:450) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:326) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.content.res.Resources.loadDrawable(Resources.java:1740) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.view.View.<init>(View.java:1951) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.view.View.<init>(View.java:1899) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.view.ViewGroup.<init>(ViewGroup.java:286) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  at android.widget.LinearLayout.<init>(LinearLayout.java:120) 
02-07 16:51:13.644: ERROR/AndroidRuntime(5354):  ... 23 more 
02-07 16:51:13.674: WARN/ActivityManager(156): Force finishing activity com.bengaard/.Shout 
02-07 16:51:13.674: WARN/ActivityManager(156): Force finishing activity com.bengaard/.OB 
02-07 16:51:14.174: WARN/ActivityManager(156): Activity pause timeout for HistoryRecord{40a5f000 com.bengaard/.Shout} 
02-07 16:51:14.224: ERROR/AK8973(135): Compass driver encountered fatal error2. 

它看起來像內存外的一個問題,但我怎麼能關閉的當我點擊後退按鈕時的活動,所以我不會得到這個問題,或者是否有另一個問題的答案?爲什麼點擊後退鍵後sensorlistener不會停止?對不起,我是如此小白:o)

+0

它還在您的一個XML文件中使用第21行引發運行時異常。 – 2011-02-07 16:12:40

回答

0

你有一個位圖在你的一個視圖足夠大,當應用程序試圖膨脹這個視圖的xml和解碼有問題的位圖它崩潰與出內存錯誤。

嘗試查看您是否可以優化此drawable或嘗試在ondestroy您的活動清理它。

+0

感謝您的幫助!我使用視圖快速翻轉了大約10個圖像。如何在ondestroy中清理那個操作?我試圖將其設置爲空,但我確定有更好的方法? – bengaard 2011-02-07 21:07:02

+0

看看這篇文章的例子,以確保你沒有做這些事情之一:http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-android/和然後查看如何確保在Shelves開放源代碼Android應用程序中的drawables上沒有回調:http://code.google.com/p/shelves/source/browse/trunk/Shelves/src/org/curiouscreature /android/shelves/util/ImageUtilities.java看看cleanUpCache函數 – Yahel 2011-02-07 23:10:12

0

您需要取消註冊您的傳感器偵聽器(最好在您的活動的onPause()方法中),否則系統將保留對其的引用,並且它不會被釋放以進行垃圾回收。如果註冊的偵聽器是活動本身或其他任何具有對位圖的直接或間接引用的其他內容,則該位圖也不會被釋放到垃圾收集器。