logcat错误:“ AddView(查看,LayoutParams)在AdapterView中不支持” listView中
-
14-10-2019 - |
题
我正在为Android做一个应用程序,我需要的是,它显示了SD卡中所有文件和目录的列表,并且必须能够通过不同的目录进行移动。我发现 Anddev的好教程. 。我修改了一些内容,因此应用程序在SD卡中移动,而不是在Android root Directories中移动,但其余的大部分是相同的。
这是我的XML文件:
<?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>
这是活动的代码:
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时,我会看到以下内容:
01-01 23:30:29.858:错误/Androidruntime(14911):致命例外:主要 01-01 23:30:29.858:错误/androidruntime(14911):java.lang.unsupportedoperationException:addview(view,layoutparams)不支持AdapterView
我不知道该怎么办,我在Google中抬头看,我什么也没找到,我在Stackoverflow上也做了同样的事情。这是我在Java和Android中的第一次放置,所以我是一个真正的N00B,如果答案在那里,我不理解,所以我真的很感激您是否可以解释我该怎么办来解决此错误以及为什么。
感谢您提前的一切。
解决方案
创建一个 ArrayAdapter 像您一样,ArrayAdapter需要布局资源 仅包含一个文本视图。尝试使用以下方式更改阵列adapter的创建
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
this.directoryEntries));
看看它是否有效。
其他提示
对于那些有这个问题并在布局中夸大的人来说 ArrayAdapter
' getView
, ,设置 parent
参数为 null
, ,如 view = inflater.inflate(R.layout.mylayout, null);
经验法则
您应该始终尝试在充气视图时传递父,否则该框架将无法确定用于该膨胀视图的布局参数。
这 正确的方式 处理例外是通过 错误的 第三 范围, ,此标志指示是否应直接添加到传递的容器中的视图。
对于适配器,在您返回getView方法的视图之后,适配器本身将添加视图:
view = flater.inflate(r.layout.mylayout,parent, 错误的);
Baseadapter 样本来自 仅仅是US Devbytes:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View result = convertView;
if (result == null) {
result = mInflater.inflate(R.layout.grid_item, parent, false);
}
// Try to get view cache or create a new one if needed
ViewCache viewCache = (ViewCache) result.getTag();
if (viewCache == null) {
viewCache = new ViewCache(result);
result.setTag(viewCache);
}
// Fetch item
Coupon coupon = getItem(position);
// Bind the data
viewCache.mTitleView.setText(coupon.mTitle);
viewCache.mSubtitleView.setText(coupon.mSubtitle);
viewCache.mImageView.setImageURI(coupon.mImageUri);
return result;
}
Cursoradapter 样本:
@Override
public View newView(final Context context, final Cursor cursor, final ViewGroup root) {
return mInflater.inflate(R.layout.list_item_ticket, root, false);
}
有关查看通货膨胀的更多信息,请参见此处 文章.
从XML文件中的ListView删除TextView
我的问题是:我在ArrayAdapter中覆盖GetView。然后,当我夸大视图时,我忘了设置 固定在false上 这导致了这个例外。
这就是必须完成的方式:
convertView = LayoutInflater.from(getContext()).inflate(R.layout.market_search_product_view, parent,false);
在解决这个问题之后!