-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,352
  • 21
  • 80
  • 110
asqa
  • 201
  • 3
  • 16

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,326
  • 8
  • 25
  • 43
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,187
  • 7
  • 26
  • 35
Nouman Ch
  • 3,896
  • 4
  • 30
  • 39
0
relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);

Try above code.

Vishal G. Gohel
  • 974
  • 1
  • 16
  • 29
0

Try this one:

relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);
Natig Babayev
  • 2,788
  • 14
  • 22