按下保存按钮后,我的应用程序将崩溃

时间:2017-03-04 13:12:40

标签: java android

public class AddCarActivity extends AppCompatActivity {

    @BindView(R.id.make)
    EditText make;
    @BindView(R.id.year)
    EditText year;
    @BindView(R.id.model)
    EditText model;
    @BindView(R.id.plate_no)
    EditText plateNo;
    @BindView(R.id.carPhoto)
    ImageView carPhoto;
    @BindView(R.id.save)
    Button save;
    private FirebaseDatabase database;

    String imageStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_car);
        ButterKnife.bind(this);

        database = FirebaseDatabase.getInstance();
    }

    @OnClick(R.id.carPhoto)
    public void onClick() {
        ImagePicker.pickImage(this, "Select your image:");
    }

    @OnClick(R.id.save)
    public void onClickSave() {

        if(TextUtils.isEmpty(make.getText().toString())
                || TextUtils.isEmpty(model.getText().toString())
                || TextUtils.isEmpty(plateNo.getText().toString())
                || TextUtils.isEmpty(year.getText().toString())){
            Utils.showToast(AddCarActivity.this,"Please Enter Valid Data");
            return;
        }

        final ProgressDialog progressDialog = Utils.showProgressDialog(AddCarActivity.this);

        DatabaseReference feedsRef = database.getReference("cars");
        try {
            Map<String, Object> map =new HashMap<>();
            map.put("make",make.getText().toString());
            map.put("model",model.getText().toString());
            map.put("plateNo",plateNo.getText().toString());
            map.put("year",year.getText().toString());
            map.put("carId", Utils.getUserId(this));
            map.put("carPhoto",imageStr);
            map.put("userId",Utils.getUserId(AddCarActivity.this));
            feedsRef.push().setValue(map).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    progressDialog.dismiss();
                    AddCarActivity.this.finish();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        try {
            Bitmap bitmap = ImagePicker.getImageFromResult(this, requestCode, resultCode, data);
            imageStr=encodeImage(bitmap);
            carPhoto.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String encodeImage(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String encImage = Base64.encodeToString(b, Base64.DEFAULT);

        return encImage;
    }
}

按下保存按钮后,应用会崩溃。我的代码有问题吗?我是创建应用程序的新手,我无法弄清楚它有什么问题。

这是应用的screenshot(空白)。这里有screenshot证明它正在使用中。

1 个答案:

答案 0 :(得分:0)

在ConstraintLayout中删除match_parent并尝试以下代码:

android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"