有人可以帮我解决下面代码中的问题。 当我尝试运行下面的代码时,我得到空指针异常,应用程序结束。
但是当我尝试在Android 2.3版本中运行相同的代码时,它正在工作,但是当我尝试在android 4.4中运行它时,它给出了空指针异常。
public class MainActivity extends ActionBarActivity implements OnClickListener {
SeekBar seek_bar;
public Songlist songmanager;
public ArrayList<HashMap<String,String>> Songlist = new ArrayList<HashMap<String,String>>();
ImageButton play_button, pause_button, stop_button, list_button;
MediaPlayer player;
TextView text_shown;
Handler seekHandler = new Handler(); /** Called when the activity is first created. */
private static final String TAG = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player = new MediaPlayer();
songmanager = new Songlist();
Songlist = songmanager.getplaylist();
seek_bar = (SeekBar) findViewById(R.id.seekBar1);
play_button = (ImageButton) findViewById(R.id.play);
pause_button = (ImageButton) findViewById(R.id.pause);
stop_button = (ImageButton)findViewById(R.id.stop);
list_button = (ImageButton) findViewById(R.id.list);
text_shown = (TextView) findViewById(R.id.textView1);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
stop_button.setOnClickListener(this);
stop_button.setImageResource(R.drawable.stop_int);
play_button.setImageResource(R.drawable.play_int);
pause_button.setImageResource(R.drawable.pause_int);
list_button.setImageResource(R.drawable.listbtn);
playsong(0);
list_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list_button.setImageResource(R.drawable.listbtn_aft);
Intent in1 = new Intent(getBaseContext(),Playlist.class);
startActivityForResult(in1, 100);
}
});
}
public void onActivityResult(int requestcode, int resultcode, Intent data){
list_button.setImageResource(R.drawable.listbtn);
super.onActivityResult(requestcode, resultcode, data);
if (resultcode == 100){
int songnum = data.getExtras().getInt("songindex");
playsong(songnum);
player.start();
play_button.setImageResource(R.drawable.play_aft);
pause_button.setImageResource(R.drawable.pause_int);
stop_button.setImageResource(R.drawable.stop_int);
seek_bar.setMax(player.getDuration());
}
}
private void playsong(int songnum) {
try {
player.reset();
player.setDataSource(Songlist.get(songnum).get("songname"));
player.prepare();
seek_bar.setProgress(0);
seek_bar.setMax(100);
seekUpdation();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Runnable run = new Runnable() {
@Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
public void onDestroy(){
player.stop();
player.release();
Log.d(TAG, "Player Crushed");
super.onDestroy();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play:
text_shown.setText("Playing...");
player.start();
pause_button.setImageResource(R.drawable.pause_int);
stop_button.setImageResource(R.drawable.stop_int);
play_button.setImageResource(R.drawable.play_aft);
seek_bar.setMax(player.getDuration());
break;
case R.id.pause:
if(player.isPlaying()){
player.pause();
text_shown.setText("Paused...");
play_button.setImageResource(R.drawable.play_int);
pause_button.setImageResource(R.drawable.pause_aft);
}
break;
case R.id.stop:
if(player.isPlaying()){
player.stop();
text_shown.setText("Stopped...");
play_button.setImageResource(R.drawable.play_int);
pause_button.setImageResource(R.drawable.pause_int);
stop_button.setImageResource(R.drawable.stop_aft);
player.prepareAsync();
player.seekTo(0);
}
else{
try {
player.prepare();
player.seekTo(0);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
}
}
class MP3filter implements FilenameFilter{
@Override
public boolean accept(File dir, String filename) {
// TODO Auto-generated method stub
return (filename.endsWith(".mp3"));
}
}
public class Songlist extends ActionBarActivity{
public ArrayAdapter<String> ad;
public ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String, String>> ();
@SuppressLint("SdCardPath") public final String SD_CARD = new String ("/sdcard/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getplaylist();
}
public ArrayList<HashMap<String, String>> getplaylist() {
// TODO Auto-generated method stub
File home = new File(SD_CARD);
if(home.listFiles(new MP3filter()).length > 0){
for(File file:home.listFiles(new MP3filter())){
HashMap<String, String> song = new HashMap<String, String>();
song.put("songname", file.getPath());
al.add(song);
}
}
return al;
}
}
logcat的
08-28 23:18:49.002: E/AndroidRuntime(2118): FATAL EXCEPTION: main
08-28 23:18:49.002: E/AndroidRuntime(2118): Process: com.example.musicplay, PID: 2118
08-28 23:18:49.002: E/AndroidRuntime(2118): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.musicplay/com.example.musicplay.MainActivity}: java.lang.NullPointerException
08-28 23:18:49.002: E/AndroidRuntime(2118): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-28 23:18:49.002: E/AndroidRuntime(2118): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-28 23:18:49.002: E/AndroidRuntime(2118): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-28 23:18:49.002: E/AndroidRuntime(2118): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-28 23:18:49.002: E/AndroidRuntime(2118): at android.os.Handler.dispatchMessage(Handler.java:102)
08-28 23:18:49.002: E/AndroidRuntime(2118): at android.os.Looper.loop(Looper.java:136)
08-28 23:18:49.002: E/AndroidRuntime(2118): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-28 23:18:49.002: E/AndroidRuntime(2118): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 23:18:49.002: E/AndroidRuntime(2118): at java.lang.reflect.Method.invoke(Method.java:515)
08-28 23:18:49.002: E/AndroidRuntime(2118): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-28 23:18:49.002: E/AndroidRuntime(2118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-28 23:18:49.002: E/AndroidRuntime(2118): at dalvik.system.NativeStart.main(Native Method)
08-28 23:18:49.002: E/AndroidRuntime(2118): Caused by: java.lang.NullPointerException
08-28 23:18:49.002: E/AndroidRuntime(2118): at com.example.musicplay.Songlist.getplaylist(Songlist.java:39)
08-28 23:18:49.002: E/AndroidRuntime(2118): at com.example.musicplay.MainActivity.onCreate(MainActivity.java:38)
08-28 23:18:49.002: E/AndroidRuntime(2118): at android.app.Activity.performCreate(Activity.java:5231)
08-28 23:18:49.002: E/AndroidRuntime(2118): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-28 23:18:49.002: E/AndroidRuntime(2118): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-28 23:18:49.002: E/AndroidRuntime(2118): ... 11 more
答案 0 :(得分:1)
而不是public final String SD_CARD = new String ("/sdcard/");
写
public final String SD_CARD = new String (Environment.getExternalStorageDirectory());
答案 1 :(得分:0)
查找SD_CARD路径的方法是正确的
public final String SD_CARD = new String(Environment.getExternalStorageDirectory()。getpath());
你需要做的两件事是
1.明确清单中的权限
使用权限android:name =&#34; android.permission.READ_EXTERNAL_STORAGE&#34;
2.还要检查设备是否有外部存储。如果是,那么它是否可读。要检查它的可用性,您可以按照文档中提到的代码进行操作。
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal