1

I'm trying to make an audio recorder for which I want to display a list of recordings done till now.

I'm able to get the list recorded files from my SD card in to an ArrayList, but my app crashes when it tries to populate the list view.

ListRecordings.java is called using an Intent from MainActivity.java.

Here is the for ListRecordings.java:

public class ListRecordings extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.listrecordings);
    ListView lv;
    String path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/MyAudioRecorder";

    ArrayList<String> FilesInFolder = GetFiles(path);

    lv = (ListView) findViewById(R.id.filelist);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.textlayout,
            FilesInFolder); 

    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            // Clicking on items
        }
    });
}



public ArrayList<String> GetFiles(String DirectoryPath) {
    ArrayList<String> MyFiles = new ArrayList<String>();
    File f = new File(DirectoryPath);

    f.mkdirs();
    File[] files = f.listFiles();

    if (files.length == 0)
        return null;
    else {
        for (int i = 0; i < files.length; i++)
            MyFiles.add(files[i].getName());
    }

    return MyFiles;
}
}

Here's the code for listrecordings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/filelist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Here's the code for textlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:textStyle="bold" >
</TextView>

Any help is appreciated.

I've just started with android so excuse me if i made any lame mistakes.

Thanks

8
  • 1
    post the stacktrace pls Commented Jul 25, 2014 at 5:50
  • what is your error? please post your logcat error here Commented Jul 25, 2014 at 5:52
  • I think the problem is the R.layout.textlayout, I'm not sure, but I don't think you can pass a xml here, but rather an Id. Commented Jul 25, 2014 at 5:53
  • As a wild guess, your app might crash if you try to list an empty directory. Does it work if you return Collections.EMPTY_LIST instead of null from GetFiles? Commented Jul 25, 2014 at 5:54
  • Probably you're getting NPE. Commented Jul 25, 2014 at 5:55

2 Answers 2

1

You missing this line in onCreate()

super.onCreate(savedInstanceState);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Haresh can't imagine how I missed it.
Glad to help you and if you carefully check your log error then you getting why is it not working.
For some reason my LogCat was not showing any log at all. I had to restart Eclipse to get the logs back. But yes I'll keep this in mind. Thanks
0

Add something like this

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.textlayout, R.id.textview_id_in_textlayout,FilesInFolder);

I hope this will help.

1 Comment

Hi, thanks for the help. I tried this but i'm getting and exception as follows: android.app.SuperNotCalledException: Activity {com.idsil.myaudiorecoder/com.idsil.myaudiorecoder.ListRecordings} did not call through to super.onCreate()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.