-1

i have custom alert dialog with two button inside. but each button have error null object reference, i want to make my relativelayout clickable and here is my java code :

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

here is my upload_video_options.xml for customize the alert dialog:

<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">
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
asqa
  • 199
  • 3
  • 17

4 Answers4

2

Try below Code , You are getting view from upload_video_options.xml so you need do like this.

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

Full Code :

 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();
    }
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
1

Try this one.

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

The problem is you are searching for views in main layout instead of alerviews's layout.

Hope this will help you.

Ram Koti
  • 2,203
  • 7
  • 26
  • 36
Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
0
relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);

Try above code.

Vishal G. Gohel
  • 1,008
  • 1
  • 16
  • 31
0

Try this one:

relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);
Natig Babayev
  • 3,128
  • 15
  • 23