我创建的Android App Forceclose

时间:2014-08-23 07:00:47

标签: android android-layout android-activity forceclose

所以我有这个XML问题就是当我打开它时它让我的力量接近。当我删除一些图像视图时,让我们说只有7个图像视图在XML中工作。知道为什么吗?当我的xml上有更多图像时,我不知道为什么它不起作用

App3.XML

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/JCBG"
    android:gravity="center"
    android:orientation="vertical">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:text="Products"
            android:textColor="@color/JCLogo"
            android:textSize="30dp"/>

        <ViewFlipper
            android:id="@+id/fliping"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <!--  The child Views/Layout to flip -->

        <!--  Layout 1 for 1st Screen -->

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center_vertical|center_horizontal|center"
                android:orientation="vertical" >

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/gr" />

            </LinearLayout>

             <!--  Layout 2 for 2nd Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/calvitcdetails" />

            </LinearLayout>

             <!--  Layout 3 for 3rd Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/kapedetails"/>

            </LinearLayout>

              <!--  Layout 4 for 4th Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView4"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/glutafitdetails"/>

            </LinearLayout>

              <!--  Layout 5 for 5th Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView5"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/pgtdetails"/>

            </LinearLayout>


              <!--  Layout 6 for 6th Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView6"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/hilifedetails"/>

            </LinearLayout>


                <!--  Layout 7 for 7th Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView7"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/organicbarleyjuicedetails"/>

            </LinearLayout>

                <!--  Layout 8 for 8th Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView8"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/barleydetails"/>

            </LinearLayout>

             <!--  Layout 9 for 9th Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView9"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/spirulinadetails"/>

            </LinearLayout>

              <!--  Layout 10 for 10th Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView10"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/omnisoapdetails"/>

            </LinearLayout>


             <!--  Layout 11 for 11th Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView11"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/omnipinkishdetails"/>

            </LinearLayout>


            <!--  Layout 12 for 12th Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView12"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/omnidaydetails"/>

            </LinearLayout>


            <!--  Layout 13 for 13th Screen -->

            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView13"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="15dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/omnitonerdetails"/>

            </LinearLayout>


        </ViewFlipper>

</LinearLayout>

Activity3.java

    package com.example.jcw;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

public class activity3 extends Activity 
{

    private ViewFlipper vf;
    private float lastX;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.app3);
    vf = (ViewFlipper) findViewById(R.id.fliping);
    }



    public boolean onTouchEvent(MotionEvent touchevent) {
    switch (touchevent.getAction()) {
    case MotionEvent.ACTION_DOWN: {
    lastX = touchevent.getX();
    break;
    }
    case MotionEvent.ACTION_UP: {
    float currentX = touchevent.getX();
    if (lastX < currentX) {
        if (vf.getDisplayedChild() == 0)
            break;
            vf.setInAnimation(this, R.anim.in_from_left);
            vf.setOutAnimation(this, R.anim.out_to_right);
            vf.showNext();
    }
    if (lastX > currentX) {
        if (vf.getDisplayedChild() == 1)
            break;
            vf.setInAnimation(this, R.anim.in_from_right);
            vf.setOutAnimation(this, R.anim.out_to_left);
            vf.showPrevious();
            }
            break;
            }
        }   
        return false;
}




}

3 个答案:

答案 0 :(得分:1)

ImageView小部件使用消耗大量堆空间的Bitmaps。因此,如果您尝试同时加载到许多图像,您将获得可能在Logcat中的OutOfMemoryError

如果位图分辨率高于显示所需的分辨率,您可以优化加载它们,here您将找到Google的示例。

如果不是这样,那么你必须以不同的方式设计你的应用程序。

答案 1 :(得分:0)

在appmanifest中添加: 机器人:largeHeap = “真”

答案 2 :(得分:0)

您可以将<ViewFlipper ... </ViewFlipper>更改为此<AdapterViewFlipper /> 为此你需要将你的布局安排到这个:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/JCBG"
android:gravity="center"
android:orientation="vertical">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Products"
        android:textColor="@color/JCLogo"
        android:textSize="30dp"/>

    <AdapterViewFlipper 
        android:id="@+id/flipper"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"/>

</LinearLayout>  

然后你需要创建新的布局并将代码放在下面:

mLayout.xml

<LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:gravity="center"
         android:orientation="vertical">

         <ImageView
             android:id="@+id/imageView"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_marginTop="15dp"
             android:scaleType="fitXY"/>

</LinearLayout>  
获取adapterViewFlipper视图设置适配器后,在主活动中

adapterViewFlipper.setAdapter(new BAdapter(this,yourImageResourceArray));

在此之后你应该创建自定义适配器(例如扩展基本适配器)并在其中获取view方法,膨胀mLayout.xml和findView of ImageView,然后为它设置你的Image。

这是您需要的customAdapter示例:

 public class Badapter extends BaseAdapter{

Context context;
ArrayList<Integer> imageResources;

public Badapter(Context context,ArrayList<Integer> imageResources) {
    this.context = context;
    this.imageResources = imageResources;
}


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return imageResources.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return imageResources.get(arg0);
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View arg1, ViewGroup arg2) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater
            .inflate(R.layout.mLayout, null, true);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
    imageView.setBackgroundResource(imageResources.get(position));
    //if you use view Holder you will save more memory
    return rowView;
}

}  

如果这样做,视图将回收并使用更少的内存 注意:如果您使用大小超过50Kb的图像,在某些设备中,您的应用程序将崩溃。因此您应该使用较小尺寸的图像。