android上的音频文件ListView

时间:2013-10-23 16:08:46

标签: android listview audio

我正在尝试收集设备外部源上的所有音频文件。当我在设备上运行时,我收到一条消息,说“不幸”已停止。我还没有找到问题持续存在的地方。我看过网上,发现了多个这样的例子就是这样,没有什么可行的。

  package com.example.djraspi;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.provider.MediaStore;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.database.Cursor;

public class MainActivity extends ListActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMusicList();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initMusicList(){
String selection = MediaStore.Audio.Media.IS_MUSIC + " !=0";
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST_ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};

Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);
List<String> songList = new ArrayList<String>();
while(cursor.moveToNext()){
songList.add(cursor.getString(0) + 
"||" + cursor.getString(1) + "||" +   cursor.getString(2) + "||" +   cursor.getString(3) + "||" +  cursor.getString(4) + "||" +  cursor.getString(5) + "||" +  cursor.getString(6));
}
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main, android.R.id.list, songList));


}

}

这是我的activity_main

   <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_marginTop="20dp"/>

</RelativeLayout>

这是我得到的logCat文件:

10-23 12:35:44.093: I/Process(8677): Sending signal. PID: 8677 SIG: 9
10-23 12:36:42.131: E/Trace(8837): error opening trace file: No such file or directory (2)
10-23 12:36:42.264: D/AndroidRuntime(8837): Shutting down VM
10-23 12:36:42.264: W/dalvikvm(8837): threadid=1: thread exiting with uncaught exception (group=0x40dbf2a0)
10-23 12:36:42.303: E/AndroidRuntime(8837): FATAL EXCEPTION: main
10-23 12:36:42.303: E/AndroidRuntime(8837): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.djraspi/com.example.djraspi.MainActivity}: java.lang.NullPointerException
10-23 12:36:42.303: E/AndroidRuntime(8837):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at android.app.ActivityThread.access$700(ActivityThread.java:141)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at android.os.Looper.loop(Looper.java:137)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at android.app.ActivityThread.main(ActivityThread.java:5059)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at java.lang.reflect.Method.invoke(Method.java:511)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at dalvik.system.NativeStart.main(Native Method)
10-23 12:36:42.303: E/AndroidRuntime(8837): Caused by: java.lang.NullPointerException
10-23 12:36:42.303: E/AndroidRuntime(8837):     at com.example.djraspi.MainActivity.initMusicList(MainActivity.java:43)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at com.example.djraspi.MainActivity.onCreate(MainActivity.java:21)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at android.app.Activity.performCreate(Activity.java:5058)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-23 12:36:42.303: E/AndroidRuntime(8837):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
10-23 12:36:42.303: E/AndroidRuntime(8837):     ... 11 more
10-23 12:41:48.053: E/Trace(9081): error opening trace file: No such file or directory (2)
10-23 12:41:48.467: D/AndroidRuntime(9081): Shutting down VM
10-23 12:41:48.467: W/dalvikvm(9081): threadid=1: thread exiting with uncaught exception (group=0x40dbf2a0)
10-23 12:41:48.467: E/AndroidRuntime(9081): FATAL EXCEPTION: main
10-23 12:41:48.467: E/AndroidRuntime(9081): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.djraspi/com.example.djraspi.MainActivity}: java.lang.NullPointerException
10-23 12:41:48.467: E/AndroidRuntime(9081):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at android.app.ActivityThread.access$700(ActivityThread.java:141)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at android.os.Looper.loop(Looper.java:137)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at android.app.ActivityThread.main(ActivityThread.java:5059)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at java.lang.reflect.Method.invoke(Method.java:511)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at dalvik.system.NativeStart.main(Native Method)
10-23 12:41:48.467: E/AndroidRuntime(9081): Caused by: java.lang.NullPointerException
10-23 12:41:48.467: E/AndroidRuntime(9081):     at com.example.djraspi.MainActivity.initMusicList(MainActivity.java:43)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at com.example.djraspi.MainActivity.onCreate(MainActivity.java:21)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at android.app.Activity.performCreate(Activity.java:5058)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-23 12:41:48.467: E/AndroidRuntime(9081):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
10-23 12:41:48.467: E/AndroidRuntime(9081):     ... 11 more

3 个答案:

答案 0 :(得分:0)

试试这个:

<ListView 
     android:id="@android:id/list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" />

答案 1 :(得分:0)

您正在使用ListActivity。将您的listView ID设置为android:id="@android:id/list"中的XML

<ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_marginTop="20dp"
        android:listSelector="@drawable/menu_listitem_selector"/>

答案 2 :(得分:0)

使用listview的id作为android的列表。

<ListView 
     android:id="@android:id/list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" />

你读过logcat了吗? 它的说法&#34; java.lang.RuntimeException:你的内容必须有一个ListView,其id属性是&#39; android.R.id.list&#34; ,所以给出id列表,你会找到解决方案。