各位开发者高手们好!
因为小弟目前是新手,也着手于ListView的使用
但是发现并无法把清单上面显示的文字抓出来
烦请各位高手们帮忙看看我的code是哪边有问题
谢谢各位大大!!!
public class MainActivity extends ListActivity {
private File file;
private List<String> myList;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> myList = new ArrayList<String>();
String root_sd = Environment.getExternalStorageDirectory().toString();
File file = new File( root_sd ) ;
File list[] = file.listFiles();
for(int i=0 ;i<list.length;i++)
{
myList.add(list[i].getAbsolutePath());
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myList ));
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(),position+"",
Toast.LENGTH_LONG).show();
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;
}
}
在这上面的code中会出现阵列的顺序如 0 1 2这样
但是又尝试了
Toast.makeText(getApplicationContext(),myList.get(position)+"",
Toast.LENGTH_LONG).show();
而出现了错误讯息 如下
08-25 09:24:49.432: E/AndroidRuntime(28120): FATAL EXCEPTION: main
08-25 09:24:49.432: E/AndroidRuntime(28120): java.lang.NullPointerException
08-25 09:24:49.432: E/AndroidRuntime(28120):
at com.example.projectname.MainActivity.onListItemClick(MainActivity.java:48)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at android.widget.AbsListView.performItemClick(AbsListView.java:1244)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3013)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at android.widget.AbsListView$1.run(AbsListView.java:3718)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at android.os.Handler.handleCallback(Handler.java:615)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at android.os.Handler.dispatchMessage(Handler.java:92)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at android.os.Looper.loop(Looper.java:137)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at android.app.ActivityThread.main(ActivityThread.java:4881)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at java.lang.reflect.Method.invokeNative(Native Method)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at java.lang.reflect.Method.invoke(Method.java:511)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
08-25 09:24:49.432: E/AndroidRuntime(28120):
at dalvik.system.NativeStart.main(Native Method)
烦请高手们帮解决 感恩不尽!!!!