我正在为Android应用程序和一些我需要的是,它显示了SD卡中的所有文件和目录的列表,它必须能够移动通过不同的目录。我发现一个很好的教程在anddev 。我修改了一些东西,所以在SD卡上的应用程序移动,而不是在Android的根目录下,但其余大部分是相同的。
I'm doing an application for Android and something I need is that it shows a list of all files and directories in the SD Card and it has to be able to move through the different directories. I found a good tutorial in anddev. I modified a few things so the application moves in the SD Card and not in Android root Directories but the rest is mostly the same.
这是我对活动的xml文件:
This is my xml file for the activity:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ListView>
这是code的活动:
And this is the code for the Activity:
import hackreatorz.cifrador.R;
import java.io.File;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ArchivosSD extends ListActivity {
private ArrayList<String> directoryEntries = new ArrayList<String>();
private File currentDirectory = new File("/sdcard/");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
browseToSD();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private void browseToSD() {
browseTo(new File("/sdcard/"));
}
private void upOneLevel() {
if(this.currentDirectory.getParent() != null)
this.browseTo(this.currentDirectory.getParentFile());
}
private void browseTo(final File directory) {
if (directory.isDirectory()) {
this.currentDirectory = directory;
fill(directory.listFiles());
} else {
Toast.makeText(ArchivosSD.this, this.directoryEntries.get(this.getSelectedItemPosition()), Toast.LENGTH_SHORT).show();
}
}
private void fill(File[] files) {
this.directoryEntries.clear();
this.directoryEntries.add(getString(R.string.current_dir));
if(this.currentDirectory.getParent() != null)
this.directoryEntries.add(getString(R.string.up_one_level));
int currentPathStringLength = (int) this.currentDirectory.getAbsoluteFile().length();
for (File file : files) {
this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLength));
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.archivos_sd, this.directoryEntries));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int selectionRowID = (int) this.getSelectedItemPosition();
String selectedFileString = this.directoryEntries.get(selectionRowID);
if (selectedFileString.equals(".")) {
this.browseToSD();
} else if(selectedFileString.equals("..")) {
this.upOneLevel();
} else {
File clickedFile = null;
clickedFile = new File(this.currentDirectory.getAbsolutePath() + this.directoryEntries.get(selectionRowID));
if(clickedFile != null)
this.browseTo(clickedFile);
}
}
}
我不明白在Eclipse中的任何错误,但运行在我的手机应用程序,当我得到一个强制关闭,当我看着我的logcat看到以下内容:
I don't get any errors in Eclipse, but I get a Force Close when running the application on my phone and when I look at Logcat I see the following:
01-01 23:30:29.858:ERROR / AndroidRuntime(14911):致命异常:主要的 * 01-01 23:30:29.858:ERROR / AndroidRuntime(14911):java.lang.UnsupportedOperationException:addView(查看,的LayoutParams)不支持适配器视图*
01-01 23:30:29.858: ERROR/AndroidRuntime(14911): FATAL EXCEPTION: main *01-01 23:30:29.858: ERROR/AndroidRuntime(14911): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView*
我没有一个线索做什么,我看着在谷歌和我没有找到任何东西,我也做了同样的计算器。这是Java和Android的我的第一个aplication所以我是一个真正的n00b如果答案是有,我不明白,所以我真的很AP preciate如果你能解释一下我应该做些什么来解决这个问题错误和原因。
I don't have a clue what to do, I've looked up in Google and I didn't find anything and I did the same at stackoverflow. This is my first aplication in Java and for Android so I'm a real n00b and if the answer was there, I didn't understand it so I would really appreciate if you could explain what I should do to fix this error and why.
感谢一切在前进。
推荐答案
在创建一个 ArrayAdapter 像你这样的,一个ArrayAdapter需要一个布局资源仅包含一个TextView。 试着改变你的ArrayAdapter与创造:
When creating an ArrayAdapter like yours, the ArrayAdapter needs a layout resource containing just a TextView. Try changing your ArrayAdapter creation with:
setListAdapter(新ArrayAdapter&LT;字符串&GT;(这一点,android.R.layout.simple_list_item_1,this.directoryEntries));
和看看是否可行。
相关推荐
最新文章