如何检查,我实际上在哪个 contentView 中?安卓工作室

问题描述

嗨!

我的 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);
                    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然后如果我点击按钮以外的某个地方,应用程序将崩溃,因为它在 photo.xml 中找到 ConstraintLayout 但应用程序仍在 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 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])) + ",Toast.LENGTH_SHORT).show();
                            }
                            else if(pos[2] > pos[0] && pos[1] > pos[3]){
                                Toast.makeText(getBaseContext(),Toast.LENGTH_SHORT).show();
                            }
                        }

                        i++;
                    }

            }
            
        return false;
    }

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

解决方法

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

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

ConstraintLayout constraintLayout = findViewById(R.id.photo);
if (constraintLayout == null) {
    return false;
}