2013-07-27 105 views
0

我刚刚推出的Play商店名称印度徽标测验了java.lang.RuntimeException:无法启动活动

而且我收到了一定的崩溃报告我的第一个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按钮坠毁.... 难道是因为在任何情况下嵌套的重量?? 请帮助!...

回答

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

希望它有助于!

+0

我正在使用的代码: 可以通过回收位图做的一种方式 公共位图getBitmap(上下文CTX,字符串pathNameRelativeToAssetsFolder) \t { \t \t的InputStream bitmapIs = NULL; \t \t Bitmap bmp = null; \t \t尝试 \t \t { \t \t \t bitmapIs = ctx.getAssets()。开(pathNameRelativeToAssetsFolder); \t \t bmp = BitmapFactory.decodeStream(bitmapIs); \t \t \t } \t \t 捕\t(IOException的E) \t \t { \t \t e.printStackTrace(); \t \t} \t \t最后 \t \t { \t \t如果(bitmapIs!= NULL) \t \t { \t \t \t尝试 \t \t \t { \t \t \t \t \t bitmapIs.close(); \t \t \t \t \t \t \t \t \t} \t \t \t赶上(IOException的E) \t \t \t \t { \t \t \t \t \t // TODO自动生成的catch程序块 \t \t \t \t \t e.printStackTrace ( ); \t \t \t \t} \t \t} \t \t} \t \t返回BMP; \t} – Vaido

+0

因为我已将所有图像保存在我的资产文件夹中! – Vaido

+0

但只要logcat值得关注,位图中只有内存异常。所以最好检查一下哪种情况会出错? –

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

你忘了把结束标记父布局最后。 </LinearLayout>

0

一旦您不需要它们,从内存中删除位图是一种很好的做法。

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

我收到错误:无法绘制回收的位图! – Vaido

+0

在使用之前,您不应该回收图像。如果确认将来不再使用该图像的引用,那么您应该回收它。否则你会得到NullpointerException。无论如何,你可以发布你的代码在哪里回收? –

相关问题