-2

I'm having an issue with MediaPlayer code in fragments. Logcat is suggesting it's down to the OnClickListener in this passage of code within the SoundsFragment.java class.

Can anyone tell me what I'm doing wrong?

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.fragment_sounds);
 setVolumeControlStream(AudioManager.STREAM_MUSIC);
 Button button1=(Button)findViewById(R.id.button_1);
 Button button2=(Button)findViewById(R.id.button_2);
 button1.setOnClickListener(this);
 button2.setOnClickListener(this);
}

SoundsFragment.java file which hosts the mediaplayer code that was recently inserted and has causes these issues.

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.Button;


public class SoundsFragment extends Fragment implements OnClickListener{

    public SoundsFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_sounds, container, false);

        return rootView;
    }

    private MediaPlayer mp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
 setContentView(R.layout.fragment_sounds);
     setVolumeControlStream(AudioManager.STREAM_MUSIC);
     Button button1=(Button)findViewById(R.id.button_1);
     Button button2=(Button)findViewById(R.id.button_2);
     button1.setOnClickListener(this);
     button2.setOnClickListener(this);
    }

    private Button findViewById(int button1) {
        // TODO Auto-generated method stub
        return null;
    }

    private void setVolumeControlStream(int streamMusic) {
        // TODO Auto-generated method stub

    }

    private void setContentView(int activityMain) {
        // TODO Auto-generated method stub

    }

    public void onClick(View v) {
     int resId;
     switch (v.getId()) {
     case R.id.button_1:
       resId = R.raw.a;
       break;
     case R.id.button_2:
        resId = R.raw.b;
       break;
     default:
       resId = R.raw.a;
       break;
     }
     // Release any resources from previous MediaPlayer
     if (mp != null) {
        mp.release();
     }
     // Create a new MediaPlayer to play this sound
     mp = MediaPlayer.create(getActivity(), resId);
     mp.start();
    }


    @Override
    public void onDestroy() {
      if(null!=mp){
     mp.release();
      }
      super.onDestroy();
    }
}

fragment_sound.xml This is the xml file the manages the layout of the sounds fragment and is hosting the buttons.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/sounds_fragment">

        <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="22dp"
        android:layout_marginTop="36dp"
        android:text="Play audio a.mp3" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button_1"
        android:layout_below="@+id/button_1"
        android:text="Play audio b.mp3" />

</RelativeLayout>
rossd
  • 229
  • 1
  • 3
  • 14
  • Something is `null` at line 59 of `MainActivity`. – codeMagic Feb 16 '14 at 03:22
  • I'm very confused by this edit. Have you *completely changed* the question? Don't do that. If you have a new question to ask about new code, then ask a new question. – Charles Feb 17 '14 at 17:52

2 Answers2

0

Here's how you read a crashlog like that. First, start from Fatal exception. Then find the top most line that names a function in your app. In this case,
me.soundasleep.app.MainActivity.onCreate(MainActivity.java:59).

Then scroll back up to find the nearest exception type. In this case, NullPointerException. This means that in onCreate() in MainActivity.java on line 59, you accessed a null pointer. I'm not going to bother to find line 59 for you, but somewhere on that line you access one or more pointers. One of them is null. Figure out which one it is. Then figure out where you forgot to initialize it, or if you did initialize it why that initialization failed. Usually in this case in onCreate() its that you forgot to new an object, accessed an object before you created it, or used the wrong id in a findViewById().

codeMagic
  • 44,549
  • 13
  • 77
  • 93
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I'm new to java that I cant work out how to initialise it. Most of my code is from tutorials from the internet. The problem comes from this: button1.setOnClickListener(this); button2.setOnClickListener(this); – rossd Feb 17 '14 at 15:18
  • THe buttons you're looking for are in a fragment, but your main activity is looking for them in the top level layout. It isn't finding them, so you're getting null for both button 1 and button 2. – Gabe Sechan Feb 17 '14 at 15:24
  • Thought that was the case, moved it into SoundsFragment.java and it's stll crashing on the same point. Updated the question to show more up to date code within the files. Any help would be appreciated. – rossd Feb 17 '14 at 17:22
0

The checked exception :NullPointerException is thrown when you are trying to call an object through an reference variable where it doesn't has a pointer to the object you're trying to call.

In other words: Initialize the variable, before using it.

Luis Pena
  • 4,132
  • 2
  • 15
  • 23