setCompoundDrawablesWithIntrinsicBounds関数を使用して、ListViewの各アイテムの左側にアイコンを追加しようとしましたが、NullPointerExceptionが発生し続けます。私はこれを行う他の方法を見てきましたが、この方法で試したときにこのエラーが発生し続けるのではないかと思っていました。
ListView(mediaitems.xml)の各行のXMLファイルは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mediatext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp"
>
SDカードのディレクトリを参照するためのコードは次のとおりです。
public class SDCardExplorer extends ListActivity {
private String mediapath = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
private List<String> item = null;
private List<String> path = null;
private TextView mypath;
private TextView mediatext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medialist);
mypath = (TextView) findViewById(R.id.mypath);
mediatext = (TextView) findViewById(R.id.mediatext);
LoadDirectory(mediapath);
}
// class to limit the choices shown when browsing to SD card to media files
public class AudioFilter implements FileFilter {
// only want to see the following audio file types
private String[] extension = {".aac", ".mp3", ".wav", ".ogg", ".midi", ".3gp", ".mp4", ".m4a", ".amr", ".flac"};
@Override
public boolean accept(File pathname) {
// if we are looking at a directory that's not hidden we want to see it so return TRUE
if (pathname.isDirectory() && !pathname.isHidden()) {
return true;
}
// loops through and determines the extension of all files in the directory
// returns TRUE to only show the audio files defined in the String[] extension array
for (String ext : extension) {
if (pathname.getName().toLowerCase().endsWith(ext)) {
return true;
}
}
return false;
}
}
private void LoadDirectory(String dirPath) {
mypath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles(new AudioFilter());
// If we aren't in the SD card root directory, add "Up" to go back to previous folder
if(!dirPath.equals(mediapath)) {
item.add("Up");
path.add(f.getParent());
mediatext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_upicon, 0, 0, 0);
//mediatext.setCompoundDrawables(getResources().getDrawable(R.drawable.ic_upicon), null, null, null);
}
// Loops through the files and lists them
for (int i = 0; i < files.length; i++) {
File file = files[i];
path.add(file.getPath());
// Add "/" to indicate you are looking at a folder
if(file.isDirectory()) {
item.add(file.getName() + "/");
mediatext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_foldericon, 0, 0, 0);
}
else {
item.add(file.getName());
mediatext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_audioicon, 0, 0, 0);
}
}
// Displays the directory list on the screen
setListAdapter(new ArrayAdapter<String>(this, R.layout.mediaitems, item));
}
LogCatエラーは次のとおりです。
02-03 13:00:48.295: E/ACRA(8231): iSleep fatal error : Unable to start activity ComponentInfo{com.bromancelabs.isleep/com.bromancelabs.isleep.SDCardExplorer}: java.lang.NullPointerException
02-03 13:00:48.295: E/ACRA(8231): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bromancelabs.isleep/com.bromancelabs.isleep.SDCardExplorer}: java.lang.NullPointerException
02-03 13:00:48.295: E/ACRA(8231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-03 13:00:48.295: E/ACRA(8231): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-03 13:00:48.295: E/ACRA(8231): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-03 13:00:48.295: E/ACRA(8231): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-03 13:00:48.295: E/ACRA(8231): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 13:00:48.295: E/ACRA(8231): at android.os.Looper.loop(Looper.java:123)
02-03 13:00:48.295: E/ACRA(8231): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 13:00:48.295: E/ACRA(8231): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 13:00:48.295: E/ACRA(8231): at java.lang.reflect.Method.invoke(Method.java:521)
02-03 13:00:48.295: E/ACRA(8231): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-03 13:00:48.295: E/ACRA(8231): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-03 13:00:48.295: E/ACRA(8231): at dalvik.system.NativeStart.main(Native Method)
02-03 13:00:48.295: E/ACRA(8231): Caused by: java.lang.NullPointerException
02-03 13:00:48.295: E/ACRA(8231): at com.bromancelabs.isleep.SDCardExplorer.LoadDirectory(SDCardExplorer.java:116)
02-03 13:00:48.295: E/ACRA(8231): at com.bromancelabs.isleep.SDCardExplorer.onCreate(SDCardExplorer.java:60)
02-03 13:00:48.295: E/ACRA(8231): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-03 13:00:48.295: E/ACRA(8231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-03 13:00:48.295: E/ACRA(8231): ... 11 more
誰もが提供できる助けをありがとう。