itsJane
itsJane

Reputation: 15

Switch Control in ListView gets UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

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.

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)

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>

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.

Upvotes: 0

Views: 218

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007494

Please remove the child elements from <ListView> in your layout, as that is not how you use a 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]);
  }
}

(from this sample application)

Upvotes: 1

Related Questions