ListView
的android设备SD卡上的某个文件夹中显示所有文件,到目前为止我运气不好,它崩溃了一个nullpointer异常,所以我想知道如果这里的任何人都可以在我的活动和下面的logcat上点亮我的问题,并且感谢。
活动
package com.mkyong.android;
import java.io.File;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
public class ListMobileActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.res);
}
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;
}
{
ListView lv;
ArrayList<String> FilesInFolder = GetFiles("/sdcard/RootBox");
lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, FilesInFolder));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
}
和logcat
05-24 06:42:28.992: E/AndroidRuntime(796): FATAL EXCEPTION: main
05-24 06:42:28.992: E/AndroidRuntime(796): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.ListMobileActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-24 06:42:28.992: E/AndroidRuntime(796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-24 06:42:28.992: E/AndroidRuntime(796): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-24 06:42:28.992: E/AndroidRuntime(796): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-24 06:42:28.992: E/AndroidRuntime(796): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-24 06:42:28.992: E/AndroidRuntime(796): at android.os.Handler.dispatchMessage(Handler.java:99)
答案 0 :(得分:1)
如果您在Android清单中要求权限以读取存储空间,那么这可能就是问题所在。
从代码中可以看出,您正在查询实例块中的列表视图实例。 这可能在调用setContentView之前调用。 在布局膨胀之后,您应该在XML中实例化一个UI组件,否则您将遇到异常。
试试这个。
public class ListMobileActivity extends ListActivity {
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.res);
ArrayList<String> FilesInFolder = GetFiles("/sdcard/RootBox");
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, FilesInFolder));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
}
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;
}
}
它应该有用。
答案 1 :(得分:0)
在日志中:
ListMobileActivity}:java.lang.RuntimeException:您的内容必须 有一个ListView,其id属性是'android.R.id.list'
如果要扩展ListActivity以创建ListView,则需要将xml中的ListView id设置为“android.R.id.list”。将其作为res
布局:
<ListView android:id="@id/android:list" <!--set ListView id to android.R.id.list-->
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"/>
并在代码中将ListView实例初始化为:
lv = (ListView)findViewById(android.R.id.list);
OR
lv =ListMobileActivity.this.getListView();
答案 2 :(得分:0)
试试这个:
package com.mkyong.android;
import java.io.File;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
public class ListMobileActivity extends ListActivity {
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.res);
ArrayList<String> FilesInFolder = GetFiles("/sdcard/RootBox");
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, FilesInFolder));
}
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;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
}