2013-07-27 103 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。無論如何,你可以發佈你的代碼在哪裏回收? –

相關問題