【问题标题】:Switch case statements causes fatal error in my Android AppSwitch case 语句在我的 Android 应用程序中导致致命错误
【发布时间】:2011-05-17 03:20:57
【问题描述】:

我正在构建一个 Android 应用,使用一堆 switch 语句在视图之间切换。一切都很顺利,直到我添加了一个在应用程序启动时导致致命错误的案例。这些案例由 XML 中的 onClick 语句调用。有问题的案例块实际上是可以正常工作的语句块的一部分。谁能看到我错过了什么?是什么导致了致命错误?我在 logCat 文件中包含了损坏代码和工作代码的 sn-ps。

05-17 01:48:01.931: INFO/System.out(270): debugger has settled (1381)
05-17 01:48:02.341: WARN/dalvikvm(270): VFY: invalid switch target 7170 (-> 0x1c0b) at 0x9[0]
05-17 01:48:02.351: WARN/dalvikvm(270): VFY:  rejected Lcom/findthway/main;.nextLayout (Landroid/view/View;)V
05-17 01:48:02.351: WARN/dalvikvm(270): Verifier rejected class Lcom/findthway/main;
05-17 01:48:02.351: WARN/dalvikvm(270): Class init failed in newInstance call (Lcom/findthway/main;)
05-17 01:48:03.031: INFO/ActivityManager(67): Displayed activity com.android.launcher/com.android.launcher2.Launcher: 50893 ms (total 50893 ms)
05-17 01:48:08.001: WARN/ActivityManager(67): Launch timeout has expired, giving up wake lock!
05-17 01:48:08.751: WARN/ActivityManager(67): Activity idle timeout for HistoryRecord{43eda500 com.findthway/.main}
05-17 01:52:41.531: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
05-17 01:57:41.551: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
05-17 02:02:41.576: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
05-17 02:07:41.641: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
05-17 02:08:45.831: DEBUG/dalvikvm(67): GC_FOR_MALLOC freed 15592 objects / 665856 bytes in 160ms
05-17 02:12:41.695: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
05-17 02:16:30.392: DEBUG/dalvikvm(67): GC_FOR_MALLOC freed 4697 objects / 200928 bytes in 108ms
05-17 02:17:41.716: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
05-17 02:22:41.745: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
05-17 02:27:41.801: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
05-17 02:32:41.812: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
05-17 02:37:41.851: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
05-17 02:41:00.341: DEBUG/dalvikvm(67): GC_FOR_MALLOC freed 12690 objects / 520224 bytes in 235ms

将调用损坏的 switch 案例的 XML 示例:

<RelativeLayout android:id="@+id/level07_layout00"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/vlayoutup"
android:visibility="gone">
    <ImageButton android:id="@+id/level07_closeButton00"
    android:layout_height="wrap_content" 
    android:background="@drawable/close" 
    android:layout_width="wrap_content" 
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp"
    android:onClick="closetomain">
    </ImageButton>
    <ImageButton android:id="@+id/level07_leftButton00"
    android:layout_height="wrap_content" 
    android:background="@drawable/node" 
    android:layout_width="wrap_content" 
    android:layout_alignParentTop="true"
    android:layout_marginTop="2dp"
    android:layout_marginLeft="135dp"
    android:onClick="nextLayout">
    </ImageButton>
    <ImageButton android:id="@+id/level07_rightButton00"
    android:layout_height="wrap_content" 
    android:background="@drawable/node" 
    android:layout_width="wrap_content" 
    android:layout_alignParentTop="true"
    android:layout_marginTop="2dp"
    android:layout_marginLeft="340dp"
    android:onClick="nextLayout">
    </ImageButton>
    <ImageView android:id="@+id/level07_beginButton00"
    android:layout_height="wrap_content" 
    android:background="@drawable/beginbutton" 
    android:layout_width="wrap_content" 
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="235dp">
    </ImageView>
    <ImageView android:id="@+id/level07_ImageView00" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="325dp"
    android:background="@drawable/rocket">
    </ImageView>
</RelativeLayout> 

<RelativeLayout android:id="@+id/level07_layout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/vlayoutup"
android:visibility="gone">
    <ImageButton android:id="@+id/level07_closeButton01"
    android:layout_height="wrap_content" 
    android:background="@drawable/close" 
    android:layout_width="wrap_content" 
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp"
    android:onClick="closetomain">
    </ImageButton>
    <ImageButton android:id="@+id/level07_leftButton01"
    android:layout_height="wrap_content" 
    android:background="@drawable/node" 
    android:layout_width="wrap_content" 
    android:layout_alignParentTop="true"
    android:layout_marginTop="2dp"
    android:layout_marginLeft="135dp"
    android:onClick="nextLayout">
    </ImageButton>
    <ImageButton android:id="@+id/level07_rightButton01"
    android:layout_height="wrap_content" 
    android:background="@drawable/node" 
    android:layout_width="wrap_content" 
    android:layout_alignParentTop="true"
    android:layout_marginTop="2dp"
    android:layout_marginLeft="340dp"
    android:onClick="nextLayout">
    </ImageButton>
    <ImageButton android:id="@+id/level07_backButton01"
    android:layout_height="wrap_content" 
    android:background="@drawable/backlevel" 
    android:layout_width="wrap_content" 
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="235dp"
    android:onClick="prevLayout">
    </ImageButton>
</RelativeLayout> 

这是损坏的java的sn-p:

    case R.id.level07_backButton01:
        RelativeLayout bB017 = (RelativeLayout)findViewById(R.id.level07_layout00);
        bB017.setVisibility(View.VISIBLE);
        RelativeLayout bB017b = (RelativeLayout)findViewById(R.id.level07_layout01);
        bB017b.setVisibility(View.GONE);
        break;
    case R.id.level07_backButton02:
        RelativeLayout bB027b = (RelativeLayout)findViewById(R.id.level07_layout01);
        bB027b.setVisibility(View.VISIBLE);
        RelativeLayout bB027 = (RelativeLayout)findViewById(R.id.level07_layout02);
        bB027.setVisibility(View.GONE);
        break;
    case R.id.level07_backButton03:
        RelativeLayout bB037 = (RelativeLayout)findViewById(R.id.level07_layout02);
        bB037.setVisibility(View.VISIBLE);
        RelativeLayout bB037b = (RelativeLayout)findViewById(R.id.level07_layout03);
        bB037b.setVisibility(View.GONE);
        break;

这是在同一块中工作的代码的 sn-p:

case R.id.level07_backButton16:
        RelativeLayout bB167 = (RelativeLayout)findViewById(R.id.level07_layout15);
        bB167.setVisibility(View.VISIBLE);
        RelativeLayout bB167b = (RelativeLayout)findViewById(R.id.level07_layout16);
        bB167b.setVisibility(View.GONE);
        break;
    case R.id.level07_backButton17:
        RelativeLayout bB177 = (RelativeLayout)findViewById(R.id.level07_layout13);
        bB177.setVisibility(View.VISIBLE);
        RelativeLayout bB177b = (RelativeLayout)findViewById(R.id.level07_layout17);
        bB177b.setVisibility(View.GONE);
        break;
    case R.id.level07_backButton18:
        RelativeLayout bB187 = (RelativeLayout)findViewById(R.id.level07_layout17);
        bB187.setVisibility(View.VISIBLE);
        RelativeLayout bB187b = (RelativeLayout)findViewById(R.id.level07_layout18);
        bB187b.setVisibility(View.GONE);
        break;

如您所见,我认为这两个 switch case 块是相同的。我认为重要线索的部分在 logCat: VFY: invalid switch target 中。

如果您需要更多信息,请告诉我。提前感谢您的帮助!

【问题讨论】:

  • 我没有看到 backButton02 或 03 定义。 (或后来的 16/17/18 - 这是整个 xml 吗?
  • 这不是整个 XML,只是一个示例。这几乎是一堆相似的块,但它们都有相同的图像按钮,用于返回、关闭和下一步。
  • 你在这个 xml 中有多少视图?有多少布局?假设您在 Button02 或 Button03 上的 xml 中没有错字,您是否可能超出了 #of 视图或布局的限制?
  • jkhouw1 - 你在正确的轨道上,但事实证明情况太多了。谁知道?我以前从未见过这种情况,但这只是我在 Java 上的第二次尝试。

标签: android xml view onclick switch-statement


【解决方案1】:

使用默认情况来捕获未指定的情况,但这不是必需的。

检查the switch statement

【讨论】:

  • 感谢您的建议,但这是否有助于解决这些似乎导致的奇怪致命错误?
【解决方案2】:

方法有多大?什么版本的安卓?

http://code.google.com/p/android/issues/detail?id=11552。

不看代码我不能保证这是同一个问题,但听起来很相似。您可以通过在 APK 上运行“dexdump”并搜索方法来检查方法的大小。

【讨论】:

    【解决方案3】:

    原来switch语句的case太多了。我不知道有限制,但我想有。我将 switch 语句分解为单独的方法,这就成功了。谢谢大家的建议!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      相关资源
      最近更新 更多