Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object...












0















I have a problem that I'm at whit's end to solve:



I'm working on a program in Android Studio that essentially cycles through a list of storyboards (Like a slideshow) and I'm using a viewPager component to do so. It's sorta cool because I don't need buttons. I can just swipe (Though I'm working on setting up some)However, I'm also trying to link the "slides" in the show with sound (In this case, the storyboard audio). The issue is that the app is crashing and it's giving me some null error in logcat. This is what I've did so far . . .



I've create a slideAdapter Activity:



package com.example.******.*************;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class SlideAdapter extends PagerAdapter {

Context context;
LayoutInflater inflater;

// List of Images

public int lst_storyboard = {
R.drawable.imgmob02, R.drawable.imgmob03, R.drawable.imgmob04,
R.drawable.imgmob05, R.drawable.imgmob06, R.drawable.imgmob07,
R.drawable.imgmob08, R.drawable.imgmob09, R.drawable.imgmob010,
R.drawable.imgmob011,R.drawable.imgmob012,R.drawable.imgmob013
};

// List of mp3 raw audio

public int lst_audio_eng = {
R.raw.audio_eng_01, R.raw.audio_eng_02, R.raw.audio_eng_03,
R.raw.audio_eng_04, R.raw.audio_eng_05, R.raw.audio_eng_06,
R.raw.audio_eng_07, R.raw.audio_eng_08, R.raw.audio_eng_09,
R.raw.audio_eng_010, R.raw.audio_eng_011, R.raw.audio_eng_012
};

public SlideAdapter(Context context) {
this.context = context;
}

@Override
public int getCount() {
return lst_storyboard.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_slide_adapter,container,false);
LinearLayout layoutslide = (LinearLayout) view.findViewById(R.id.slidelinearlayout);
ImageView imgslide = (ImageView) view.findViewById(R.id.slideimg);

imgslide.setImageResource(lst_storyboard[position]);


container.addView(view);
return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}

}


And this is the slideactivity.xml:



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

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/slideimg"
android:contentDescription="@string/todo" />
</LinearLayout>


This is the mainActivity file:



    package com.example.******.***********;

import android.media.MediaPlayer;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import static com.example.gman5541.childrensbookconcept2.SlideAdapter.lst_audio_eng;

public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private SlideAdapter myadapter;

private MediaPlayer mp;
private MediaPlayer BG;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myadapter = new SlideAdapter(this);
viewPager = (ViewPager) findViewById(R.id.viewpager);

viewPager.addOnPageChangeListener(player);

viewPager.setAdapter(myadapter);
viewPager.setCurrentItem(0);

BG = MediaPlayer.create(getApplicationContext(), R.raw.audio_eng_01);
BG.setLooping(true);
BG.setVolume(100, 100);
BG.start();
mp = MediaPlayer.create(MainActivity.this,lst_audio_eng[0]);
mp.start();
}




@Override
protected void onPause() {
super.onPause();
BG.pause();
}

@Override
protected void onResume() {
super.onResume();
BG.start();
}

public ViewPager.OnPageChangeListener player = new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int arg1)
{
mp = MediaPlayer.create(MainActivity.this, lst_audio_eng[arg1]);
mp.start();
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}

@Override
public void onPageScrollStateChanged(int arg0)
{
}
};

}


And it's xml file:



   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gman5541.childrensbookconcept2.MainActivity">

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

</LinearLayout>


This is what I've got from the logcat debug:



--------- beginning of crash
2018-11-25 18:40:07.511 15354-15354/com.example.gman5541.childrensbookconcept2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gman5541.childrensbookconcept2, PID: 15354
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gman5541.childrensbookconcept2/com.example.gman5541.childrensbookconcept2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
at com.example.gman5541.childrensbookconcept2.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 


Though I'm also working on having a "play" button with a "sound" switch to switch sound on and off, Right now, I'm just trying to match one list (storyboards) with another list(audio). The Program works, if I comment out the MediaPlayer lines. Please help.










share|improve this question





























    0















    I have a problem that I'm at whit's end to solve:



    I'm working on a program in Android Studio that essentially cycles through a list of storyboards (Like a slideshow) and I'm using a viewPager component to do so. It's sorta cool because I don't need buttons. I can just swipe (Though I'm working on setting up some)However, I'm also trying to link the "slides" in the show with sound (In this case, the storyboard audio). The issue is that the app is crashing and it's giving me some null error in logcat. This is what I've did so far . . .



    I've create a slideAdapter Activity:



    package com.example.******.*************;
    import android.content.Context;
    import android.support.v4.view.PagerAdapter;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.LinearLayout;

    public class SlideAdapter extends PagerAdapter {

    Context context;
    LayoutInflater inflater;

    // List of Images

    public int lst_storyboard = {
    R.drawable.imgmob02, R.drawable.imgmob03, R.drawable.imgmob04,
    R.drawable.imgmob05, R.drawable.imgmob06, R.drawable.imgmob07,
    R.drawable.imgmob08, R.drawable.imgmob09, R.drawable.imgmob010,
    R.drawable.imgmob011,R.drawable.imgmob012,R.drawable.imgmob013
    };

    // List of mp3 raw audio

    public int lst_audio_eng = {
    R.raw.audio_eng_01, R.raw.audio_eng_02, R.raw.audio_eng_03,
    R.raw.audio_eng_04, R.raw.audio_eng_05, R.raw.audio_eng_06,
    R.raw.audio_eng_07, R.raw.audio_eng_08, R.raw.audio_eng_09,
    R.raw.audio_eng_010, R.raw.audio_eng_011, R.raw.audio_eng_012
    };

    public SlideAdapter(Context context) {
    this.context = context;
    }

    @Override
    public int getCount() {
    return lst_storyboard.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
    return (view==(LinearLayout)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.activity_slide_adapter,container,false);
    LinearLayout layoutslide = (LinearLayout) view.findViewById(R.id.slidelinearlayout);
    ImageView imgslide = (ImageView) view.findViewById(R.id.slideimg);

    imgslide.setImageResource(lst_storyboard[position]);


    container.addView(view);
    return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((LinearLayout)object);
    }

    }


    And this is the slideactivity.xml:



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

    <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/slideimg"
    android:contentDescription="@string/todo" />
    </LinearLayout>


    This is the mainActivity file:



        package com.example.******.***********;

    import android.media.MediaPlayer;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    import static com.example.gman5541.childrensbookconcept2.SlideAdapter.lst_audio_eng;

    public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private SlideAdapter myadapter;

    private MediaPlayer mp;
    private MediaPlayer BG;


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myadapter = new SlideAdapter(this);
    viewPager = (ViewPager) findViewById(R.id.viewpager);

    viewPager.addOnPageChangeListener(player);

    viewPager.setAdapter(myadapter);
    viewPager.setCurrentItem(0);

    BG = MediaPlayer.create(getApplicationContext(), R.raw.audio_eng_01);
    BG.setLooping(true);
    BG.setVolume(100, 100);
    BG.start();
    mp = MediaPlayer.create(MainActivity.this,lst_audio_eng[0]);
    mp.start();
    }




    @Override
    protected void onPause() {
    super.onPause();
    BG.pause();
    }

    @Override
    protected void onResume() {
    super.onResume();
    BG.start();
    }

    public ViewPager.OnPageChangeListener player = new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageSelected(int arg1)
    {
    mp = MediaPlayer.create(MainActivity.this, lst_audio_eng[arg1]);
    mp.start();
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2)
    {
    }

    @Override
    public void onPageScrollStateChanged(int arg0)
    {
    }
    };

    }


    And it's xml file:



       <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.gman5541.childrensbookconcept2.MainActivity">

    <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

    </LinearLayout>


    This is what I've got from the logcat debug:



    --------- beginning of crash
    2018-11-25 18:40:07.511 15354-15354/com.example.gman5541.childrensbookconcept2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.gman5541.childrensbookconcept2, PID: 15354
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gman5541.childrensbookconcept2/com.example.gman5541.childrensbookconcept2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
    at com.example.gman5541.childrensbookconcept2.MainActivity.onCreate(MainActivity.java:31)
    at android.app.Activity.performCreate(Activity.java:7009)
    at android.app.Activity.performCreate(Activity.java:7000)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 


    Though I'm also working on having a "play" button with a "sound" switch to switch sound on and off, Right now, I'm just trying to match one list (storyboards) with another list(audio). The Program works, if I comment out the MediaPlayer lines. Please help.










    share|improve this question



























      0












      0








      0








      I have a problem that I'm at whit's end to solve:



      I'm working on a program in Android Studio that essentially cycles through a list of storyboards (Like a slideshow) and I'm using a viewPager component to do so. It's sorta cool because I don't need buttons. I can just swipe (Though I'm working on setting up some)However, I'm also trying to link the "slides" in the show with sound (In this case, the storyboard audio). The issue is that the app is crashing and it's giving me some null error in logcat. This is what I've did so far . . .



      I've create a slideAdapter Activity:



      package com.example.******.*************;
      import android.content.Context;
      import android.support.v4.view.PagerAdapter;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.ImageView;
      import android.widget.LinearLayout;

      public class SlideAdapter extends PagerAdapter {

      Context context;
      LayoutInflater inflater;

      // List of Images

      public int lst_storyboard = {
      R.drawable.imgmob02, R.drawable.imgmob03, R.drawable.imgmob04,
      R.drawable.imgmob05, R.drawable.imgmob06, R.drawable.imgmob07,
      R.drawable.imgmob08, R.drawable.imgmob09, R.drawable.imgmob010,
      R.drawable.imgmob011,R.drawable.imgmob012,R.drawable.imgmob013
      };

      // List of mp3 raw audio

      public int lst_audio_eng = {
      R.raw.audio_eng_01, R.raw.audio_eng_02, R.raw.audio_eng_03,
      R.raw.audio_eng_04, R.raw.audio_eng_05, R.raw.audio_eng_06,
      R.raw.audio_eng_07, R.raw.audio_eng_08, R.raw.audio_eng_09,
      R.raw.audio_eng_010, R.raw.audio_eng_011, R.raw.audio_eng_012
      };

      public SlideAdapter(Context context) {
      this.context = context;
      }

      @Override
      public int getCount() {
      return lst_storyboard.length;
      }

      @Override
      public boolean isViewFromObject(View view, Object object) {
      return (view==(LinearLayout)object);
      }

      @Override
      public Object instantiateItem(ViewGroup container, int position) {
      inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
      View view = inflater.inflate(R.layout.activity_slide_adapter,container,false);
      LinearLayout layoutslide = (LinearLayout) view.findViewById(R.id.slidelinearlayout);
      ImageView imgslide = (ImageView) view.findViewById(R.id.slideimg);

      imgslide.setImageResource(lst_storyboard[position]);


      container.addView(view);
      return view;
      }

      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
      container.removeView((LinearLayout)object);
      }

      }


      And this is the slideactivity.xml:



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

      <ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/slideimg"
      android:contentDescription="@string/todo" />
      </LinearLayout>


      This is the mainActivity file:



          package com.example.******.***********;

      import android.media.MediaPlayer;
      import android.support.v4.view.ViewPager;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;

      import static com.example.gman5541.childrensbookconcept2.SlideAdapter.lst_audio_eng;

      public class MainActivity extends AppCompatActivity {
      private ViewPager viewPager;
      private SlideAdapter myadapter;

      private MediaPlayer mp;
      private MediaPlayer BG;


      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      myadapter = new SlideAdapter(this);
      viewPager = (ViewPager) findViewById(R.id.viewpager);

      viewPager.addOnPageChangeListener(player);

      viewPager.setAdapter(myadapter);
      viewPager.setCurrentItem(0);

      BG = MediaPlayer.create(getApplicationContext(), R.raw.audio_eng_01);
      BG.setLooping(true);
      BG.setVolume(100, 100);
      BG.start();
      mp = MediaPlayer.create(MainActivity.this,lst_audio_eng[0]);
      mp.start();
      }




      @Override
      protected void onPause() {
      super.onPause();
      BG.pause();
      }

      @Override
      protected void onResume() {
      super.onResume();
      BG.start();
      }

      public ViewPager.OnPageChangeListener player = new ViewPager.OnPageChangeListener() {

      @Override
      public void onPageSelected(int arg1)
      {
      mp = MediaPlayer.create(MainActivity.this, lst_audio_eng[arg1]);
      mp.start();
      }

      @Override
      public void onPageScrolled(int arg0, float arg1, int arg2)
      {
      }

      @Override
      public void onPageScrollStateChanged(int arg0)
      {
      }
      };

      }


      And it's xml file:



         <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.gman5541.childrensbookconcept2.MainActivity">

      <android.support.v4.view.ViewPager
      android:id="@+id/viewpager"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      </android.support.v4.view.ViewPager>

      </LinearLayout>


      This is what I've got from the logcat debug:



      --------- beginning of crash
      2018-11-25 18:40:07.511 15354-15354/com.example.gman5541.childrensbookconcept2 E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.gman5541.childrensbookconcept2, PID: 15354
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gman5541.childrensbookconcept2/com.example.gman5541.childrensbookconcept2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
      at android.os.Handler.dispatchMessage(Handler.java:106)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6494)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
      at com.example.gman5541.childrensbookconcept2.MainActivity.onCreate(MainActivity.java:31)
      at android.app.Activity.performCreate(Activity.java:7009)
      at android.app.Activity.performCreate(Activity.java:7000)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
      at android.os.Handler.dispatchMessage(Handler.java:106) 
      at android.os.Looper.loop(Looper.java:164) 
      at android.app.ActivityThread.main(ActivityThread.java:6494) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 


      Though I'm also working on having a "play" button with a "sound" switch to switch sound on and off, Right now, I'm just trying to match one list (storyboards) with another list(audio). The Program works, if I comment out the MediaPlayer lines. Please help.










      share|improve this question
















      I have a problem that I'm at whit's end to solve:



      I'm working on a program in Android Studio that essentially cycles through a list of storyboards (Like a slideshow) and I'm using a viewPager component to do so. It's sorta cool because I don't need buttons. I can just swipe (Though I'm working on setting up some)However, I'm also trying to link the "slides" in the show with sound (In this case, the storyboard audio). The issue is that the app is crashing and it's giving me some null error in logcat. This is what I've did so far . . .



      I've create a slideAdapter Activity:



      package com.example.******.*************;
      import android.content.Context;
      import android.support.v4.view.PagerAdapter;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.ImageView;
      import android.widget.LinearLayout;

      public class SlideAdapter extends PagerAdapter {

      Context context;
      LayoutInflater inflater;

      // List of Images

      public int lst_storyboard = {
      R.drawable.imgmob02, R.drawable.imgmob03, R.drawable.imgmob04,
      R.drawable.imgmob05, R.drawable.imgmob06, R.drawable.imgmob07,
      R.drawable.imgmob08, R.drawable.imgmob09, R.drawable.imgmob010,
      R.drawable.imgmob011,R.drawable.imgmob012,R.drawable.imgmob013
      };

      // List of mp3 raw audio

      public int lst_audio_eng = {
      R.raw.audio_eng_01, R.raw.audio_eng_02, R.raw.audio_eng_03,
      R.raw.audio_eng_04, R.raw.audio_eng_05, R.raw.audio_eng_06,
      R.raw.audio_eng_07, R.raw.audio_eng_08, R.raw.audio_eng_09,
      R.raw.audio_eng_010, R.raw.audio_eng_011, R.raw.audio_eng_012
      };

      public SlideAdapter(Context context) {
      this.context = context;
      }

      @Override
      public int getCount() {
      return lst_storyboard.length;
      }

      @Override
      public boolean isViewFromObject(View view, Object object) {
      return (view==(LinearLayout)object);
      }

      @Override
      public Object instantiateItem(ViewGroup container, int position) {
      inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
      View view = inflater.inflate(R.layout.activity_slide_adapter,container,false);
      LinearLayout layoutslide = (LinearLayout) view.findViewById(R.id.slidelinearlayout);
      ImageView imgslide = (ImageView) view.findViewById(R.id.slideimg);

      imgslide.setImageResource(lst_storyboard[position]);


      container.addView(view);
      return view;
      }

      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
      container.removeView((LinearLayout)object);
      }

      }


      And this is the slideactivity.xml:



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

      <ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/slideimg"
      android:contentDescription="@string/todo" />
      </LinearLayout>


      This is the mainActivity file:



          package com.example.******.***********;

      import android.media.MediaPlayer;
      import android.support.v4.view.ViewPager;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;

      import static com.example.gman5541.childrensbookconcept2.SlideAdapter.lst_audio_eng;

      public class MainActivity extends AppCompatActivity {
      private ViewPager viewPager;
      private SlideAdapter myadapter;

      private MediaPlayer mp;
      private MediaPlayer BG;


      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      myadapter = new SlideAdapter(this);
      viewPager = (ViewPager) findViewById(R.id.viewpager);

      viewPager.addOnPageChangeListener(player);

      viewPager.setAdapter(myadapter);
      viewPager.setCurrentItem(0);

      BG = MediaPlayer.create(getApplicationContext(), R.raw.audio_eng_01);
      BG.setLooping(true);
      BG.setVolume(100, 100);
      BG.start();
      mp = MediaPlayer.create(MainActivity.this,lst_audio_eng[0]);
      mp.start();
      }




      @Override
      protected void onPause() {
      super.onPause();
      BG.pause();
      }

      @Override
      protected void onResume() {
      super.onResume();
      BG.start();
      }

      public ViewPager.OnPageChangeListener player = new ViewPager.OnPageChangeListener() {

      @Override
      public void onPageSelected(int arg1)
      {
      mp = MediaPlayer.create(MainActivity.this, lst_audio_eng[arg1]);
      mp.start();
      }

      @Override
      public void onPageScrolled(int arg0, float arg1, int arg2)
      {
      }

      @Override
      public void onPageScrollStateChanged(int arg0)
      {
      }
      };

      }


      And it's xml file:



         <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.gman5541.childrensbookconcept2.MainActivity">

      <android.support.v4.view.ViewPager
      android:id="@+id/viewpager"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      </android.support.v4.view.ViewPager>

      </LinearLayout>


      This is what I've got from the logcat debug:



      --------- beginning of crash
      2018-11-25 18:40:07.511 15354-15354/com.example.gman5541.childrensbookconcept2 E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.gman5541.childrensbookconcept2, PID: 15354
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gman5541.childrensbookconcept2/com.example.gman5541.childrensbookconcept2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
      at android.os.Handler.dispatchMessage(Handler.java:106)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6494)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
      at com.example.gman5541.childrensbookconcept2.MainActivity.onCreate(MainActivity.java:31)
      at android.app.Activity.performCreate(Activity.java:7009)
      at android.app.Activity.performCreate(Activity.java:7000)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
      at android.os.Handler.dispatchMessage(Handler.java:106) 
      at android.os.Looper.loop(Looper.java:164) 
      at android.app.ActivityThread.main(ActivityThread.java:6494) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 


      Though I'm also working on having a "play" button with a "sound" switch to switch sound on and off, Right now, I'm just trying to match one list (storyboards) with another list(audio). The Program works, if I comment out the MediaPlayer lines. Please help.







      image audio android-viewpager android-mediaplayer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 2:50







      user71311

















      asked Nov 24 '18 at 21:50









      user71311user71311

      14




      14
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462668%2fattempt-to-invoke-virtual-method-void-android-media-mediaplayer-setloopingbool%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462668%2fattempt-to-invoke-virtual-method-void-android-media-mediaplayer-setloopingbool%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen