2017-02-16 6 views
-1

私は、nullポインタ例外iはのAndroidメディアプレーヤーNULLポインタ例外

MediaPlayer mp = new MediaPlayer(); 

public int check() 
    { 
if(mp!=null) 
    { 
     return 1; 
    } 
    else 
    { 
     return 2; 
    } 

    } 

を助けていない使用して試してみましたが何であるかを知っています。活性 ComponentInfo {com.openaisearch.www.musicplayer/com.openaisearch.www.musicplayer.MainActivity}を開始できません: れるjava.langこれはエラーイムgetting-

java.lang.RuntimeExceptionあります.NullPointerException:ヌル オブジェクト参照に仮想メソッドを呼び出す試み 'ブールcom.openaisearch.www.musicplayer.Music.check()'

public class MainActivity extends AppCompatActivity { 
    ArrayList<String> mSongsList = new ArrayList<>() ; 
    ArrayList<Long> SongsPath = new ArrayList<>() ; 
    Button btnPlay; 
    Music mService; 
SeekBar seekBar; 
ListView lv; 
boolean mBound = false; 
private ServiceConnection mConnection = new ServiceConnection() { 

    @Override 
    public void onServiceConnected(ComponentName className, 
            IBinder service) { 
     Music.LocalBinder binder = (Music.LocalBinder) service; 
     mService = binder.getService(); 
     mBound = true; 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName arg0) { 
     mBound = false; 
    } 
}; 

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

    Intent intent = new Intent(this, Music.class); 
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
    startService(intent); 

    btnPlay = (Button) findViewById(R.id.buttonPlay); 
    seekBar = (SeekBar) findViewById(R.id.seekBar); 
    lv = (ListView) findViewById(R.id.list); 
    getAudioList(); 
    ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,mSongsList); 
    lv.setAdapter(arrayAdapter); 
    onSongClick(); 
    setSeekBar(); 
    check(); 
} 



@Override 
protected void onStop() { 
    super.onStop(); 
    if (mBound) { 
     unbindService(mConnection); 
     mBound = false; 
    } 
} 


public void getAudioList() { 
    String orderBy = MediaStore.Audio.Media.TITLE ; 
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; 
    Cursor mCursor = getContentResolver().query(
      MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      new String[] { MediaStore.Audio.Media.TITLE, 
        MediaStore.Audio.Media._ID }, selection, null, orderBy); 

    int count = mCursor.getCount(); 
    while (mCursor.moveToNext()) { 
     mSongsList.add(mCursor.getString(mCursor 
       .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE))); 
     SongsPath.add(mCursor.getLong(mCursor 
       .getColumnIndexOrThrow(MediaStore.Audio.Media._ID))); 
    } 
    mCursor.close(); 
} 

public void onSongClick() 
{ 

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      mService.playSong(SongsPath.get(position)); 
      if(btnPlay.getText() == "Play") 
      { 
       btnPlay.setText("Pause"); 
      } 
     } 
    }); 
} 

public void onButtonClick(View view) { 
    int a = mService.stopMusic(); 
    if(a == 1) 
    { 
     btnPlay.setText("Play"); 
    } 
    else if (a ==2) 
    { 
     btnPlay.setText("Pause"); 
    } 
} 
public void check() 
{ 
    boolean check = mService.check(); 
} 
public void setSeekBar() 
{ 
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
      if (fromUser) 
      { 
        mService.setProg(progress*1000); 
      } 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 

     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 

     } 
    }); 
} 


} 

Service-

public class Music extends Service { 

Long b; 
MediaPlayer mp; 
private final IBinder mBinder = new LocalBinder(); 
public class LocalBinder extends Binder { 
    Music getService() { 

     return Music.this; 
    } 
} 

public void playSong(Long a) { 
    if (a!= b) 
    { if(mp!=null) 
      { 
       mp.stop(); 
       mp.release(); 
       mp=null; 
      } 
     b =a; 
     Uri contentUri = ContentUris.withAppendedId(
       android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, a); 
     mp = new MediaPlayer(); 
     mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     try { 
      mp.setDataSource(getApplicationContext(), contentUri); 
      mp.prepare(); 
      mp.setLooping(true); 
      mp.start(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    } 


public int stopMusic() 
{ 
    if(mp.isPlaying()) 
    { 
     mp.pause(); 
     return 1; 
    } 
    else if (mp!=null) 
    { 
     mp.start(); 
     return 2; 
    } 
    else 
    { 
     return 0; 
    } 
} 


public void setProg(int a) 
{ if(mp!=null) 
    { 
    mp.seekTo(a); 
    } 

} 

public boolean check() 
{ 
    if(mp!=null) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 

} 

@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 
} 

答えて

0

サービスはウル機能でmboundを使用してアクティビティに接続されていないかどうかがチェックされていません。mServiceはラインにnullであるので、あなたがNPEを取得しているonCreate()方法

を上書きします。

  public void check() 
       { 
        i f(mBound) 
        { 
      boolean check = mService.check(); 
        } 
      } 
0

おそらく、このラインからNPEを取得している:boolean check = mService.check();だからそれがnullであるmService、私はあなたがまだ開始されていないサービスをバインドしようとは思いませmp

+0

iは1と2のリターンで代わりにブールのint型を使用して疲れていはまだ –

+0

に動作しないと、正確にNPEがで指している行、[OK]を同じエラーに –

+0

を与えますか? –

0

をです。 startService(intent);の前に電話してくださいbindService(intent, mConnection, Context.BIND_AUTO_CREATE);

+1

Context.BIND_AUTO_CREATEオートは、そのイムがint公共int型を使用した場合でも – Sumit

0

サービスクラスはこの形式に従ってください。 mService.check();

public class MyService extends Service { 
    private Binder binder; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     binder = new Binder(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return binder; 
    } 

    public class Binder extends android.os.Binder { 
     public MyService getService() { 
      return MyService.this; // return instance 
     } 
    } 
} 
+0

を開始していない場合は、サービスを開始しますcheck(){if(mp!= null){return 1;} else {return 2;}}まだ動作しません –

+0

サービスはまだそれが作品をdoesntの代わりに、1または2を返すようにint型を使用して、私が試してみましたブール値を返すのサービスと間違って何も他の機能のためにその作業がない他の機能のため –

+0

を働いています –