在ListView控件,显示的NullPointerException媒体文件? [英] Display Media files in ListView, NullPointerException?

查看:109
本文介绍了在ListView控件,显示的NullPointerException媒体文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MainActivity

public class MainActivity extends Activity 
{

/** Called when the activity is first created. */
private List<String> myList;
File file;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    Log.i("MEDIA", "A");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.i("MEDIA", "B");
    ListView listView = (ListView) findViewById(R.id.listView1);
    myList = new ArrayList<String>();

    Log.i("MEDIA", "C");
    File directory = Environment.getExternalStorageDirectory();
    file = new File(directory + "/Test");
    File list[] = file.listFiles();

    Log.i("MEDIA", "D");
    //for (int i = 0; i < list.length; i++)
    for (int i = 0; i < 5; i++)
    {
        Log.i("MEDIA", "D1"); //CRASHES HERE
        //myList.add(list[i].getName());
        myList.add(list[i].getName());
        Log.i("MEDIA", "D2");
    }

    Log.i("MEDIA", "E");
    final StableArrayAdapter adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, myList);
    listView.setAdapter(adapter); 
    // Set all the file in the list.
}

private class StableArrayAdapter extends ArrayAdapter<String> 
  {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,List<String> objects) 
    {
      super(context, textViewResourceId, objects);
      for (int i = 0; i < objects.size(); ++i) 
      {
        mIdMap.put(objects.get(i), i);
      }
    }

    @Override
    public long getItemId(int position) 
    {
      String item = getItem(position);
      return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() 
    {
      return true;
    }

  }
}

日志: http://pastebin.com/WWmqKKvQ

我挑选的答案上形成SO这个问题:
<一href=\"http://stackoverflow.com/questions/9782168/how-to-show-audio-files-in-a-listview-in-android\">How显示在Android的列表视图音频文件?

I've picked the answer form this question on SO: How to show audio files in a listview in Android?

也要想开展试过这个教程:
http://www.androidhive.info/2012/03/ Android的建筑音频播放器教程/

Andhave also tried this tutorial: http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/

推荐答案

该probem是/测试目录不存在,所以我用/蓝牙(这是一个真实存在的目录)取代了它,现在它会显示该文件夹中的文件列表。

The probem was that "/Test" directory does not exist, so i replaced it with "/Bluetooth" (which is a dir that exists) , and now it displays a list of files in that folder.

问题解决了。 :D

    Log.i("MEDIA", "C");
    File directory = Environment.getExternalStorageDirectory();
    file = new File(directory+ "/Bluetooth");// + "/Test");
    File list[] = file.listFiles();

这篇关于在ListView控件,显示的NullPointerException媒体文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆