- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个简单的 Android 应用程序,但出现此错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twista.shopinglist/com.twista.shopinglist.ItemDetail}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
我尝试用谷歌搜索并通过此处的一些答案进行修复,但没有解决 :(。
我有一个带有 ListView
的 MainActivity
,在 onCreate()
中调用
public class MainActivity extends Activity {
....
@Override
protected void onCreate(Bundle savedInstanceState) {
...
final ListView listView = (ListView) findViewById(R.id.overListView);
List<Item> values = dataSource.getItems();
final ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(this,
android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item i = adapter.getItem(position);
Intent intent = new Intent(MainActivity.this, ItemDetail.class);
Bundle bundle = new Bundle();
bundle.putLong("my_id", i.getId());
intent.putExtras(bundle);
startActivity(intent);
}
});
这段代码应该开始新的 Activity ,ItemDetail
,其代码在这里:
public class ItemDetail extends Activity {
private ItemsDataSource dataSource;
private long itemId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
Bundle bundle = getIntent().getExtras();
itemId = bundle.getLong("my_id");
Intent intent = getIntent();
dataSource = new ItemsDataSource(this);
try {
dataSource.open();
} catch (SQLException e) {
e.printStackTrace();
}
ListView listView = (ListView) findViewById(R.id.detailListView);
List<SubItem> values = dataSource.getSubItemOfItem(itemId);
ArrayAdapter<SubItem> adapter = new ArrayAdapter<SubItem>(this,
android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
我仍然遇到这个错误:
09-10 22:30:30.555 10695-10695/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twista.shopinglist/com.twista.shopinglist.ItemDetail}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
at android.widget.AdapterView.addView(AdapterView.java:477)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:750)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:323)
at android.app.Activity.setContentView(Activity.java:1881)
at com.twista.shopinglist.ItemDetail.onCreate(ItemDetail.java:27)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
第二个布局文件(详细 Activity ):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/detailListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:id="@+id/groupSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/add_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:onClick="onClick"/>
<Button
android:id="@+id/delete_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete First"
android:onClick="onClick"/>
</LinearLayout>
</ListView>
非常感谢您的任何建议。
最佳答案
您的 XML 文件包含作为 ListView 的 subview 的 View ,您不能这样做。您的 XML 文件应如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:id="@+id/detailListView" />
<LinearLayout
android:id="@+id/groupSub"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/add_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:onClick="onClick"/>
<Button
android:id="@+id/delete_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete First"
android:onClick="onClick"/>
</LinearLayout>
</LinearLayout>
关于android - AdapterView 不支持 UnsupportedOperationException : addView(View, LayoutParams),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18728453/
我一直在开发搜索功能,该功能使我能够在 RecyclerView 中搜索数据。当我单击搜索图标时,我的应用程序崩溃了。经过一些调试后,我发现了问题,似乎无法清除信息或将信息添加到新列表。 Adapte
我已经创建了一个 ContainerRequestfilter 并成功触发了它。现在我想访问我之前在 grizzly HttpServerProbe 中设置的 UserPrincipal,如下所示:
我试图获取特定于区域设置的百分比符号。我的崩溃日志在尝试从 DecimalFormatSymbols 获取百分比时显示“UnsupportedOperationException”。 我使用给定区域设
我在下面编写了代码,我不知道出了什么问题,但 validDatesIterator.remove() 给了我 UnsupportedOperationException 异常。使用java 1.6 L
我正在尝试制作一个包含名称的列表。该列表应该是可修改的(添加、删除、排序等)。但是,每当我尝试更改 ArrayAdapter 中的项目时,程序都会崩溃,并出现 java.lang.Unsupporte
所以,我目前正在编写一个方法来迭代目录以及该目录内的所有目录: public static Set iterateDirectory(String dir){ Set children = n
我正在开发一个滑雪追踪器应用程序,但我在第一个任务上失败了:)秒表 这是我的服务: public class TrackerService extends Service { private
当运行以下代码时,我遇到一个异常,它简单地说:线程“LWJGL Application”中出现异常 java.lang.UnsupportedOperationException // Declare
这个问题已经有答案了: remove() on List created by Arrays.asList() throws UnsupportedOperationException (3 个回答)
String[] aArr = ...; String[] bArr = ...; List images = Arrays.asList(aArr); images.addA
我是 facebook sdk 的新手,所以我不太确定为什么会收到此错误。我已将其缩小到此部分: List permissions = session.getPermissions()
调用 add() 时出现 UnsupportedOperationException在以下代码中: Collection myClass = method(); MyClass newObject =
代码如下: private static Map> merge(Map> m1, Map> m2) { Map> mx = new HashMap>(); for (Entry> en
我有这个代码: public static String SelectRandomFromTemplate(String template,int count) { String[] split
所以其中一个方法的描述如下: public BasicLinkedList addToFront(T data) This operation is invalid for a sorted list
我正在使用 Grails 2.3.11 并使用 MongoDb 插件 mongodb:3.0.3。 我的代码是这样的: def home() { List abcs = ABC.list()
我正在尝试创建一个自定义 ListView 来保存自定义对象的 View ,在这种情况下,自定义对象将是 Data 类的实例 Java 代码如下: import android.app.ListAct
我不太明白在哪里可以抛出这个异常。 例如,我正在实现 Future接口(interface)并且不希望任何人调用该方法: Future#get(long, TimeUnit) . 那么,我可以直接扔掉
我有下面的示例代码: String[] patternArray = {"1","2","3"}; List patternCheck = Arrays.asList(patternArray); p
在研究 Collection API 时,我们发现某些方法(add、remove、...)可能会抛出 java.lang.UnsupportedOperationException 如果 Collec
我是一名优秀的程序员,十分优秀!