java.lang.RuntimeException:无法启动活动

时间:2013-07-27 04:56:03

标签: android android-layout

我刚刚在Play商店推出了我的第一个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 个答案:

答案 0 :(得分: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;
}

希望它能帮助!!

答案 1 :(得分:0)

其他答案已经讨论过Out of Memory Exception,但既然你已经谈过嵌套权重,那么这可能有助于更好地理解。它可能无法解决您的问题,但可以解决一些问题。

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

<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>

答案 2 :(得分:0)

你忘记在最后为父版面添加结束标记了。 </LinearLayout>

答案 3 :(得分:0)

最好在不需要时从内存中删除位图。 您可以通过回收Bitmap来实现这一目的:

Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
 // Use here your newImage...
 newImage.recycle();
 newImage = null;