Friday, 15 February 2013

java - Android app crashes when filling aray list with name of audio files in sd card? -



java - Android app crashes when filling aray list with name of audio files in sd card? -

so working on application has 3 tabs swipe gesture. tab playlist listfragment. trying read names of sound in sd card , fill arraylist them.

here code manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.stupidmusicplayer.application" > <uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" android:launchmode="singletop" android:screenorientation="portrait" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>

this code adapter

package com.example.stupidmusicplayer.application; import java.util.arraylist; public class playlistarrayadapter extends arrayadapter<string> { private final context context; private final string[] artistarray; private final arraylist<string> songarray; public playlistarrayadapter(context context, arraylist songnames, string[] artistnames) { super(context, r.layout.listview_layout, songnames); this.context = context; songarray = songnames; artistarray = artistnames; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view rowview = inflater.inflate(r.layout.listview_layout, parent, false); textview songname = (textview) rowview.findviewbyid(r.id.text1); textview artistname = (textview) rowview.findviewbyid(r.id.text2); //set text text1 here songname.settext(songarray.get(position)); // set text text2 here artistname.settext(artistarray[position]); homecoming rowview; } }

and code in listfragment

public class playlist extends listfragment { public arraylist<string> songarray; @override public void oncreate(bundle icicle) { super.oncreate(icicle); findallmusicfiles(); string artistarray[] = new string[]{ //random info illustration in array... }; playlistarrayadapter adapter = new playlistarrayadapter(getactivity().getbasecontext(), songarray, artistarray); setlistadapter(adapter); } //the code fill arraylist sound file names. public void findallmusicfiles() { uri uri = mediastore.audio.media.external_content_uri; string[] projection = { mediastore.audio.audiocolumns.data, mediastore.audio.audiocolumns.display_name }; cursor audiocursor = getactivity().getbasecontext().getcontentresolver().query(uri, projection, null, null, null); if (audiocursor != null) { while (audiocursor.movetonext()) { songarray.add(audiocursor.getstring(audiocursor.getcolumnindex(mediastore.audio.audiocolumns.display_name))); } audiocursor.close(); } } }

if replace entire arrylist code simple array info works when seek bring in arraylist crashes.

adb logs say

"ddms: null java.lang.nullpointerexception @ com.android.ddmlib.client.read(client.java:698) @ com.android.ddmlib.monitorthread.processclientactivity(monitorthread.java:311) @ com.android.ddmlib.monitorthread.run(monitorthread.java:263)"

and

"ddms: can't bind local 8603 debugger"

help appreciated :)

in oncreate method within listfragment, first calling findallmusicfiles() tries add() songarray. however, don't see anywhere instantiating songarray. notice how setup artistarray in oncreate after phone call finallmusicfiles(). need similar song array somewhere , i'm not seeing is.

the other reference set songarray songnames, don't see guarantee songnames has been set either. getting nullpointerexception because songarray needs instantiated before seek manipulate in way.

java android android-listview

No comments:

Post a Comment