gpt4 book ai didi

java - ListView 中的切换控件获取 UnsupportedOperationException : addView(View, LayoutParams) 在 AdapterView 中不受支持

转载 作者:行者123 更新时间:2023-12-01 13:37:06 24 4
gpt4 key购买 nike

我正在创建一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com