Исключение ошибки NullPointerException - PullRequest
0 голосов
/ 05 января 2012

Эта активность вызывается из другой активности с именем песни.Я не знаю, что делать, когда я использую Uri, он возвращает NullPointerException, но со статическим типом Uri ((Uri myUri = Uri.parse (R.raw.a20);)) проблем нет.Есть идеи?

package org.shia.dua;

import java.io.IOException;


import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;


public class ShiaduaActivity extends Activity implements Runnable  {
/** Called when the activity is first created. */

private ImageView pausePlay;
private SeekBar mSeekBar;
private MediaPlayer mPlayer = new MediaPlayer();
private int total;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Bundle bundle = this.getIntent().getExtras();
String duaNumber = bundle.getString("duaNumber");
    duaNumber = "R.raw.a"+duaNumber;
    Uri myUri = Uri.parse(duaNumber);
    mPlayer = MediaPlayer.create(this,myUri); // it returns null pointer
    pausePlay = (ImageView) findViewById(R.id.pausePlay);
    mSeekBar = (SeekBar) findViewById(R.id.seekBar1);

    mSeekBar.setProgress(0);
    try
    {
        total = mPlayer.getDuration();
    }
    catch(Exception e)
    {

Log.e("error",e.toString()); I got error here NullPointerException
    }
    mPlayer.prepare();
    mPlayer.start();
mSeekBar.setMax(total);



    pausePlay.setOnClickListener(new Button.OnClickListener()
    {

        @Override
        public void onClick(View arg0)
        {
            if(mPlayer.isPlaying())
            {
                mPlayer.pause();
                pausePlay.setBackgroundColor(R.drawable.play);
            }
            else
            {
                mPlayer.start();
                mSeekBar.setMax(total);
                pausePlay.setBackgroundColor(R.drawable.pause);
            }
        }
    }); 

    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            // TODO Auto-generated method stub
            if(fromUser){
                mPlayer.seekTo(progress);
                mSeekBar.setProgress(progress);
            }
        }
    });

    Thread currentThread  = new Thread(this);
    currentThread.start();

}
public void play(String duaNumber)
{
    try
    {
        AssetFileDescriptor descriptor;
        descriptor = getAssets().openFd(duaNumber);
        mPlayer.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() );
        descriptor.close();
        mPlayer.prepare();
        mPlayer.start();
        mSeekBar.setMax(total);

    }
    catch(IOException io)
    {
        Log.e("error","error");
    }

}

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        while(mPlayer != null){
            int currentPosition = mPlayer.getCurrentPosition();
            Message msg = new Message();
            msg.what = currentPosition;
            threadHandler.sendMessage(msg);
        }
        Thread.sleep(100);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
private Handler threadHandler = new Handler(){
    public void handleMessage(Message msg){
        //super.handleMessage(msg);
        //txt.setText(Integer.toString(msg.what));
        mSeekBar.setProgress(msg.what);
    }
};
}
...