0

我的 android 应用由于某种原因无法启动 Activity,Activity 的名称是 Caperucita,但我看不出问题出在哪里,还有另一个像这样运行完美的 Activity,它是 Ricitos。也许你可以帮助我

logcat中的错误:

07-12 01:54:06.849: E/AndroidRuntime(851): FATAL EXCEPTION: main
07-12 01:54:06.849: E/AndroidRuntime(851): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.storyteller/com.example.storyteller.Caperucita}: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.os.Looper.loop(Looper.java:137)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-12 01:54:06.849: E/AndroidRuntime(851):  at java.lang.reflect.Method.invokeNative(Native Method)
07-12 01:54:06.849: E/AndroidRuntime(851):  at java.lang.reflect.Method.invoke(Method.java:511)
07-12 01:54:06.849: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-12 01:54:06.849: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-12 01:54:06.849: E/AndroidRuntime(851):  at dalvik.system.NativeStart.main(Native Method)
07-12 01:54:06.849: E/AndroidRuntime(851): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.LayoutInflater.createView(LayoutInflater.java:613)
07-12 01:54:06.849: E/AndroidRuntime(851):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-12 01:54:06.849: E/AndroidRuntime(851):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.app.Activity.setContentView(Activity.java:1881)
07-12 01:54:06.849: E/AndroidRuntime(851):  at com.example.storyteller.Caperucita.onCreate(Caperucita.java:26)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.app.Activity.performCreate(Activity.java:5104)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-12 01:54:06.849: E/AndroidRuntime(851):  ... 11 more
07-12 01:54:06.849: E/AndroidRuntime(851): Caused by: java.lang.reflect.InvocationTargetException
07-12 01:54:06.849: E/AndroidRuntime(851):  at java.lang.reflect.Constructor.constructNative(Native Method)
07-12 01:54:06.849: E/AndroidRuntime(851):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.LayoutInflater.createView(LayoutInflater.java:587)
07-12 01:54:06.849: E/AndroidRuntime(851):  ... 25 more
07-12 01:54:06.849: E/AndroidRuntime(851): Caused by: java.lang.OutOfMemoryError
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.content.res.Resources.loadDrawable(Resources.java:1965)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.View.<init>(View.java:3330)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.View.<init>(View.java:3259)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.view.ViewGroup.<init>(ViewGroup.java:425)
07-12 01:54:06.849: E/AndroidRuntime(851):  at android.widget.RelativeLayout.<init>(RelativeLayout.java:210)
07-12 01:54:06.849: E/AndroidRuntime(851):  ... 28 more

安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.storyteller"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.storyteller.MainActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="AcercaDe"
          android:screenOrientation="portrait"
          android:theme="@android:style/Theme.NoTitleBar"></activity>

    <activity android:name="Cuentos"
          android:screenOrientation="portrait"
          android:theme="@android:style/Theme.NoTitleBar"></activity>

    <activity android:name="Ricitos"
          android:screenOrientation="portrait"
          android:theme="@android:style/Theme.NoTitleBar"></activity>

    <activity android:name="Caperucita"
          android:screenOrientation="portrait"
          android:theme="@android:style/Theme.NoTitleBar"></activity>

</application>

</manifest>

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

 <ScrollView

     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_marginTop="0dp" >

     <RelativeLayout

    android:id="@+id/rootRL"
    android:layout_width="match_parent"
    android:layout_height="262dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_weight="0.83"
    android:background="@drawable/caperucita_roja"
    android:orientation="vertical" >

         <RelativeLayout
             android:layout_width="800dp"
             android:layout_height="262dp"
             android:layout_alignParentLeft="true"
             android:layout_alignParentTop="true" >

             <ImageButton
                 android:id="@+id/ib3"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentLeft="true"
                 android:layout_alignParentTop="true"
                 android:background="@android:color/transparent"
                 android:onClick="contar"
                 android:src="@drawable/boton_contar_caperucita" />

             <ImageButton
                 android:id="@+id/ib2"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentTop="true"
                 android:layout_toRightOf="@+id/ib3"
                 android:background="@android:color/transparent"
                 android:onClick="play"
                 android:src="@drawable/boton_play_caperucita" />

             <ImageButton
                 android:id="@+id/imageButton3"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentTop="true"
                 android:layout_toRightOf="@+id/ib2"
                 android:background="@android:color/transparent"
                 android:onClick="pause"
                 android:src="@drawable/boton_pausa_caperucita" />

         </RelativeLayout>

</RelativeLayout>
</ScrollView>

</RelativeLayout>

.java文件:

package com.example.storyteller;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;

public class Caperucita extends Cuentos {

MediaPlayer mp;
int  posicion=0;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.caperucita);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


}

public void destruir() {
    if(mp!=null) 
        mp.release();
}


public void contar(View view){
    destruir ();
    mp=MediaPlayer.create(this,R.raw.caperucitaroja);
    mp.start();

    }

public void pause(View view) {
    if(mp != null && mp.isPlaying()) {
        posicion = mp.getCurrentPosition();
        mp.pause();

    }        
}




public void play(View view) {

    if(mp != null && mp.isPlaying()==false) {
        mp.seekTo(posicion);
        mp.start();

    }   

}

protected void onResume() {
    if(mp != null && !mp.isPlaying())
        mp.start();
    super.onResume();
}

public void onPause() {

    super.onPause(); 
if (mp != null)
    mp.pause(); 
}


public void onBackPressed(){
    super.onBackPressed();
    System.exit(0);     
    }

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        exitByBackKey();

        //moveTaskToBack(false);

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

protected void exitByBackKey() {

    AlertDialog alertbox = new AlertDialog.Builder(this)
    .setMessage("¿De verdad desea salir? si lo hace no podrá reproducir el audio desde donde lo dejó")
    .setPositiveButton("Si", new DialogInterface.OnClickListener() {

        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {

            finish();
            //close();


        }
    })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {

        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {
                       }
    })
      .show();

}


}
4

2 回答 2

1

看起来你的背景文件太大了。尝试在下面的相对布局中删除背景属性,看看是否会崩溃。

 <RelativeLayout
    android:id="@+id/rootRL"
    android:layout_width="match_parent"
    android:layout_height="262dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_weight="0.83"
    android:background="@drawable/caperucita_roja"
    android:orientation="vertical" >

要解决这个问题,您需要更多的内存,或者您需要使用更少的内存。获得更多内存实际上是不可能的,但您可以指定更大的堆大小。在您的清单中,使用 largeHeap 属性:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:largeHeap="true" >

您可以在此处阅读有关该属性的更多信息。

要减少所需的内存量,您需要减小该背景图像的大小。我会考虑减小该图像的大小。如果无法减小尺寸,请考虑使用PNGCrush之类的程序优化图像,这有助于减小文件大小而不影响图像质量。 这是如何从命令行使用 PNGCrush 的链接。

于 2013-07-12T02:42:15.793 回答
0

您需要减少图形文件对内存的影响。从堆栈跟踪中可以清楚地看出,系统在加载caperucita_roja后台时内存不足。(那个文件,480x3969,特别大。在内存中未压缩它会占用超过 7MB。在许多设备上,这是应用程序可用总内存的相当大的一部分。)请记住,Android 设备在可用方面受到很大限制记忆。

培训文章Loading Large Bitmaps Efficiently提供了可以帮助解决此问题的策略。

于 2013-07-12T03:19:27.723 回答