0

我想在用户从本地选择照片然后显示在(父)线性布局中之后添加一个具有图像和文本视图的线性布局。添加第一个线性布局没有问题,但是一旦我想输入第二个线性布局,应用程序就会崩溃。

JAVA文件

public class newExpense extends Fragment {

ImageView addImage;
LinearLayout imageList;
final int RQS_LOADIMAGE = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.new_expense_copy, container, false);  

    addImage = (ImageView) rootView.findViewById(R.id.addImage);
    addImage.setOnClickListener(buttonSaveOnClickListener);

    imageList = (LinearLayout) rootView.findViewById(R.id.imageList);

    return rootView;
}

ImageView.OnClickListener buttonSaveOnClickListener
= new ImageView.OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Select an option")
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               })
               .setItems(R.array.addImage, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {

                       if (which == 0) {
                           Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                           startActivityForResult(takePicture, RQS_LOADIMAGE);

                       } else {
                           Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                               android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                           startActivityForResult(pickPhoto , RQS_LOADIMAGE);
                       }
                   } 
               });
            builder.show();
    }
};

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == getActivity().RESULT_OK){
        switch(requestCode){
        case RQS_LOADIMAGE:
            Uri imageUri = data.getData();

            LinearLayout injecterLayout = (LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.image_list, imageList, false);

            ImageView img = (ImageView) injecterLayout.findViewById(R.id.imageView);
            TextView txt = (TextView) injecterLayout.findViewById(R.id.textView1);
            img.setImageURI(imageUri);
            txt.setText(imageUri.getLastPathSegment().toString());
            imageList.addView(injecterLayout);

            break;  
        }
    }
}

}

子布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:maxHeight="100dp"
    android:maxWidth="100dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="100dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_weight="3"
    android:gravity="center_vertical"
    android:minWidth="160dp"
    android:text="test" />

<ImageView
    android:id="@+id/imageRemove"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:src="@android:drawable/ic_delete" />

</LinearLayout>

父布局xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:alignmentMode="alignBounds"
    android:columnCount="2"
    android:columnOrderPreserved="false"
    android:useDefaultMargins="true" >

    <TextView
        android:id="@+id/amountLbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:text="Amount"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:orientation="horizontal" >

        <Spinner
            android:id="@+id/currency"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>

        <EditText
            android:id="@+id/amount"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:hint="Tap to enter amount"
            android:inputType="numberDecimal" />

    </LinearLayout>

    <TextView
        android:id="@+id/accountLbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:text="Paid From"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/account"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="left" />

    <TextView
        android:id="@+id/categoryLbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:text="Category"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/category"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="left" />

    <TextView
        android:id="@+id/dateLbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:text="Date"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Sat 02/03/2013" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_today" />

    </LinearLayout>

    <TextView
        android:id="@+id/noteLbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:text="Note"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/note"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:ems="10"
        android:inputType="textMultiLine" />

    <TextView
        android:id="@+id/locationLbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:text="Location"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="fill_horizontal" >

        <EditText
            android:id="@+id/location"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="left|bottom"
            android:ems="10" >
        </EditText>

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_mylocation" />
    </LinearLayout>

    <TextView
        android:id="@+id/photoLbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:text="Photo"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/addImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_gravity="right"
        android:layout_row="6"
        android:src="@android:drawable/ic_input_add" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_row="7"
        android:layout_columnSpan="1" />

    <LinearLayout
        android:id="@+id/imageList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>


    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_row="6"
        android:layout_columnSpan="1" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="fill_horizontal" >

        <Button
            android:id="@+id/clearBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Clear" />

        <Button
            android:id="@+id/saveBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="Save" />

    </LinearLayout>

</GridLayout>

</ScrollView>

原木猫

03-03 23:14:05.676: E/AndroidRuntime(23792): FATAL EXCEPTION: main
03-03 23:14:05.676: E/AndroidRuntime(23792): java.lang.OutOfMemoryError
03-03 23:14:05.676: E/AndroidRuntime(23792):    at  android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.graphics.drawable.Drawable.createFromStream(Drawable.java:733)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.widget.ImageView.resolveUri(ImageView.java:562)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.widget.ImageView.setImageURI(ImageView.java:340)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at com.example.expensesmanager.newExpense.onActivityResult(newExpense.java:92)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:161)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.app.Activity.dispatchActivityResult(Activity.java:4654)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2987)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3034)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.app.ActivityThread.access$1100(ActivityThread.java:127)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1188)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.os.Looper.loop(Looper.java:137)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at android.app.ActivityThread.main(ActivityThread.java:4511)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at java.lang.reflect.Method.invokeNative(Native Method)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at java.lang.reflect.Method.invoke(Method.java:511)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
03-03 23:14:05.676: E/AndroidRuntime(23792):    at dalvik.system.NativeStart.main(Native Method)

任何人都可以帮助修改一些代码吗?感谢帮助。

4

1 回答 1

1

您内存不足,更具体地说是堆空间不足。你的应用程序可以使用多少堆空间是有限的——这会有所不同,从旧 Android 设备上的低至 16MB 空间到具有高屏幕分辨率的新设备上可能高达 64MB。不管是什么,你都用过了,也许是在位图上。

您可以使用 MAT 找出您的堆空间在哪里被使用

于 2013-03-03T15:36:34.653 回答