首先,我是Android编程的新手,而Stackoverflow对其他网站的帮助很大。但是对于我现在遇到的问题,我还没有找到解决方案。我正在创建的应用程序崩溃,出现以下错误:
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)
首先我查看了显而易见的内容,并检查了我在所有内容上使用layout_height
的所有布局文件,并且我确实在任何地方使用它。所以我研究了更多并发现了正在发生的事情。
在http://jgilfelt.github.io/android-actionbarstylegenerator/我创建了一个自定义样式并下载了zip文件,然后将其解压缩到我的项目中(顺便说一句,我使用的是Android-Studio)。在运行应用程序时,它可以很好地使用zip文件中的内容。
在布局中添加微调器并在其上设置ArrayAdapter
会导致我的应用程序因上面的堆栈跟踪而崩溃。在问题中使用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>
答案 0 :(得分:0)
您使用的是Eclipse吗?一旦我收到此错误,我就通过清理项目来解决它
答案 1 :(得分:0)
今天我一直在努力解决这个问题而且我成功了。现在我不得不承认,我不知道它为什么现在有效的细节和具体细节,但这就是我所做的。
在java文件onCreate
的{{1}}方法中,我更改了以下行:
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);
也许有人可以通过评论解释为什么这个改变能解决问题 至于现在,如果允许,我会接受我自己的答案。