0

Hey everyone so I am having a problem with a videoView inside of a popUp window, everytime I try open the window it shuts down the app for some reason. I know where is the problem but I dont know the correct way to fix the issue. If anyone knows how to fix this it would be much appriacated.

This is my main activity

   public class Day1 extends AppCompatActivity {
      Dialog myDialog;

      @Override
        protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.part_sh_day1);

           myDialog = new Dialog(this);
}
   public void ShowFirstPopup(View v) {
        TextView mTextView;

        //================================-Text-View-====================================

        myDialog.setContentView(R.layout.video_popup);
        mTextView = myDialog.findViewById(R.id.txtclose);
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialog.dismiss();
            }
        });
        //================================-Video-View-====================================

        VideoView mVideoView;
        mVideoView = findViewById(R.id.videoPlayer);
        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.gymsharktrain_1);


        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myDialog.show();

        mVideoView.setVideoURI(uri);
   }
}

And this is the xml file op popUp window

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="350dp"
    android:layout_height="300dp"
    android:background="@color/colorGrey"
    android:padding="10dp"
    android:layout_gravity="center">
    <TextView
        android:id="@+id/txtclose"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:textColor="@color/colorLightGrey"
        android:text="X"
        android:textStyle="bold"
        android:textSize="19sp"
        android:layout_gravity="end"
        android:gravity="center"
        android:background="@drawable/circlebackground"
        android:layout_marginBottom="7dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <VideoView
            android:id="@+id/videoPlayer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
</LinearLayout>

Error messege:

2020-01-15 21:57:59.138 3179-3224/com.example..... D/IMonitor: Load library imonitor_jni
2020-01-15 21:57:59.140 3179-3179/com.example..... V/ActivityThread: Skipping new config:{1.0 231mcc2mnc [en_GB] ldltr sw360dp w360dp h679dp 480dpi nrml long hdr port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1080, 2037) mAppBounds=Rect(0, 81 - 1080, 2118) mWindowingMode=fullscreen mActivityType=undefined} nonFullScreen=0 suim:1 s.22}, config:{1.0 231mcc2mnc [en_GB] ldltr sw360dp w360dp h679dp 480dpi nrml long hdr port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1080, 2037) mAppBounds=Rect(0, 81 - 1080, 2118) mWindowingMode=fullscreen mActivityType=undefined} nonFullScreen=0 suim:1 s.22} for app:com.example.......
2020-01-15 21:57:59.142 3179-3207/com.example..... E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist!
2020-01-15 21:57:59.148 3179-3224/com.example.....W/ZrHung.AppEyeUiProbe: Failed to get config from zrhung
2020-01-15 21:57:59.164 3179-3222/com.example..... E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@1093b93
2020-01-15 21:57:59.191 3179-3179/com.example..... V/HwPolicyFactory: : success to get AllImpl object and return....
2020-01-15 21:57:59.195 3179-3179/com.example..... V/ActivityThread: callActivityOnCreate

Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)

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.example......ShowFirstPopup

Kristian
  • 165
  • 2
  • 16
  • Please grab the stacktrace from logcat and post it here. – greeble31 Jan 15 '20 at 20:39
  • @greeble31 Updated. (Posted) – Kristian Jan 15 '20 at 21:04
  • @greeble31 so if I understand it correctly, I am deferencing mVideoView after creating an activity where the object uri is not declared and that causes a NullPointerException. So I should either create an if statement where if uri == null then do something or just simply move the declaration under myDialog.show();. Is that correct? – Kristian Jan 15 '20 at 21:29
  • No - it is `mVideoView` that is null. You are trying to call `setVideoURI()` "on a null object reference". – greeble31 Jan 15 '20 at 21:47
  • Put a breakpoint on that line and run the app in debug mode, it will really help you out. – greeble31 Jan 15 '20 at 21:47
  • 1
    Incidentally you might be able to fix this by calling `myDialog.findViewById()` instead of calling it on your `Activity` (`Activity.findViewById()` will not search dialogs; it only searches the `Activity` content). – greeble31 Jan 15 '20 at 21:50
  • @greeble31 would you please be that kind and write an example of what should I write because I dont know why its so confusing for me but I cant see the solution for this problem. – Kristian Jan 15 '20 at 22:13
  • 1
    Take the line `mVideoView = findViewById(R.id.videoPlayer);` and replace it with `mVideoView = mDialog.findViewById(R.id.videoPlayer);`. – greeble31 Jan 15 '20 at 22:23
  • @greeble31 Of course that makes sence! Wonderful, thank you. – Kristian Jan 16 '20 at 10:57

0 Answers0