以下代码抛出IO异常。有任何想法吗?此代码用于文件浏览器作为音乐制作应用程序的一部分,我想向用户显示他们在文件夹(资产)中有哪些音频循环。
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");
}
}
[编辑]对不起延迟的人。这是承诺的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
答案 0 :(得分:0)
您需要设置列表视图
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
答案 1 :(得分:0)
根据您的评论异常 - 您正在实施ListActivity,但您的布局中没有ListView。添加一个id为android:id.list的列表,或者从Activity而不是ListActivity派生。