以下代码引发IO异常。有任何想法吗?此代码是作为音乐制作应用程序一部分的文件浏览器的,我想向用户展示他们在文件夹( Assets )中存在哪些音频循环。
package com.razor5cl.droiddub;
import android.app.ListActivity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
public class BrowserActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
Intent intent = getIntent();
String button = intent.getStringExtra("id");
AssetManager assetManager = getAssets();
String[] fileNames =assetManager.list("loops");
}
}
[编辑]对不起,延迟人员。这也是 promise 的XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#111111"
tools:context=".MainActivity"
>
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="467dp" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:background="#111111"
tools:context=".MainActivity"
>
<Button
android:layout_width="100dip"
android:layout_height="50dip"
android:text="@string/preview_text"
android:textSize="20sp"
android:id="@+id/preview_button"
android:layout_marginRight="10dip"
/>
<Button
android:layout_width="100dip"
android:layout_height="50dip"
android:text="@string/select_text"
android:textSize="20sp"
android:id="@+id/select_button"
android:layout_marginLeft="10dip"
/>
</LinearLayout>
</LinearLayout>
和堆栈跟踪:
02-24 10:04:42.445: E/AndroidRuntime(10029): FATAL EXCEPTION: 02-24 10:04:42.445: E/AndroidRuntime(10029): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.razor5cl.droiddub/com.razor5cl.droiddub.BrowserActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.os.Looper.loop(Looper.java:137)
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-24 10:04:42.445: E/AndroidRuntime(10029): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 10:04:42.445: E/AndroidRuntime(10029): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 10:04:42.445: E/AndroidRuntime(10029): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-24 10:04:42.445: E/AndroidRuntime(10029): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-24 10:04:42.445: E/AndroidRuntime(10029): at dalvik.system.NativeStart.main(Native Method)
02-24 10:04:42.445: E/AndroidRuntime(10029): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
02-24 10:04:42.445: E/AndroidRuntime(10029): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.app.Activity.setContentView(Activity.java:1881)
02-24 10:04:42.445: E/AndroidRuntime(10029): at com.razor5cl.droiddub.BrowserActivity.onCreate(BrowserActivity.java:21)
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.app.Activity.performCreate(Activity.java:5104)
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-24 10:04:42.445: E/AndroidRuntime(10029): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-24 10:04:42.445: E/AndroidRuntime(10029): ... 11 more
最佳答案
您需要设置列表 View
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fileNames );
setListAdapter(adapter);
举个例子,请看http://www.vogella.com/articles/AndroidListView/article.html