按钮空对象引用

button null object reference

我有自定义警报对话框,里面有两个按钮。但是每个按钮都有错误的空对象引用,我想让我的 relativelayout 可以点击,这是我的 java 代码:

RelativeLayout relaCamera;
RelativeLayout relaGallery;

relaCamera = findViewById(R.id.relaCameraIntent);
relaGallery = findViewById(R.id.relaGalleryIntent);
......
......
private void pickFromGallery() {
    LayoutInflater layoutInflater = LayoutInflater.from(this);
    View view = layoutInflater.inflate(R.layout.upload_video_options, null);

    final AlertDialog alertD = new AlertDialog.Builder(this).create();

    relaCamera.setOnClickListener(new View.OnClickListener() { // error here
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.setTypeAndNormalize("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_GET_VIDEO);

        }
    });

    relaGallery.setOnClickListener(new View.OnClickListener() {// error here
        public void onClick(View v) {

            Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
            }

        }
    });

    alertD.setView(view);

    alertD.show();
}

这是我的 upload_video_options.xml 自定义警报对话框:

<RelativeLayout
        android:id="@+id/relaCameraIntent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:clickable="true"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:id="@+id/relaGalleryIntent"
        android:clickable="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

试试这个。

RelativeLayout relaCamera;
RelativeLayout relaGallery;  

private void pickFromGallery() {
    LayoutInflater layoutInflater = LayoutInflater.from(this);
    View view = layoutInflater.inflate(R.layout.upload_video_options, null);

    relaCamera = view.findViewById(R.id.relaCameraIntent);
    relaGallery = view.findViewById(R.id.relaGalleryIntent);
    final AlertDialog alertD = new AlertDialog.Builder(this).create();

    relaCamera.setOnClickListener(new View.OnClickListener() { // error here
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.setTypeAndNormalize("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_GET_VIDEO);

        }
    });

    relaGallery.setOnClickListener(new View.OnClickListener() {// error here
    public void onClick(View v) {

        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
        }
    });

    alertD.setView(view);

    alertD.show();
}

问题是您在主布局而不是 alerviews 的布局中搜索视图。

希望对您有所帮助。

relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);

试试上面的代码。

试试这个:

relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);

试试下面的代码,你从 upload_video_options.xml 得到 view 所以你需要这样做。

    LayoutInflater layoutInflater = LayoutInflater.from(this);
                   View view = layoutInflater.inflate(R.layout.upload_video_options,null);

                       relaCamera = view.findViewById(R.id.relaCameraIntent);
                       relaGallery = view.findViewById(R.id.relaGalleryIntent);

完整代码:

 private void pickFromGallery() {
    LayoutInflater layoutInflater = LayoutInflater.from(this);
    View view = layoutInflater.inflate(R.layout.upload_video_options, null);

 relaCamera = view.findViewById(R.id.relaCameraIntent);
           relaGallery = view.findViewById(R.id.relaGalleryIntent);

    final AlertDialog alertD = new AlertDialog.Builder(this).create();


    relaCamera.setOnClickListener(new View.OnClickListener() { // error here
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.setTypeAndNormalize("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_GET_VIDEO);

        }
    });

    relaGallery.setOnClickListener(new View.OnClickListener() {// error here
        public void onClick(View v) {

            Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
            }

        }
    });

    alertD.setView(view);

    alertD.show();
    }