简体   繁体   English

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

[英]My app will crash as soon as i press on save button

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;
    }
}

The app will crash as soon as I press on save button. 按保存按钮后,该应用程序将崩溃。 Is there something wrong with my code? 我的代码有问题吗? I'm quite new in creating apps and I can't figure out what's wrong with it. 我在创建应用程序方面还很陌生,我无法弄清这是怎么回事。

Here's a screenshot of the app (blank). 这是该应用程序的屏幕截图 (空白)。 And here's a screenshot demonstrating it in use. 这是一个截图,演示了它的使用。

In your ConstraintLayout remove match_parent and try the code below: 在您的ConstraintLayout中,删除match_parent并尝试以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM