应用程序在第4次屏幕旋转时崩溃(第二次在横向上),具有不同的纵向和横向布局

时间:2011-10-16 20:20:22

标签: android android-layout

我有一个应用程序,它使用两种截然不同的纵向和横向布局。纵向模式是主要用例,用户可以在其中控制应用程序,并且包含一个ListView,其周围有一些其他视图元素。横向布局只是在纵向视图中最后选择的内容的信息视图,包含一个填充了WebViews的ViewPager,以及一些与protrait模式相同的视图,但不是全部。当我在Eclipse中以调试模式运行应用程序时,我从纵向旋转到横向,再回到纵向,然后回到横向,它每次都会在最后一次旋转时崩溃到横向(下面的logcat中的堆栈打印输出)。如果我在没有调试模式的手机上运行应用程序,它不会崩溃。堆栈跟踪并没有告诉我崩溃发生的原因。我尝试确保视图在代码中使用它们时不为空。

我的问题看起来类似于this,但在该对话中也没有关于解决方案的进一步细节。谁能给我一个可能出错的线索?我的猜测: 应用程序内存不足(每次旋转都会发生某种内存泄漏,导致4次轮换后发生崩溃)。

或者我应该重构我的应用程序,并以某种方式使两个截然不同的布局两个不同的活动?这两个布局共享足够的功能,使我想要使用相同的活动。

Logcat堆栈跟踪:

10-16 21:53:58.690: ERROR/AndroidRuntime(4494): FATAL EXCEPTION: main
10-16 21:53:58.690: ERROR/AndroidRuntime(4494): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fonkmobile.player22tracks/com.fonkmobile.player22tracks.PlaylistActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.app.ActivityThread.access$2400(ActivityThread.java:125)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.os.Looper.loop(Looper.java:123)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at java.lang.reflect.Method.invoke(Method.java:521)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at dalvik.system.NativeStart.main(Native Method)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:679)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:614)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.app.Activity.setContentView(Activity.java:1647)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at com.fonkmobile.player22tracks.PlaylistActivity.onCreate(PlaylistActivity.java:170)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     ... 12 more
10-16 21:53:58.690: ERROR/AndroidRuntime(4494): Caused by: java.lang.reflect.InvocationTargetException
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.widget.LinearLayout.<init>(LinearLayout.java:115)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at java.lang.reflect.Constructor.constructNative(Native Method)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     ... 25 more
10-16 21:53:58.690: ERROR/AndroidRuntime(4494): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/playlist_expanded_bg.png from drawable resource ID #0x7f02003c
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.content.res.Resources.loadDrawable(Resources.java:1714)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.View.<init>(View.java:1885)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.View.<init>(View.java:1834)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.view.ViewGroup.<init>(ViewGroup.java:285)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     ... 29 more
10-16 21:53:58.690: ERROR/AndroidRuntime(4494): Caused by: java.lang.NullPointerException
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.graphics.Bitmap.createBitmap(Bitmap.java:439)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:342)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:504)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:478)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:325)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
10-16 21:53:58.690: ERROR/AndroidRuntime(4494):     ... 33 more

我的2个布局文件:

RES /布局脊/ playlist.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/playlistLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@android:color/black">

    <android.support.v4.view.ViewPager
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/webviewpager"/>

    <SlidingDrawer android:layout_alignParentBottom="true"
        android:id="@+id/slidingDrawerAudioBar" android:layout_width="fill_parent"
        android:layout_height="70dip" android:layout_marginBottom="117dip"
        android:handle="@+id/audiohandle" android:content="@+id/audiocontent">

        <ImageView android:layout_width="0dip"
            android:layout_height="0dip" android:id="@id/audiohandle" />
        <include android:id="@+id/audiocontent" layout="@layout/volumebar" />
    </SlidingDrawer>
    <SlidingDrawer android:layout_alignParentBottom="true"
        android:id="@+id/slidingDrawerScrubbingBar" android:layout_width="fill_parent"
        android:layout_height="125dip" android:handle="@+id/scrubbinghandle"
        android:layout_marginBottom="117dip" android:content="@+id/scrubbingcontent">
        <ImageView android:layout_width="0dip"
            android:layout_height="0dip" android:id="@id/scrubbinghandle" />
        <include android:id="@+id/scrubbingcontent" layout="@layout/seekbar" />
    </SlidingDrawer>
    <SlidingDrawer android:layout_alignParentBottom="true"
        android:id="@+id/slidingDrawerNowPlaying" android:layout_width="fill_parent"
        android:layout_height="50dip" android:handle="@+id/nowplayinghandle"
        android:content="@+id/nowplayingcontent">
        <ImageView android:layout_width="0dip"
            android:layout_height="0dip" android:id="@id/nowplayinghandle" />
        <include android:id="@+id/nowplayingcontent" layout="@layout/nowplaying"></include>
    </SlidingDrawer>

    <SlidingDrawer android:layout_alignParentBottom="true"
        android:id="@+id/slidingDrawerPlayBar" android:layout_width="fill_parent"
        android:layout_height="125dip" android:handle="@+id/playhandle"
        android:content="@+id/playcontent">

        <ImageView android:layout_width="0dip"
            android:layout_height="0dip" android:id="@id/playhandle" />
        <include android:id="@+id/playcontent" layout="@layout/playbar" />
    </SlidingDrawer>

</RelativeLayout>

RES /布局端口/ playlist.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/playlistLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@android:color/black">

    <android.support.v4.view.ViewPager
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/webviewpager"
    android:visibility = "gone"/>

     <include android:id="@+id/djbar" layout="@layout/djbar" />

    <ListView android:id="@+id/playlistview" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:choiceMode="singleChoice"
        android:longClickable="false" android:cacheColorHint="@android:color/transparent"
        android:listSelector="@android:color/transparent"
        android:drawSelectorOnTop="false"
        android:layout_below="@id/djbar"
        >
    </ListView>

    <ProgressBar android:id="@+id/emptyplaylist"
        style="?android:attr/progressBarStyleLarge" android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:layout_centerInParent="true">
    </ProgressBar>

    <com.fonkmobile.player22tracks.widgets.MultiDirectionSlidingDrawer
        xmlns:my="http://schemas.android.com/apk/res/com.fonkmobile.player22tracks"
        android:id="@+id/genredrawer"
        my:allowSingleTap="true"
        my:animateOnClick = "true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        my:handle="@+id/genrehandle"
        my:content="@+id/genrecontent"
        my:direction="leftToRight">
        <include
            android:id="@id/genrecontent"
            layout="@layout/genrelist" />
        <ImageView
            android:id="@id/genrehandle"
            android:layout_width="40dip"
            android:layout_height="80dip"
             />
    </com.fonkmobile.player22tracks.widgets.MultiDirectionSlidingDrawer>

    <SlidingDrawer android:layout_alignParentBottom="true"
        android:id="@+id/slidingDrawerAudioBar" android:layout_width="fill_parent"
        android:layout_height="70dip" android:layout_marginBottom="117dip"
        android:handle="@+id/audiohandle" android:content="@+id/audiocontent">

        <ImageView android:layout_width="0dip"
            android:layout_height="0dip" android:id="@id/audiohandle" />
        <include android:id="@+id/audiocontent" layout="@layout/volumebar" />
    </SlidingDrawer>
    <SlidingDrawer android:layout_alignParentBottom="true"
        android:id="@+id/slidingDrawerScrubbingBar" android:layout_width="fill_parent"
        android:layout_height="125dip" android:handle="@+id/scrubbinghandle"
        android:layout_marginBottom="117dip" android:content="@+id/scrubbingcontent">
        <ImageView android:layout_width="0dip"
            android:layout_height="0dip" android:id="@id/scrubbinghandle" />
        <include android:id="@+id/scrubbingcontent" layout="@layout/seekbar" />
    </SlidingDrawer>
    <SlidingDrawer android:layout_alignParentBottom="true"
        android:id="@+id/slidingDrawerNowPlaying" android:layout_width="fill_parent"
        android:layout_height="50dip" android:handle="@+id/nowplayinghandle"
        android:content="@+id/nowplayingcontent">
        <ImageView android:layout_width="0dip"
            android:layout_height="0dip" android:id="@id/nowplayinghandle" />
        <include android:id="@+id/nowplayingcontent" layout="@layout/nowplaying"></include>
    </SlidingDrawer>

    <SlidingDrawer android:layout_alignParentBottom="true"
        android:id="@+id/slidingDrawerPlayBar" android:layout_width="fill_parent"
        android:layout_height="125dip" android:handle="@+id/playhandle"
        android:content="@+id/playcontent">

        <ImageView android:layout_width="0dip"
            android:layout_height="0dip" android:id="@id/playhandle" />
        <include android:id="@+id/playcontent" layout="@layout/playbar" />
    </SlidingDrawer>
    <ImageView android:src="@drawable/dragbar_left"
        android:layout_centerVertical="true" android:layout_alignParentLeft="true"
        android:background="@null" android:id="@+id/imageDrawerArrow"
        android:layout_width="26dip" android:layout_height="100dip" 
        android:adjustViewBounds="true"
        android:layout_margin="0dip"></ImageView>


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

这是因为内存泄漏而发生的。

在清单中,向活动部分添加:

android:configChanges="keyboardHidden|orientation"

在活动代码中,实施:

//used in onCreate() and onConfigurationChanged() to set up the UI elements
public void InitializeUI()
{
    //get views from ID's
    this.textViewHeaderMainMessage = (TextView) this.findViewById(R.id.TextViewHeaderMainMessage);

    //etc... hook up click listeners, whatever you need from the Views
}

//Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    InitializeUI();
}

//this is called when the screen rotates.
// (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);

    InitializeUI();
}