-4

Here's MediaPlayer1.class

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MediaPlayer1 extends AppCompatActivity {

    MediaPlayer mpAudio;
    Boolean play;

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

        play = false;
        mpAudio = MediaPlayer.create(this, R.raw.youngagain);
        mpAudio.setLooping(true);

        Button bPlay = (Button) findViewById(R.id.playBtn);
        bPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (play == false)
                    mpAudio.start();
                else
                    mpAudio.pause();
                play = !play;
            }
        });
    }
}

and the layout file activity_media_player.xml

<FrameLayout 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:id="@+id/base_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_material_dark"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/zmountain"
        android:scaleType="centerCrop"
        android:foreground="#e1000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hardwell on Air"
            android:textColor="#fafafa"
            android:textSize="24sp"
            android:textStyle="bold"
            android:layout_marginTop="60dp"
            android:layout_marginBottom="10dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Hardwell on Air"
            android:textColor="#fafafa"
            android:layout_marginBottom="20dp"
            android:id="@+id/textView" />

        <sas.unplug.SquareImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/zghost" />

        <com.wnafee.vector.MorphButton
            android:id="@+id/playBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:background="@null"
            app:vc_endDrawable="@drawable/ic_pause_to_play"
            app:vc_foregroundTint="@color/colorAccent"
            app:vc_startDrawable="@drawable/ic_play_to_pause" />
    </LinearLayout>

</FrameLayout>

I am getting the following error on my activity above which I've called through my adapter, like this:

@Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final PodcastData item = items.get(position);

        holder.ptitle.setText(item.getPTitle());
        holder.plikes.setText(""+item.getPLikes());

        //Converting image into a bitmap
        Bitmap bitmap = item.getAlbum();
        //usig the generate method
        Palette palette = Palette.generate(bitmap, 16);
        Palette.generateAsync(bitmap, 16, new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                Palette.Swatch vibrant = palette.getVibrantSwatch();
                if (vibrant != null) {
                    // If we have a vibrant color
                    // update the title TextView
                    holder.ptitle.setTextColor(vibrant.getTitleTextColor());
                    holder.ptitle.setBackgroundColor(vibrant.getRgb());
                }
            }
        });
        holder.album.setImageBitmap(item.getAlbum());

        holder.bLike.setOnClickListener(new View.OnClickListener() {
            int likes = item.getPLikes();
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Likes selected " + items.get(position).getPLikes(), Toast.LENGTH_SHORT).show();
                if(item.liked) {
                    holder.bLike.setColorFilter(Color.argb(255, 255, 255, 255));
                    likes-=1;
                }
                else {
                    holder.bLike.setColorFilter(Color.argb(255, 255, 38, 38));
                    likes+=1;
                }
                item.liked = !item.liked;
                items.get(position).setPlikes(likes);
                item.setPlikes(likes);
                holder.plikes.setText("" + item.getPLikes());
                ParseQuery<ParseObject> query = ParseQuery.getQuery("Podcast");
                query.whereEqualTo("name", item.getPTitle());
                query.getInBackground(item.objectId, new GetCallback<ParseObject>() {
                    @Override
                    public void done(ParseObject object, ParseException e) {
                        if (e == null) {
                            object.put("likes", likes);
                            object.saveInBackground();
                        }
                    }
                });
            }
        });
        holder.album.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, MediaPlayer1.class);    //  Start MediaPlayer1 activity here

                context.startActivity(intent);
            }
        });
    }

activity is started in the last function - holder.album.setOnClickListener()

    FATAL EXCEPTION: main
Process: sas.unplug, PID: 1715

    java.lang.RuntimeException: Unable to start activity ComponentInfo{sas.unplug/sas.unplug.MediaPlayer1}: 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:2416)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
                                                                  at sas.unplug.MediaPlayer1.onCreate(MediaPlayer1.java:21)
                                                                  at android.app.Activity.performCreate(Activity.java:6237)
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                  at android.os.Looper.loop(Looper.java:148) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I am running the same exact code individually as a seperate project and the player works fine there but as soon as I start this activity in my main project my app crashes. Please help me I am in need of help desperately as I have my minor project submission tomorrow.

  • Your issue is right there in your LogCat. Perfect time to learn how to debug. You have an NPE at line 21 of your main activity class. – basic Dec 02 '15 at 16:27

1 Answers1

0
mpAudio =   MediaPlayer.create(this, R.raw.youngagain)

MediaPlayer.create() return a MediaPlayer object, or null if creation failed.

MediaPlayer.create() failed and mpAudio = null .

So when you mpAudio.setLoop, you get nullpointer error.

tiny sunlight
  • 6,231
  • 3
  • 21
  • 42