【问题标题】:Android File Browser throws IO Exception?Android 文件浏览器抛出 IO 异常?
【发布时间】:2013-01-31 22:54:45
【问题描述】:

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

【问题讨论】:

  • 请给我们完整的崩溃堆栈跟踪。
  • 它说我收到了一个未处理的 IOException,当我尝试捕获它时,应用程序崩溃并且我在 LogCat 中得到以下信息:
  • 02-15 21:51:20.558: E/AndroidRuntime(23389): Caused by: java.lang.RuntimeException: 你的内容必须有一个 ID 属性为 'android.R.id.列表'
  • 没有stacktrace真的很难帮忙,可能是你没有设置权限,可能是文件夹不存在......

标签: java android file audio browser


【解决方案1】:

你需要设置列表视图

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

【讨论】:

  • 这仍然不起作用,由于没有带有 id 列表的列表视图,我得到运行时异常,即使我有。
  • 您的布局 activity_browser 必须有一个带有 id 的列表视图。您指定 id 并设置列表。请添加完整的堆栈跟踪和activity_browser的xml
  • 稍后我会在我的电脑上添加必要的 XML。
  • ListView listView = (ListView) findViewById(R.id.list); ArrayAdapter 适配器 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, fileNames); setListAdapter(适配器); listView.setAdapter(适配器);
  • 我现在可以使用它,但我无法找到资产文件夹。如果我将路径作为资产,我只会得到一个空白列表。
【解决方案2】:

根据您的 cmets 异常 - 您正在实现 ListActivity,但您的布局中没有 ListView。添加一个 id 为 android:id.list 的列表,或者从 Activity 派生而不是 ListActivity。

【讨论】:

  • 不过,我有一个包含 id 列表的列表视图。
猜你喜欢
  • 2021-12-25
  • 2012-04-19
  • 2021-08-26
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多