我的Android应用程序由于某种原因无法启动Activity,Activity的名称是Caperucita,但是我看不出问题是什么,还有另外一个运行完美的活动,并且是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
android清单:
<?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();
}
}
答案 0 :(得分: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" >
您可以阅读有关该属性here的更多信息。
要减少所需的内存量,您需要减小该背景图像的大小。我会考虑缩小图像的大小。如果无法缩小尺寸,请尝试使用PNGCrush等程序优化图像,这有助于缩小文件大小而不影响图像质量。 Here's a link to how to use PNGCrush from the command line.
答案 1 :(得分:0)
您需要减少图形文件的内存影响。从堆栈跟踪中可以清楚地看到系统在加载caperucita_roja
背景时内存不足。 (该文件位于480x3969,特别大。在内存中未压缩,将占用超过7MB。在许多设备上,这相当于应用程序可用内存总量的很大一部分。)请记住,Android设备在可用方面受到很大限制存储器中。
培训文章Loading Large Bitmaps Efficiently提供了有助于此的策略。