Spinner Causes Error Using Custom Style

First off, I'm pretty new to Android programming, and Stackoverflow really helped other sites. However, for the problem that I encountered, I have not yet found a solution. The application I am creating causes an error with the following error:

AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Binary XML file line #19: You must supply a layout_height attribute.
        at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
        at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5319)
        at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:5271)
        at android.view.ViewGroup.generateLayoutParams(ViewGroup.java:4471)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:477)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
        at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
        at android.widget.Spinner.onMeasure(Spinner.java:296)
        at android.view.View.measure(View.java:12751)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1017)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
        at android.view.View.measure(View.java:12751)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
        at android.view.View.measure(View.java:12751)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
        at android.view.View.measure(View.java:12751)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
        at android.view.View.measure(View.java:12751)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
        at android.view.View.measure(View.java:12751)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
        at android.view.View.measure(View.java:12751)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
        at android.view.View.measure(View.java:12751)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
        at android.view.View.measure(View.java:12751)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2120)
        at android.view.View.measure(View.java:12751)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1106)
        at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2558)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4697)
        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:787)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
        at dalvik.system.NativeStart.main(Native Method)

First, I checked the obvious and checked all of my layout files that I used layout_height, and I use it everywhere. So I researched more and found out what was going on.

On http://jgilfelt.imtqy.com/android-actionbarstylegenerator/ I created my own style and uploaded the zip file and I unpacked it into my project (by the way, I use Android-Studio). When you start the application, the styles blend perfectly with the contents of the zip file.

ArrayAdapter stacktrace . setAdapter , , , . , . , , .

, , - , , . . . .

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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.testspinnertheme.MainActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

java MainActivity.java

package com.example.testspinnertheme;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] strings = new String[]{"one", "two", "three", "four"};
        ArrayAdapter<String> aaString = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, strings);
        aaString.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        spinnerDistance.setAdapter(aaString);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testspinnertheme" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Test" >
        <activity
            android:name="com.example.testspinnertheme.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

(styles_test.xml) -

<?xml version="1.0" encoding="utf-8"?>
<!-- File created by the Android Action Bar Style Generator

 Copyright (C) 2011 The Android Open Source Project
 Copyright (C) 2012 readyState Software Ltd

 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.
-->

<resources>

    <style name="Theme.Test" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarItemBackground">@drawable/selectable_background_test</item>
        <item name="android:popupMenuStyle">@style/PopupMenu.Test</item>
        <item name="android:dropDownListViewStyle">@style/DropDownListView.Test</item>
        <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Test</item>
        <item name="android:actionDropDownStyle">@style/DropDownNav.Test</item>
        <item name="android:actionBarStyle">@style/ActionBar.Solid.Test</item>
        <item name="android:actionModeBackground">@drawable/cab_background_top_test</item>
        <item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_test</item>
        <item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Test</item>
    </style>

    <style name="ActionBar.Solid.Test" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
        <item name="android:background">@drawable/ab_solid_test</item>
        <item name="android:backgroundStacked">@drawable/ab_stacked_solid_test</item>
        <item name="android:backgroundSplit">@drawable/ab_bottom_solid_test</item>
        <item name="android:progressBarStyle">@style/ProgressBar.Test</item>
    </style>

    <style name="ActionBar.Transparent.Test" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@drawable/ab_transparent_test</item>
        <item name="android:progressBarStyle">@style/ProgressBar.Test</item>
    </style>

    <style name="PopupMenu.Test" parent="@android:style/Widget.Holo.Light.ListPopupWindow">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_test</item>  
    </style>

    <style name="DropDownListView.Test" parent="@android:style/Widget.Holo.Light.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_test</item>
    </style>

    <style name="ActionBarTabStyle.Test" parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_test</item>
    </style>

    <style name="DropDownNav.Test" parent="@android:style/Widget.Holo.Light.Spinner">
        <item name="android:background">@drawable/spinner_background_ab_test</item>
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_test</item>
        <item name="android:dropDownSelector">@drawable/selectable_background_test</item>
    </style>

    <style name="ProgressBar.Test" parent="@android:style/Widget.Holo.Light.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_test</item>
    </style>

    <style name="ActionButton.CloseMode.Test" parent="@android:style/Widget.Holo.Light.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_test</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Test.Widget" parent="@android:style/Theme.Holo">
        <item name="android:popupMenuStyle">@style/PopupMenu.Test</item>
        <item name="android:dropDownListViewStyle">@style/DropDownListView.Test</item>
    </style>

</resources>
+4
2

- , . , , , .

onCreate java MainActivity.java :

ArrayAdapter<String> aaString = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, strings);
aaString.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);

:

ArrayAdapter<String> aaString = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, strings);
aaString.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

- , , .
, .

0

Eclipse? , ,

0

All Articles