0

你好!

我的 Android 应用程序有一些问题,我只在一个班级工作(我知道这很糟糕,但现在它对我来说是最快的选择),我有基本活动布局(activity_main.xml),我在这个布局上有 2 个按钮,其中一个按钮具有 setOnClickListener 以打开文件管理器以选择照片并将其添加到photo.xml.

imageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                startActivityForResult(i, ACTIVITY_SELECT_IMAGE);

            }
        });
protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
            case 1234:
                if(resultCode == RESULT_OK){
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};

                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String filePath = cursor.getString(columnIndex);
                    cursor.close();


                    Bitmap SelectedImage = BitmapFactory.decodeFile(filePath);
                    setContentView(R.layout.photo);
                    ImageView img = findViewById(R.id.image);



                    int screenWidth = DeviceDimensionsHelper.getDisplayWidth(this);
                    int screenHeight = DeviceDimensionsHelper.getDisplayHeight(this);

                    Bitmap SelectedImageScaled = Bitmap.createScaledBitmap(SelectedImage, screenWidth, screenHeight, true);
                    img.setImageBitmap(SelectedImageScaled);

                }
            }
        }

然后,我有 OnTouchEvent,我正在搜索具有 id=photo 的 ConstraintLayout,它在 中photo.xml但是如果我在其中main_activity.xml,然后如果我单击按钮以外的其他位置,应用程序将崩溃,bcs 它正在寻找 ConstraintLayout inphoto.xml但应用仍在main_acitivity.xml.

    public boolean onTouchEvent(MotionEvent event) {
        
            switch (event.getAction()) {

                case MotionEvent.ACTION_UP:
                    int x = (int)event.getX();
                    int y = (int)event.getY();
                    TextView tv;
                    if(i < 2) {
                        tv = new TextView(MainActivity.this);
                        tv.setText("\u29bf");
                        tv.setId(tv.generateViewId());
                        ConstraintLayout constraintLayout = (ConstraintLayout)findViewById(R.id.photo);
                        constraintLayout.addView(tv);
                        tv.setX(x);
                        tv.setY(y-360);


                        if(i == 0){
                            pos[0] = constraintLayout.findViewById(tv.getId()).getX();
                            pos[1] = constraintLayout.findViewById(tv.getId()).getY();
                        } else {
                            pos[2] = constraintLayout.findViewById(tv.getId()).getX();
                            pos[3] = constraintLayout.findViewById(tv.getId()).getY();
                            Log.i("222", "x1= " + pos[0] + " y1= " + pos[1] + " x2= " + pos[2] + " y2= " + pos[3]);
                            if(pos[2] < pos[0] && pos[1] < pos[3]){
                                Toast.makeText(getBaseContext(), "Różnica X= " + String.valueOf((int)(pos[0] - pos[2])) + " , różnica Y= " + String.valueOf((int)(pos[3] - pos[1])), Toast.LENGTH_SHORT).show();
                            }
                            else if(pos[2] < pos[0] && pos[1] > pos[3]){
                                Toast.makeText(getBaseContext(), "Różnica X= " + String.valueOf((int)(pos[0] - pos[2])) + " , różnica Y= " + String.valueOf((int)(pos[1] - pos[3])), Toast.LENGTH_SHORT).show();
                            }
                            else if(pos[2] > pos[0] && pos[1] < pos[3]){
                                Toast.makeText(getBaseContext(), "Różnica X= " + String.valueOf((int)(pos[2] - pos[0])) + " , różnica Y= " + String.valueOf((int)(pos[3] - pos[1])), Toast.LENGTH_SHORT).show();
                            }
                            else if(pos[2] > pos[0] && pos[1] > pos[3]){
                                Toast.makeText(getBaseContext(), "Różnica X= " + String.valueOf((int)(pos[2] - pos[0])) + " , różnica Y= " + String.valueOf((int)(pos[1] - pos[3])), Toast.LENGTH_SHORT).show();
                            }
                        }

                        i++;
                    }

            }
            
        return false;
    }

我如何在 onTouchEvent 中创建 if 来检查我是否在photo.xml其他人中?

4

1 回答 1

0

确实应该为每个功能设置单独的活动(或片段),而不是替换当前视图,而是在获取图像时启动新的活动(或片段)。从长远来看,这将是最好的,所以我建议这样做。

也就是说 -现在要解决这个问题- 你可以做一个空检查onTouchEvent

ConstraintLayout constraintLayout = findViewById(R.id.photo);
if (constraintLayout == null) {
    return false;
}
于 2021-03-07T19:00:28.880 回答