在循环中的LinearLayout内膨胀RelativeLayout得到OutOfMemoryError

时间:2014-09-22 22:15:58

标签: java android android-layout

您好我正在尝试在LinearLayout内部膨胀RelativeLayout,但我一直在我的设备中获取OutOfMemoryError。我不太确定我是否应该在一个循环位内膨胀视图我只是不知道如何实现这一点。这段代码工作正常如果我删除循环并只夸大TextView一次。

这是我正在膨胀的布局:

activity_academic_status_period_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/academic_status_period_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<TextView
    android:id="@+id/academic_status_grade"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_margin="20dp"
    android:background="@drawable/academic_status_grade_circle"
    android:gravity="center"
    android:text="20"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#FFF" />

<TextView
    android:id="@+id/academic_status_first_period"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/academic_status_grade"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:text="Nth Period"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#000" >

</TextView>
</RelativeLayout>

编辑

代码,我调用inflatePeriodGrade()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    instanceType = super.instance;
    instanceId = super.instanceId;

    inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    inflatePeriodGrade();
}

这是我活动中的代码:

public void inflatePeriodGrade() {
    AcademicStatusController.getInstance(this);

    ArrayList<PeriodInfo> periodsInfo = AcademicStatusController
            .getPeriodsInfo(instanceType, instanceId);
    Iterator<PeriodInfo> it = periodsInfo.iterator();

    // Find LinearLayout where the period grade will be placed
    LinearLayout llDisplayData = (LinearLayout) findViewById(R.id.help_relative_layout_for_periods);

    while (it.hasNext()) {
        // Inflate the view of the period
        View customView = inflater.inflate(
                R.layout.activity_academic_status_period_layout, null);

        // Remove all the items in the LinearLayout
        llDisplayData.removeAllViews();

        // Set the text to the TextView.
        TextView tvtxt = (TextView) customView
                .findViewById(R.id.academic_status_grade);
        tvtxt.setText(it.next().getName());

        llDisplayData.addView(customView);
    }
}

我需要为期间ArrayList中的每个元素(具有最多5个元素)扩充布局,但它不起作用,我不确定为什么。另外,如何将TextViews添加到另一个右侧,这样我就能得到类似的东西:

我只是试图将包含带有文本的小蓝圈的布局扩展到包含整个视图的RelativeLayout。

修改 StackTrace usging Log.d(“myapp”,Log.getStackTraceString(new Exception()));在onCreate();

09-23 10:27:55.711: D/myapp(23878): java.lang.Exception
09-23 10:27:55.711: D/myapp(23878):     at      com.akdemia.views.AcademicStatusActivity.onCreate(AcademicStatusActivity.java:36)
09-23 10:27:55.711: D/myapp(23878):     at android.app.Activity.performCreate(Activity.java:5248)
09-23 10:27:55.711: D/myapp(23878):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
09-23 10:27:55.711: D/myapp(23878):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
09-23 10:27:55.711: D/myapp(23878):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
09-23 10:27:55.711: D/myapp(23878):     at android.app.ActivityThread.access$800(ActivityThread.java:139)
09-23 10:27:55.711: D/myapp(23878):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
09-23 10:27:55.711: D/myapp(23878):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-23 10:27:55.711: D/myapp(23878):     at android.os.Looper.loop(Looper.java:136)
09-23 10:27:55.711: D/myapp(23878):     at android.app.ActivityThread.main(ActivityThread.java:5086)
09-23 10:27:55.711: D/myapp(23878):     at java.lang.reflect.Method.invokeNative(Native Method)
09-23 10:27:55.711: D/myapp(23878):     at java.lang.reflect.Method.invoke(Method.java:515)
09-23 10:27:55.711: D/myapp(23878):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-23 10:27:55.711: D/myapp(23878):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-23 10:27:55.711: D/myapp(23878):     at dalvik.system.NativeStart.main(Native Method)

修改 这是我试图制作的布局,如下图所示。我也在使用NavigationDrawer。

   <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/carla123456"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/academic_status_banner"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
        android:src="@drawable/banner_team" />

    <TextView
        android:id="@+id/academic_status_average"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="20dp"
        android:background="@color/akdemia_blue"
        android:paddingBottom="5dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="5dp"
        android:text="Promedio: ND"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFF" />

    <TextView
        android:id="@+id/academic_status_subjects"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/academic_status_banner"
        android:background="@color/akdemia_blue"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:text="Materias"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFF" />

    <ListView
        android:id="@+id/academic_status_subjects_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/academic_status_subjects" >
    </ListView>

    <RelativeLayout
        android:id="@+id/help_relative_layout_for_periods"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:layout_centerHorizontal="true" >

    </RelativeLayout>
</RelativeLayout>

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<!-- should not be larger than 320 to show content -->

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

enter image description here

0 个答案:

没有答案