首页 > 解决方案 > 每次上传第三张图片时,Android Java App 都会崩溃

问题描述

我有一个小应用程序,它允许用户上传一些基本数据(姓名、学年、级别),然后上传一张 w/e 的图片。所以一切都按预期工作,直到上传第三张图片,应用程序重新启动,我什至没有收到错误消息。

所以我有一个主要活动,它使用对话框来允许某些数据输入,并在对话框中将数据保存到主要活动中。当用户上传完数据后,他们可以保存所有数据并继续进行最后的活动,其中显示了一个列表视图,显示了所有已输入的数据。

所以这个问题特别是在用户上传第三张图片时上传第二张图片然后继续进行最后的活动,它只是重新启动而没有错误消息或任何东西。但是,如果您上传一堆没有图像的东西,您可以根据需要添加和查看。

所以我为我的学生对象和数组适配器设置了一个类,然后是我的活动。我将在下面发布所有相关代码。我已经研究过阵列适配器的问题,但我不认为这是具体的问题,因为它在没有图像时工作正常,并且在添加 2 个图像时工作。所以我真的不明白问题是什么,特别是因为我没有收到任何错误消息。

代码如下

主要活动


public class MainActivity extends AppCompatActivity  {
    static final int REQUEST_IMAGE_CAPTURE = 1;
    ImageView myImage;
    Student myself;
    ArrayList<Student> studentArrayList;
    EditText myName;
    Bitmap imageBitmap;
    static Bitmap img;
    Button finalSave;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myImage = (ImageView) findViewById(R.id.personalImageID);
        myName = (EditText) findViewById(R.id.studentName);
        finalSave = (Button)findViewById(R.id.finalSave);
        myself = new Student();
        studentArrayList = new ArrayList<Student>(0);
        finalSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                endMainActivity();
            }
        });

    }
    private void endMainActivity()
    {
        Intent studentListIntent =  new Intent(this, ReportActivity.class);
        System.out.println("Main Activity Student List size : " + studentArrayList.size());
        studentListIntent.putParcelableArrayListExtra("students", studentArrayList);
        startActivity(studentListIntent);
    }
    @Override
    protected void onResume() {
        super.onResume();
        myself = new Student();
    }

    public void upload(View view) {
        Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(pictureIntent.resolveActivity(getPackageManager()) != null)
            startActivityForResult(pictureIntent, REQUEST_IMAGE_CAPTURE);
    }


    public void addYearAndLevel(View view) {
        myself.studentName =  myName.getText().toString();
        AddYearAndLevelDialog dialog = new AddYearAndLevelDialog();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        dialog.show(transaction, "fragment");
    }

    public void addToList()
    {
        myself.imageData = img;
        studentArrayList.add(myself);
        System.out.println(myself.studentName + " " + myself.level + " " + myself.year + " " + myself.imageData );
        myName.setText("");
        myself = new Student();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            imageBitmap = (Bitmap) extras.get("data");
            myImage.setImageBitmap(imageBitmap);
            img = imageBitmap;
        }
    }

}

视图控制器


public class ReportActivity extends AppCompatActivity {
    ListView list;
    ArrayList<Student> students;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        students = getIntent().getParcelableArrayListExtra("students");
        System.out.println("Student List Size : " + students.size());
        list = findViewById(R.id.studentList);
        StudentAdapter adapter = new StudentAdapter(getApplicationContext(), students);
        list.setAdapter(adapter);

    }

}

用于查看的xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".ReportActivity">

    <ListView
        android:id="@+id/studentList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>

适配器


public class StudentAdapter extends BaseAdapter {
    Context context;
    ArrayList<Student> students;
    LayoutInflater inflater;
    View view;

    public StudentAdapter(Context appContext, ArrayList<Student>students)
    {
        this.context = appContext;
        this.students = students;
        inflater = LayoutInflater.from(this.context);
    }
    @Override
    public int getCount() {
        return students.size();
    }

    @Override
    public Object getItem(int position) {
        return students.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        view =  inflater.inflate(R.layout.list_item, null);

        ImageView image = (ImageView) view.findViewById(R.id.imageUploaded);
        TextView studentName = (TextView) view.findViewById(R.id.studName);
        TextView studYearLvl = (TextView) view.findViewById(R.id.lvlAndYear);
        image.setImageBitmap(students.get(position).imageData);
        studentName.setText(students.get(position).studentName);
        studYearLvl.setText("Level: " + students.get(position).level + "  " + "Year: " + " " + students.get(position).year);

        return view;
    }
}

列表显示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/imageUploaded"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
            <TextView
                android:id="@+id/studName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                />
        <TextView
            android:id="@+id/lvlAndYear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>


</LinearLayout>

标签: javaandroid

解决方案


我解决了这个问题。此特定问题是位图使用的图像大小造成的。降低设备相机中的图像分辨率可解决此问题。


推荐阅读