1

I am building an application. When I click a button it should popup a window with a videoview and a video plays in it. so i used a popup.xml layout file with a linear layout, video video in it. In MyActivity I have created the following mentioned popup function. When I called showVideoPopup() i'm getting the following errors.

The following is showVideoPopup() popup function

 private void showVideoPopup()
    {
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customView = layoutInflater.inflate(R.layout.dispensing_intro,null);


        //instantiate popup window
        dispense_intro_popup = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        //display the popup window
        dispense_intro_popup.showAtLocation(dispense_intro_view, Gravity.CENTER, 0, 0);

        VideoView video = (VideoView) findViewById(R.id.dispense_intro);
        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/raw/"+ R.raw.dispense_intro);
        video.setVideoURI(videoUri);
        video.start();

        video.setOnCompletionListener(this);

    }

The popup layout XML is the following

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dispense_intro_holder">


    <VideoView
        android:id="@+id/dispense_intro"
        android:layout_width="640dp"
        android:layout_height="360dp"  />
</LinearLayout>

The following is the detailed error

 java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
        at android.view.View.performClick(View.java:7140)
        at android.view.View.performClickInternal(View.java:7117)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27351)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
        at android.view.View.performClick(View.java:7140) 
        at android.view.View.performClickInternal(View.java:7117) 
        at android.view.View.access$3500(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27351) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoURI(android.net.Uri)' on a null object reference
        at com.myapp.adapp.MyActivity.showSortPopup(MyActivity.java:252)
        at com.myapp.adapp.MyActivity.dispense(MyActivity.java:174)

Please don't mark it as duplicate. Could you please explain the issue and solve it in this scenario

Izhan Ali
  • 567
  • 7
  • 17
Himavan
  • 385
  • 3
  • 16
  • Use this `VideoView video = (VideoView) customView.findViewById(R.id.dispense_intro);` – AskNilesh Sep 24 '19 at 10:30
  • Please don't mark as duplicate. Could you post the links here of original question @NileshRathod – Himavan Sep 24 '19 at 10:30
  • To solve `Caused by: java.lang.NullPointerException:` this issue please do `findViewById()` as i suggested in above comment – AskNilesh Sep 24 '19 at 10:34
  • 1
    Thanks It solved the issue. But if you reply in the answer you would have get reputation. Any how thanks very much. – Himavan Sep 24 '19 at 10:36

0 Answers0