1

以下代码引发 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
4

2 回答 2

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

于 2013-02-15T21:56:59.383 回答
0

根据您的评论例外 - 您正在实施 ListActivity,但您的布局中没有 ListView。要么添加一个 id 为 android:id.list 的列表,要么从 Activity 派生而不是 ListActivity。

于 2013-02-15T21:56:59.587 回答