我正在创建一个android应用程序,它将文件从SD卡上的目录加载到列表中。轻触项目后,它会解析其路径,并将其放入字符串中。我试图使用.SetDataSource()方法将字符串加载到媒体播放器中,但强制关闭并导致异常。我怎样才能解决这个问题?请帮帮我,谢谢!代码如下:
package com.mysoftwaremobileapps.Cross;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MusicActivity extends ListActivity {
//Called when the activity is first created
List<String> myList = new ArrayList<String>();
EditText AddItemToListViewEditText;
static final String[] COUNTRIES = new String[] {
"Movies"
};
MediaPlayer mp1;
String musicUri;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.musicscreen);
//Setup ListView
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
File mFile = new File(Environment.getExternalStorageDirectory() + "/Music/");
myList.addAll(Arrays.asList(mFile.list()));
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//When an item is clicked, play it on a MediaPlayer
try {
musicUri = Environment.getExternalStorageDirectory() + "/Music/" + myList.get(position);
mp1.setDataSource(musicUri);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
logcat的:
07-03 21:25:42.031: D/ViewConsistency(5165): AbsListView android.widget.ListView@40588c48 enabled= true
07-03 21:25:42.051: D/ViewConsistency(5165): AbsListView android.widget.ListView@40588c48 enabled= true
07-03 21:25:43.691: D/AndroidRuntime(5165): Shutting down VM
07-03 21:25:43.691: W/dalvikvm(5165): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-03 21:25:43.721: E/AndroidRuntime(5165): FATAL EXCEPTION: main
07-03 21:25:43.721: E/AndroidRuntime(5165): java.lang.NullPointerException
07-03 21:25:43.721: E/AndroidRuntime(5165): at com.mysoftwaremobileapps.Cross.MusicActivity$1.onItemClick(MusicActivity.java:52)
07-03 21:25:43.721: E/AndroidRuntime(5165): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-03 21:25:43.721: E/AndroidRuntime(5165): at android.widget.ListView.performItemClick(ListView.java:3513)
07-03 21:25:43.721: E/AndroidRuntime(5165): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1877)
07-03 21:25:43.721: E/AndroidRuntime(5165): at android.os.Handler.handleCallback(Handler.java:587)
07-03 21:25:43.721: E/AndroidRuntime(5165): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 21:25:43.721: E/AndroidRuntime(5165): at android.os.Looper.loop(Looper.java:130)
07-03 21:25:43.721: E/AndroidRuntime(5165): at android.app.ActivityThread.main(ActivityThread.java:3835)
07-03 21:25:43.721: E/AndroidRuntime(5165): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 21:25:43.721: E/AndroidRuntime(5165): at java.lang.reflect.Method.invoke(Method.java:507)
07-03 21:25:43.721: E/AndroidRuntime(5165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
07-03 21:25:43.721: E/AndroidRuntime(5165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
07-03 21:25:43.721: E/AndroidRuntime(5165): at dalvik.system.NativeStart.main(Native Method)
07-03 21:25:45.171: I/Process(5165): Sending signal. PID: 5165 SIG: 9
答案 0 :(得分:0)
你永远不会实例化MediaPlayer对象,这就是你正在抛出的NPE;你只留下它:MediaPlayer m1p
在设置数据源之前,需要使用以下命令对其进行实例化:
m1p = new MediaPlayer();