0

我刚刚在 Play 商店名称 Indian Logo Quiz上启动了我的第一个 Android 应用程序

我收到了某些崩溃报告。其中一个是这个:

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.indian.logoquiz/com.indian.logoquiz.Play}:
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class     <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at              com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:    56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
at android.app.Activity.setContentView(Activity.java:1928)
at com.indian.logoquiz.Play.onCreate(Play.java:39)
at android.app.Activity.performCreate(Activity.java:5250)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:593)
... 23 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
at android.content.res.Resources.loadDrawable(Resources.java:2854)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3460)
at android.view.ViewGroup.<init>(ViewGroup.java:446)
at android.widget.LinearLayout.<init>(LinearLayout.java:176)
at android.widget.LinearLayout.<init>(LinearLayout.java:172)
... 26 more

我的play.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/playpage">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.33"
    android:layout_marginTop="5dp" 
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp">

    <Button
        android:id="@+id/lvl1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.33"
        android:alpha="0.85"
        android:text="" 

        />


    <Button
        android:id="@+id/lvl2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.33"
        android:alpha="0.85"
        android:text=""
        />


    <Button
        android:id="@+id/lvl3"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.34"
        android:alpha="0.85"
        android:text="" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.33"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" >

    <Button
        android:id="@+id/lvl4"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.33"
        android:alpha="0.85"
        android:text="" />

    <Button
        android:id="@+id/lvl5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.33"
        android:alpha="0.85"
        android:text="" />

    <Button
        android:id="@+id/lvl6"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.34"
        android:alpha="0.85"
        android:text="" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.33" 
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp">

    <Button
        android:id="@+id/lvl7"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.33"
        android:alpha="0.85"
        android:text="" />

    <Button
        android:id="@+id/lvl8"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.33"
        android:alpha="0.85"
        android:text="" />

    <Button
        android:id="@+id/lvl9"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0.34"
        android:alpha="0.85"
        android:text="" />

</LinearLayout>

</LinearLayout>

该应用程序在按下 1 级按钮时崩溃.... 可能是因为嵌套重量在任何情况下?请帮忙 !....

4

4 回答 4

0

其他答案已经讨论过Out of Memory Exception,但是由于您已经讨论过嵌套权重,因此这里可能有助于更好地理解这一点。它可能无法解决您的问题,但可以清除一些事情。

通常有两个参数一起使用很多次:
android:weightSum="1"android:layout_weight。如果您未在外部布局中指定 weightSum,则将其视为1. 外部布局内所有的总和android:layout_weight应该等于该外部布局的权重总和。例如:如果您在 a 中有 3 个 Button,LinearLayout并且您想为它们定义相等的权重,那么您可以按如下方式进行。

<LinearLayout
    ...
    android:weightSum="3"
     >

    <Button
       ...
       android:layout_weight="1"
     />

    <Button
       ...
       android:layout_weight="1"
     />

    <Button
       ...
       android:layout_weight="1"
     />

</LinearLayout>

同样,您可以为布局做:

<LinearLayout
    android:weightSum="3"
    >

    <LinearLayout
        ...
         android:layout_weight="1"
    />

    <LinearLayout
        ...
         android:layout_weight="1"
    />

    <LinearLayout
        ...
         android:layout_weight="1"
    />

</LinearLayout>
于 2013-07-27T05:13:02.730 回答
0

当您不需要位图时,最好从内存中删除它们。您可以通过回收位图来做到这一点:

Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
 // Use here your newImage...
 newImage.recycle();
 newImage = null;
于 2013-07-27T06:49:33.547 回答
0

您的活动中的图像出现内存不足异常。

当任何具有小堆或缓冲区大小的设备使用应用程序时,它可能会出现内存异常,因为您可能使用了高分辨率图像。

尝试下面的代码来减少应用程序位图的内存使用。

private Bitmap decodeFile(File f){
    Bitmap b = null;

        //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    FileInputStream fis = new FileInputStream(f);
    BitmapFactory.decodeStream(fis, null, o);
    fis.close();

    int scale = 1;
    if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
        scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / 
           (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    fis = new FileInputStream(f);
    b = BitmapFactory.decodeStream(fis, null, o2);
    fis.close();

    return b;
}

希望能帮助到你!!

于 2013-07-27T05:11:34.223 回答
0

你忘了把父布局的结束标签放在最后。</LinearLayout>

于 2013-07-27T05:22:30.390 回答