关于列表视图的错误
我尝试按照以下链接选择文件 http://www.dreamincode.net/forums/topic/ 190013-creating-simple-file-chooser/
但是,我收到错误“您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView”
我已经用 google 搜索了该错误,我需要获取 xml 文件中的 listview 标签。然而,上面的例子没有它,而且看起来运行良好。
虽然我没有触发主页中的文件选择器,但我认为我的代码与之没有太大区别。您能帮我看看有什么办法可以解决这个问题吗?
file_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/fd_text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textStyle="bold" android:layout_marginTop="5dip"></TextView>
<TextView android:id="@+id/fd_text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip"></TextView>
</LinearLayout>
filechooser.java 中的
public class filechooser extends ListActivity{
private fileArrayAdapter adapter;
private File curDir;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.file_view);
curDir = new File("/sdcard/");
fill(curDir);
}
public void fill(File f){
File[] dirs = f.listFiles();
this.setTitle("Current Dir: "+f.getName());
List<fc_option>dir = new ArrayList<fc_option>();
List<fc_option>fls = new ArrayList<fc_option>();
try{
for (File ff: dirs){
if (ff.isDirectory())
dir.add(new fc_option(ff.getName(), "Folder", ff.getAbsolutePath()));
else
fls.add(new fc_option(ff.getName(), "File Size: " + ff.length(), ff.getAbsolutePath()));
}
}catch (Exception e){
}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if (!f.getName().equalsIgnoreCase("sdcard"))
dir.add(0,new fc_option("..","Parent Directory", f.getParent()));
adapter = new fileArrayAdapter(this, R.layout.file_view, dir); //get problems
this.setListAdapter(adapter);
}
//@Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l,v,position, id);
fc_option o = adapter.getItem(position);
if (o.getData().equalsIgnoreCase("folder") || o.getData().equalsIgnoreCase("parent directory")){
curDir = new File(o.getPath());
fill(curDir);
}else{
onFileClick(o);
}
}
private void onFileClick(fc_option o){
Toast.makeText(this,"File Click"+o.getName(), Toast.LENGTH_SHORT).show();
}
}
fileArrayAdapter 与链接完全相同。
感谢您的帮助
I tried to make a file choose following the below link
http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/
However, I got the error "Your content must have a ListView whose id attribute is 'android.R.id.list' "
I have googled the error and I need to get a listview tag in the xml file. However, the above example does not have it and it seems working well.
Although I am not triggering the file chooser in the main page, I think my code does not have much differences to it. Could you help me to see if there are any ways to solve this problem, please?
file_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/fd_text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textStyle="bold" android:layout_marginTop="5dip"></TextView>
<TextView android:id="@+id/fd_text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip"></TextView>
</LinearLayout>
filechooser.java
public class filechooser extends ListActivity{
private fileArrayAdapter adapter;
private File curDir;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.file_view);
curDir = new File("/sdcard/");
fill(curDir);
}
public void fill(File f){
File[] dirs = f.listFiles();
this.setTitle("Current Dir: "+f.getName());
List<fc_option>dir = new ArrayList<fc_option>();
List<fc_option>fls = new ArrayList<fc_option>();
try{
for (File ff: dirs){
if (ff.isDirectory())
dir.add(new fc_option(ff.getName(), "Folder", ff.getAbsolutePath()));
else
fls.add(new fc_option(ff.getName(), "File Size: " + ff.length(), ff.getAbsolutePath()));
}
}catch (Exception e){
}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if (!f.getName().equalsIgnoreCase("sdcard"))
dir.add(0,new fc_option("..","Parent Directory", f.getParent()));
adapter = new fileArrayAdapter(this, R.layout.file_view, dir); //get problems
this.setListAdapter(adapter);
}
//@Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l,v,position, id);
fc_option o = adapter.getItem(position);
if (o.getData().equalsIgnoreCase("folder") || o.getData().equalsIgnoreCase("parent directory")){
curDir = new File(o.getPath());
fill(curDir);
}else{
onFileClick(o);
}
}
private void onFileClick(fc_option o){
Toast.makeText(this,"File Click"+o.getName(), Toast.LENGTH_SHORT).show();
}
}
the fileArrayAdapter is exactly the same as the link.
Thank you for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:
由于您的类正在扩展 ListActivity,因此它必须有一个 ListView。但正如我从你的布局文件(file_view)中看到的,它缺少一个。因此,在布局文件中添加一个ListView,并将其id属性设置为“@android:id/list”。
Updates:
Since your class is extending the ListActivity, it must have a ListView. But as I can see from your layout file (file_view), it lacks one. So add a ListView in your layout file and set its id attribute to "@android:id/list".
这行是罪魁祸首:
file_view.xml 是列表中每个项目的布局,而不是列表本身的布局。由于您要重写 ListActivity,因此不必设置视图,因此只需删除上面的行即可。
This line is to blame:
The file_view.xml is the layout for each item in the list, not the layout of the list itself. Since you are overriding a ListActivity, you don't have to set the view, so just removing the above line will do.