SDカードのディレクトリからリストにファイルをロードするAndroidアプリケーションを作成しています。アイテムがタップされると、そのパスが解決され、文字列に入れられます。.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();
}
}
});
}
}
ログキャット:
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