- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个 Android 应用程序,它将控制某些家用设备,例如灯泡。该应用程序的任务只是让设备在与我目前使用的 HC-05 型号的蓝牙模块连接时打开和关闭。
一切都已经正常了。它现在可以控制不同的设备。问题是,当我想改进 Controller 选项卡中应用程序的 UI(我在应用程序中使用 1 个选项卡)时,它出现错误。假设我将在 ListView 中放置 3 个开关控件,但是当我已经实现它时,我的 LogCat 中遇到了这些问题:
01-17 23:11:16.662: E/AndroidRuntime(4929): FATAL EXCEPTION: main
01-17 23:11:16.662: E/AndroidRuntime(4929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.javac101/com.example.javac101.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.javac101/com.example.javac101.ControllerTab}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.os.Handler.dispatchMessage(Handler.java:107)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.os.Looper.loop(Looper.java:194)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.ActivityThread.main(ActivityThread.java:5371)
01-17 23:11:16.662: E/AndroidRuntime(4929): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 23:11:16.662: E/AndroidRuntime(4929): at java.lang.reflect.Method.invoke(Method.java:525)
01-17 23:11:16.662: E/AndroidRuntime(4929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-17 23:11:16.662: E/AndroidRuntime(4929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-17 23:11:16.662: E/AndroidRuntime(4929): at dalvik.system.NativeStart.main(Native Method)
01-17 23:11:16.662: E/AndroidRuntime(4929): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.javac101/com.example.javac101.ControllerTab}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2186)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.widget.TabHost.addTab(TabHost.java:240)
01-17 23:11:16.662: E/AndroidRuntime(4929): at com.example.javac101.MainActivity.onCreate(MainActivity.java:142)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.Activity.performCreate(Activity.java:5122)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-17 23:11:16.662: E/AndroidRuntime(4929): ... 11 more
01-17 23:11:16.662: E/AndroidRuntime(4929): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.widget.AdapterView.addView(AdapterView.java:477)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.view.LayoutInflater.rInflate(LayoutInflater.java:750)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-17 23:11:16.662: E/AndroidRuntime(4929): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.Activity.setContentView(Activity.java:1881)
01-17 23:11:16.662: E/AndroidRuntime(4929): at com.example.javac101.ControllerTab.onCreate(ControllerTab.java:87)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.Activity.performCreate(Activity.java:5122)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
01-17 23:11:16.662: E/AndroidRuntime(4929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
我可能确定它在我的 Activity_main.xml 中,我的开关控件所在的位置出了问题。这些是我在此文件中的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/digitalClock1"
android:layout_marginBottom="15dp"
android:text="@string/switch1" />
<Switch
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/switch1"
android:layout_below="@+id/switch1"
android:layout_marginBottom="15dp"
android:text="@string/switch2" />
<Switch
android:id="@+id/switch3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/switch2"
android:layout_below="@+id/switch2"
android:layout_marginBottom="15dp"
android:text="@string/switch3" />
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:layout_marginBottom="5dp"
android:layout_alignParentRight="true"/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/switch3"
android:layout_below="@+id/switch3"
android:layout_marginTop="22dp" >
<RelativeLayout
android:id="@+id/groupSub"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Switch
android:id="@+id/switch4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:text="@string/switch1" />
<Switch
android:id="@+id/switch5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:text="@string/switch2" />
</RelativeLayout>
</ListView>
</RelativeLayout>
我还注意到我的 ControllerTab 类中的 java 文件存在问题,因为此错误出现在 LogCat 中:01-17 23:11:16.662: E/AndroidRuntime(4929): 在 com.example.javac101.ControllerTab.onCreate(ControllerTab.java:87
)但是当我检查这一行时,只有这个代码: setContentView(R.layout.activity_main);
对于这个项目的任何帮助将非常感激。你真的会为这件事提供很大的帮助。预先感谢那些愿意发表评论以寻求帮助的人。作为初学者我已经尽力了。
最佳答案
请从 <ListView>
中删除子元素在您的布局中,因为这不是您使用 ListView
的方式.
填充 ListView
,您创建一个 ListAdapter
,例如 ArrayAdapter
,并将该适配器连接到 ListView
:
/***
Copyright (c) 2008-2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
http://commonsware.com/Android
*/
package com.commonsware.android.list;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewDemo extends ListActivity {
private TextView selection;
private static final String[] items={"lorem", "ipsum", "dolor",
"sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel",
"ligula", "vitae", "arcu", "aliquet", "mollis",
"etiam", "vel", "erat", "placerat", "ante",
"porttitor", "sodales", "pellentesque", "augue", "purus"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
items));
selection=(TextView)findViewById(R.id.selection);
}
@Override
public void onListItemClick(ListView parent, View v, int position,
long id) {
selection.setText(items[position]);
}
}
(来自 this sample application )
关于java - ListView 中的切换控件获取 UnsupportedOperationException : addView(View, LayoutParams) 在 AdapterView 中不受支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21189208/
我试图在 2 个小部件/布局之间创建一个 float 操作按钮 我 99% 确定我已经使用了这个教程并且有效 How can I add the new "Floating Action Button
这是 XML 代码,
因为它可以与Relative 或Linear 布局一起使用。因此,如果我们稍后将 RelativeLayout 更改为 LinearLayout,它可能会很有用。 LinearLayout.Layou
我正在尝试开发 Android 3.1 平板电脑应用程序。 这个app会有很多图片,我已经关注了Processing Bitmaps Off the UI Thread教程,但我做错了,因为我得到:
我的初始代码行位于重写方法 onGlobalLayout() 中: img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 10
我正尝试按照发布在以下位置的示例编写周历: How can I create a weekly calendar view for an Android Honeycomb application?
这段代码似乎适用于 android api 16 - 22,但不适用于 api 23。我只是想显示一个带有选项的弹出窗口,并使弹出窗口下方的背景变暗: WindowPo
带有 Android Studio 2.0 的 OS X Yosemite 10.10.5 出于教学目的,我想将尽可能多的 View 属性从 XML 移动到 Java。 我在 GridLayout 中
说明及解决方法在底部。 我正在开发一个 slider 布局动画,动画效果很好,但是当所有进程结束时,它们会得到下一个异常。 我猜 RelativeLayout 父级在异常中有一些事情要做,但我不知道如
我正在尝试创建一个 FloatingActionButton,如 this , 但是当我运行应用程序时出现以下错误 java.lang.ClassCastException: android.wid
根据大量教程和示例项目,我得到了以下代码,它应该用包含来自 Line 对象的多个值的行填充 ListView。 作为一名新手程序员,我一辈子都弄不明白我的代码到底哪里出错了。任何帮助将不胜感激! 如果
我得到了这个异常(exception): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cas
Android Studio 3.1、Java 1.8、Gradle 4.1。我使用 minVersion = 15 我想在文件 profile_category_active.xml 中以编程方式设
我正在尝试使我的整个布局(其中包含 ListView)可滚动。也就是说,整个布局应该拥有滚动条,而不仅仅是 ListView。我正在尝试使用 commonsguy 的 MergeAdapter ( h
我遇到了以下错误 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to an
还有其他相关问题,但它们没有解决我的情况(它们的错误代码与客户端代码的某种回收和/或错误的转换调用有关)。 我的情况比较复杂。 我有一些代码,用户可以在其中调出照片库。 问题是,它在我拥有的 7 部手
我正在尝试做抽屉导航选项。 这是我的 xml: 如果我删除 Linearlayout
我正在尝试获取 AppBarLayout 的布局参数,但出现此错误。 java.lang.ClassCastException: android.support.design.widget.Coord
我使用一个具有RelativeLayout -> ScrollView -> LinearLayout 的 View 。我得到了异常(exception): 11-22 11:17:24.605: E
我在调试我正在处理的 Android 应用程序时遇到了问题。目前,它应该能够从文本文件中获取输入(它可以),将其转换为包含表示网格部分的 Cell 对象的 ArrayList(它确实如此),然后使用
我是一名优秀的程序员,十分优秀!