ListView 中的切换控件获取 UnsupportedOperationException: addView(View, LayoutParams) 在 AdapterView 中不受支持 [英] Switch Control in ListView gets UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

查看:36
本文介绍了ListView 中的切换控件获取 UnsupportedOperationException: addView(View, LayoutParams) 在 AdapterView 中不受支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 Android 应用程序,它将控制某些家用设备,例如灯泡.这个应用程序的任务只是让设备能够打开和关闭,只要它与我的蓝牙模块连接,我目前使用的是 HC-05 型号.

I'm creating an android application that will control certain domestic devices such as bulbs. The task of this application is simply to enable the device to turn on and turn off as long as it is connected with my bluetooth module which I'm currently using a HC-05 model.

一切正常.它现在可以控制不同的设备.问题是当我想在我的控制器选项卡中改进我的应用程序的 UI 时(我在应用程序中使用了 1 个选项卡),它有一个错误.假设我将在 ListView 中使用我的 3 个开关控件,但是当我已经实现它时,我在我的 LogCat 中遇到了这些问题:

Everything works fine already. It can now control different devices. The problem is when I want to improve the UI of my app in my controller tab (i'm using 1 tabs in the app) its having an error. Suppose to be I'll be having my 3 switch controls inside a ListView but when I implemented it already I'm having these trouble in my 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 中,我的开关控件所在的位置出错了.这些是我在这个文件中的代码:

I'm probably sure its in my activity_main.xml where my switch controls are located in which it went wrong. These are my codes in this file:

<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);

I noticed also that there's a problem with my java file in my class ControllerTab since this error appear in the LogCat: 01-17 23:11:16.662: E/AndroidRuntime(4929): at com.example.javac101.ControllerTab.onCreate(ControllerTab.java:87) But when I check this line its only this code: setContentView(R.layout.activity_main);

对这个项目的任何帮助都将不胜感激.你真的会对这个有很大的帮助.在此先感谢那些会发表评论以寻求帮助的人.作为初学者,我已经尽力了.

Any help will be so much appreciated in for this project. You will really be a big help for this one. Thank you in advance to those who will comment for some help. I already did my best as a beginner.

推荐答案

请从布局中的 中删除子元素,因为这不是您使用 ListView 的方式.

Please remove the child elements from <ListView> in your layout, as that is not how you use a ListView.

要填充 ListView,您需要创建一个 ListAdapter,例如 ArrayAdapter,然后将该适配器附加到 ListView:

To populate a ListView, you create a ListAdapter, such as an ArrayAdapter, and attach that adapter to the 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]);
  }
}

(来自这个示例应用程序)

这篇关于ListView 中的切换控件获取 UnsupportedOperationException: addView(View, LayoutParams) 在 AdapterView 中不受支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆