无法在Android

时间:2016-11-12 06:19:20

标签: android

我是 android 开发的初学者,我正在开发音乐应用,我在列表中查询了所有 SD卡音乐但是我无法通过名称或ID 播放所选歌曲。我已经在 listView 中列出了它,通过点击所选项目,它显示了我选择的歌曲名称,但没有使用媒体播放器类播放。请帮我解决一下。

1。这是 First Java File ,用于从SD卡获取歌曲列表:

公共类MusicAppList扩展了Activity {

TextView tv;
 ContentResolver cr;
 Cursor cs;
Uri uri;
ListView lv;
 String tname,csong;

 protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_music_app_list);

      lv=(ListView)findViewById(R.id.songslist);
      Songs();

}
 public void Songs(){

    cr=getContentResolver();
    uri=android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    cs=cr.query(uri,null,null, null,"title ASC" );

   final ArrayList<String> list=new ArrayList<String>();
    while(cs.moveToNext()){
      list.add(cs.getString(cs.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE)));
     }
  ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.mlistinterface,list);
  lv.setAdapter(adapter);

  lv.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) { 

        csong=(String)parent.getItemAtPosition(position);
        Intent i1=new Intent(getApplicationContext(),MusicPlaying.class);
        Bundle b=new Bundle();
        b.putInt("pos", position);
        b.putString("song", csong);
        b.putStringArrayList("songlist", list);
        i1.putExtras(b);
        startActivity(i1);
        }
    });   }}

2。以下是第二个Java文件,我正在尝试播放这首歌。

公共类MusicPlaying扩展了Activity {

TextView tv;
ImageView pre,nxt,play;
ArrayList<String>songList;
String name;
private MediaPlayer mp;
String song;

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

    tv=(TextView)findViewById(R.id.songname);
    pre=(ImageView)findViewById(R.id.previous);
    nxt=(ImageView)findViewById(R.id.next);
    play=(ImageView)findViewById(R.id.play);

    tv.setSelected(true);

    Bundle bb=getIntent().getExtras();
    name=bb.getString("song");
    tv.setText(String.valueOf(name));
    songList=(ArrayList)bb.getStringArrayList("songlist") ;
    int pos=bb.getInt("pos");
    mp=new MediaPlayer();
    Uri u=Uri.parse(songList.get(pos));

    try{
        mp.setDataSource(getApplicationContext(), u);
        mp.prepareAsync();
        mp.start();

    }
    catch(Exception e){
        Log.e("ErrorPlaying", "Unable to get Reference of the song");
    }
}}

3。 Logcat错误如下: -

11-15 15:57:13.503:E / AndroidRuntime(996):致命异常:主要 11-15 15:57:13.503:E / AndroidRuntime(996):进程:com.example.musicapp,PID:996

11-15 15:57:13.503:E / AndroidRuntime(996):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.musicapp / com.example.musicapp.MusicPlaying}:java.lang。 NullPointerException:尝试调用虚方法&#39; void android.media.MediaPlayer.prepareAsync()&#39;在null对象引用上 11-15 15:57:13.503:E / AndroidRuntime(996):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455) 11-15 15:57:13.503:E / AndroidRuntime(996):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517) 11-15 15:57:13.503:E / AndroidRuntime(996):在android.app.ActivityThread.access $ 800(ActivityThread.java:162) 11-15 15:57:13.503:E / AndroidRuntime(996):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1412) 11-15 15:57:13.503:E / AndroidRuntime(996):在android.os.Handler.dispatchMessage(Handler.java:106) 11-15 15:57:13.503:E / AndroidRuntime(996):在android.os.Looper.loop(Looper.java:189) 11-15 15:57:13.503:E / AndroidRuntime(996):在android.app.ActivityThread.main(ActivityThread.java:5529) 11-15 15:57:13.503:E / AndroidRuntime(996):at java.lang.reflect.Method.invoke(Native Method) 11-15 15:57:13.503:E / AndroidRuntime(996):at java.lang.reflect.Method.invoke(Method.java:372) 11-15 15:57:13.503:E / AndroidRuntime(996):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:950) 11-15 15:57:13.503:E / AndroidRuntime(996):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 11-15 15:57:13.503:E / AndroidRuntime(996):引起:java.lang.NullPointerException:尝试调用虚方法&#39; void android.media.MediaPlayer.prepareAsync()&#39;在null对象引用上 11-15 15:57:13.503:E / AndroidRuntime(996):at com.example.musicapp.MusicPlaying.onCreate(MusicPlaying.java:60) 11-15 15:57:13.503:E / AndroidRuntime(996):在android.app.Activity.performCreate(Activity.java:5966) 11-15 15:57:13.503:E / AndroidRuntime(996):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 11-15 15:57:13.503:E / AndroidRuntime(996):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)

1 个答案:

答案 0 :(得分:0)


我的建议是查看 ApplicationManifest.xml 并检查以确保您具有从外部SD卡写/读的必要权限。

典型的外部SD卡许可

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


显然,如果运行Marshallow或更高级别,您将不得不向用户请求许可,这可能是一种痛苦。