运行时异常、view.InflateException、reflection.InvocationTargetException 和 OutOfMemoryError
Posted
技术标签:
【中文标题】运行时异常、view.InflateException、reflection.InvocationTargetException 和 OutOfMemoryError【英文标题】:Runtime Exception,view.InflateException,reflection.InvocationTargetException and OutOfMemoryError 【发布时间】:2014-04-25 21:56:30 【问题描述】:我正在为 android 开发我的第一个游戏。我一直在我的 android 手机(三星 Galaxy S2)中运行和测试我的代码。在我的游戏的最后阶段,我添加了 google admob 服务。曾经添加它后,我的游戏开始变慢。游戏在某个时间点关闭,并在 logcat 中列出了异常列表。这些 logcat 条目都没有让我知道我的错误来自什么以及来自哪里。
这是我的 logcat 条目,
03-19 19:53:34.638: D/AndroidRuntime(637): Shutting down VM
03-19 19:53:34.638: W/dalvikvm(637): threadid=1: thread exiting with uncaught exception (group=0x41587ba8)
03-19 19:53:34.648: E/AndroidRuntime(637): FATAL EXCEPTION: main
03-19 19:53:34.648: E/AndroidRuntime(637): Process: btdw.agilan.minesweeper, PID: 637
03-19 19:53:34.648: E/AndroidRuntime(637): java.lang.RuntimeException: Unable to start activity ComponentInfobtdw.agilan.minesweeper/btdw.agilan.minesweeper.MenuPage: android.view.InflateException: Binary XML file line #111: Error inflating class <unknown>
03-19 19:53:34.648: E/AndroidRuntime(637): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.os.Handler.dispatchMessage(Handler.java:102)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.os.Looper.loop(Looper.java:136)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-19 19:53:34.648: E/AndroidRuntime(637): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 19:53:34.648: E/AndroidRuntime(637): at java.lang.reflect.Method.invoke(Method.java:515)
03-19 19:53:34.648: E/AndroidRuntime(637): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-19 19:53:34.648: E/AndroidRuntime(637): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-19 19:53:34.648: E/AndroidRuntime(637): at dalvik.system.NativeStart.main(Native Method)
03-19 19:53:34.648: E/AndroidRuntime(637): Caused by: android.view.InflateException: Binary XML file line #111: Error inflating class <unknown>
03-19 19:53:34.648: E/AndroidRuntime(637): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-19 19:53:34.648: E/AndroidRuntime(637): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.app.Activity.setContentView(Activity.java:1929)
03-19 19:53:34.648: E/AndroidRuntime(637): at btdw.agilan.minesweeper.MenuPage.onCreate(MenuPage.java:80)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.app.Activity.performCreate(Activity.java:5231)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-19 19:53:34.648: E/AndroidRuntime(637): ... 11 more
03-19 19:53:34.648: E/AndroidRuntime(637): Caused by: java.lang.reflect.InvocationTargetException
03-19 19:53:34.648: E/AndroidRuntime(637): at java.lang.reflect.Constructor.constructNative(Native Method)
03-19 19:53:34.648: E/AndroidRuntime(637): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
03-19 19:53:34.648: E/AndroidRuntime(637): ... 23 more
03-19 19:53:34.648: E/AndroidRuntime(637): Caused by: java.lang.OutOfMemoryError
03-19 19:53:34.648: E/AndroidRuntime(637): at java.io.BufferedInputStream.<init>(BufferedInputStream.java:96)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.gesture.GestureStore.load(GestureStore.java:281)
03-19 19:53:34.648: E/AndroidRuntime(637): at android.gesture.GestureStore.load(GestureStore.java:275)
03-19 19:53:34.648: E/AndroidRuntime(637): at com.google.ads.internal.k.a(SourceFile:42)
03-19 19:53:34.648: E/AndroidRuntime(637): at com.google.ads.AdView.a(SourceFile:514)
03-19 19:53:34.648: E/AndroidRuntime(637): at com.google.ads.AdView.a(SourceFile:370)
03-19 19:53:34.648: E/AndroidRuntime(637): at com.google.ads.AdView.<init>(SourceFile:125)
03-19 19:53:34.648: E/AndroidRuntime(637): ... 26 more
03-19 19:54:35.163: I/Process(637): Sending signal. PID: 637 SIG: 9
我什至找不到错误是来自 java 代码还是 xml 文件。请帮我弄清楚这一点。
注意:在我的东芝at10-a(平板电脑)中,此游戏运行流畅,没有此类异常。
我希望我的游戏以相同的方式在所有设备上运行。请帮助我完成我的第一款游戏。
这是我的 XML 文件 menupage.xml,
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/awesomepager"
android:layout_
android:layout_>
<RelativeLayout
android:id="@+id/rLayout"
android:layout_
android:layout_
android:background="@drawable/waterglass" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/menublanket"
android:orientation="vertical" >
<Button
android:id="@+id/bHighScores"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="High Scores" />
<Button
android:id="@+id/bNewGame"
android:layout_
android:layout_
android:layout_above="@+id/bHighScores"
android:layout_centerHorizontal="true"
android:text="New Game" />
<Button
android:id="@+id/bExit"
android:layout_
android:layout_
android:layout_below="@+id/bHighScores"
android:layout_centerHorizontal="true"
android:text="Exit" />
</RelativeLayout>
<ImageView
android:id="@+id/ivAnimatedSmiley"
android:layout_
android:layout_
android:layout_above="@id/relativeLayout1"
android:layout_marginBottom="60dp"
android:layout_centerHorizontal="true"
/>
<btdw.agilan.minesweeper.Title
android:id="@+id/tvMinesweeperTitleOut"
android:layout_
android:layout_
android:layout_above="@+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:text="Minesweeper"
android:textColor="#ff0000"
android:textSize="30dp"
android:textStyle="bold" />
<btdw.agilan.minesweeper.Title
android:id="@+id/tvMinesweeperTitleIn"
android:layout_
android:layout_
android:layout_above="@id/relativeLayout1"
android:layout_centerHorizontal="true"
android:text="Minesweeper"
android:textColor="#ffffff"
android:textSize="30dp"
android:textStyle="bold"
android:visibility="invisible" />
<ImageButton
android:id="@+id/ibSettingsImage"
android:layout_
android:layout_
android:background="@drawable/settings"
android:layout_below="@id/relativeLayout1"
android:layout_alignLeft="@id/relativeLayout1"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
/>
<ImageView
android:id="@+id/ivLikeUs"
android:layout_
android:layout_
android:src="@drawable/fb"
android:layout_alignRight="@id/relativeLayout1"
android:layout_below="@id/relativeLayout1"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
/>
<com.google.ads.AdView android:id="@+id/googleAd"
android:layout_
android:layout_centerHorizontal="true"
android:layout_
ads:adUnitId="a15318aa0c2dc06"
ads:loadAdOnCreate="true"
ads:unLoadOnPause="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
/>
</RelativeLayout>
</android.support.v4.view.ViewPager>
【问题讨论】:
您是在夸大重图像还是大量图像,OutOfMemoryError?在 UI 线程中? 向我们展示那个 XML 文件 【参考方案1】:查看此 Android 开发者链接。并将图像放在适当大小的适当文件夹中。当您没有合适大小的图像时,操作系统会尝试自行调整图像大小,这会导致内存不足异常。
https://developer.android.com/guide/practices/screens_support.html
【讨论】:
我已经完成了。我将具有不同 dpi 的图像放在不同的可绘制文件夹中。以上是关于运行时异常、view.InflateException、reflection.InvocationTargetException 和 OutOfMemoryError的主要内容,如果未能解决你的问题,请参考以下文章