2010-06-11 102 views
9

如果我身體滑出我的Moto Droid A855鍵盤,它會崩潰我的測試應用程序與粘貼下面的堆棧跟蹤。我不明白爲什麼?另外,如果我用鍵盤啓動我的應用程序,我的應用程序會在啓動時立即崩潰。爲什麼鍵盤滑動崩潰我的應用程序?

該應用程序包含一個activity,其中包含一個viewflipper作爲主視圖佈局。該viewflipper包含兩個linearlayouts ...

堆棧跟蹤:

06-10 21:10:17.652 E/AndroidRuntime(3785): Uncaught handler: thread main exiting due to uncaught exception 
06-10 21:10:17.668 E/AndroidRuntime(3785): java.lang.IllegalArgumentException: Receiver not registered: [email protected] 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:667) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.app.ApplicationContext.unregisterReceiver(ApplicationContext.java:747) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:321) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.widget.ViewFlipper.onDetachedFromWindow(ViewFlipper.java:104) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.view.View.dispatchDetachedFromWindow(View.java:5835) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1076) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1570) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.view.ViewRoot.doDie(ViewRoot.java:2556) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.view.ViewRoot.die(ViewRoot.java:2526) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:218) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.view.Window$LocalWindowManager.removeViewImmediate(Window.java:436) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3498) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3599) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.app.ActivityThread.access$2300(ActivityThread.java:119) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1867) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.os.Looper.loop(Looper.java:123) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at java.lang.reflect.Method.invoke(Method.java:521) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
06-10 21:10:17.668 E/AndroidRuntime(3785):  at dalvik.system.NativeStart.main(Native Method) 
06-10 21:10:17.684 I/Process (1017): Sending signal. PID: 3785 SIG: 3 

編輯:添加XML佈局和主要活動相關的片段。

整個XML佈局文件從主要活動

<?xml version="1.0" encoding="utf-8"?> 


<ViewFlipper 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/vFlipper" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

       <!-- Linear Layout 1: messages and overview. --> 
       <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        > 

       <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
       <TextView 
         android:text="Connection info" 
         android:id="@+id/tvCon1" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:background="#F0F0F0" 
         android:textColor="#FF0000" 
         /> 
       </TableRow> 


       <ScrollView 
         android:id="@+id/ScrollView01" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent"> 

           <TextView 
             android:id="@+id/tvMessages" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:text="" 
            /> 

       </ScrollView> 

     </LinearLayout> 


     <!-- Linear Layout 2: settings --> 
     <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        > 


       <TableRow 
         android:id="@+id/TableRow03" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content"> 

           <TextView 
           android:text="hello world" 
           android:id="@+id/asdfasdf2" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content" 
           /> 
       </TableRow>     

     </LinearLayout> 

</ViewFlipper> 

代碼片段:

/** 
* Attempt (not currently working) to work around this bug: http://code.google.com/p/android/issues/detail?id=6191 
* TODO: make it work. 
*/ 
@Override 
public void onDetachedFromWindow() { 
     Log.d("Dash","OnDetachedFromWindow()"); 

     try { 
     super.onDetachedFromWindow(); 
    } 
    catch (Exception e) { 
     ViewFlipper v = (ViewFlipper)findViewById(R.id.vFlipper); 
     if (v != null) { 
       Log.d("Dash","De-Bug hit. e=" + e.getMessage()); 
       v.stopFlipping(); 
     } 
    } 
} 

回答

21

要解決此問題,你必須

  • 定義一個名爲MyViewFlipper新的類,它覆蓋ViewFlipper(見下文)
  • 參考新課改的任何地方,你會先前引用ViewFlipper
  • 定義類和佈局如下圖所示:

新班級名爲MyViewFlipper。包含以下內容:

package com.gtosoft.dash; // change this to match your own app. 

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.widget.ViewFlipper; 

public class MyViewFlipper extends ViewFlipper { 

    public MyViewFlipper(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onDetachedFromWindow() { 
     try{ 
      super.onDetachedFromWindow(); 
     }catch(Exception e) { 
      Log.d("MyViewFlipper","Stopped a viewflipper crash"); 
      stopFlipping(); 
     } 
    } 
} 

現在使用的ViewFlipper這個「固定」版本,你必須引用它的XML佈局。因爲它基本上是一個「自定義視圖」,您必須將包路徑完全有資格MyViewFlipper,如下所示:

<com.gtosoft.dash.MyViewFlipper 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/vFlipper" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    (insert all the other old layout code here) 

</com.gtosoft.dash.MyViewFlipper> 

現在,應用程序不再崩潰在鍵盤上滑動事件或如果應用程序開始滑出已經出來。查找此日誌中:

06-11 20:08:15.811 D/MyViewFlipper(6106): Stopped a viewflipper crash 

信用:http://code.google.com/p/android/issues/detail?id=6191

+0

我想我犯了一個小錯字 - 2010-07-20 02:21:02

+0

你錯過了一條重要的路線:在catch塊中調用stopFlipping(),以便它調用updateRunning()。 – 2011-05-26 07:27:08

5

這真的很難幫你,如果你不提供你的代碼的某些部分...無論如何,這個錯誤不是新的,有一些解決方法,以解決它(我想是一些錯誤)...嘗試這樣做:

@Override 
protected void onDetachedFromWindow() { 
    try { 
     super.onDetachedFromWindow(); 
    } 
    catch (IllegalArgumentException e) { 
     stopFlipping(); 
    } 
} 

這是爲了覆蓋onDetachedFromWindow,我希望它適合你。

+0

如果您需要代碼,請爲它。 – 2010-06-11 01:56:43

+0

http://code.google.com/p/android/issues/detail?id=6191 – 2010-06-11 02:26:20

+0

昨天晚上我試着實施這個修復程序,但它沒有任何影響。首先,Eclipse不喜歡「受保護」,所以我不得不公開。此外,stopFlipping需要使用findviewById等進行限定。我將調試消息放入新代碼塊中,發現它甚至沒有開啓。 – 2010-06-11 12:30:02

2

我知道這個問題差不多兩年前有人問,但此後一個很簡單的辦法已經實施。只需在清單中的每個活動呼叫中添加android:configChanges="orientation|keyboard|keyboardHidden"即可。

0

是的我沒有看到這個bug之前..但後來我再次在我的清單中有這個

<activity 
     android:name=".MainActivity" 
     android:configChanges="keyboardHidden|navigation" 
     android:label="@string/app_name" 
     android:launchMode="singleTop" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

</activity> 
相關問題