问题描述
- Android自定义控件时遇到的奇怪bug:
-
《第一行代码——Android》第三章 3.4 自定义控件 问题
示例代码需要新建一个布局:title.xml
我进行了如下操作:
(1)
(2)
title.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/title_bg" android:orientation="horizontal"> <Button android:id="@+id/title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:background="@drawable/back_bg" android:text="Back" android:textColor="#000" /> <TextView android:id="@+id/title_text" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="Title Text" android:textColor="#fff" android:textSize="24sp" /> <Button android:id="@+id/title_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:background="@drawable/edit_bg" android:text="Edit" android:textColor="#000" /> </LinearLayout>
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/title"/> </LinearLayout>
MainActivity.java:
package com.test.lichee.uicustomviews; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Window; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); } }
运行后出现如下报错:
02-02 12:01:38.180 6186-6186/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.test.lichee.uicustomviews, PID: 6186 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.lichee.uicustomviews/com.test.lichee.uicustomviews.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2371) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2423) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5332) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641) at dalvik.system.NativeStart.main(Native Method) Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:292) at android.app.Activity.requestWindowFeature(Activity.java:3408) at com.test.lichee.uicustomviews.MainActivity.onCreate(MainActivity.java:12) at android.app.Activity.performCreate(Activity.java:5369) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1096) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2423)? at android.app.ActivityThread.access$800(ActivityThread.java:155)? at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)? at android.os.Handler.dispatchMessage(Handler.java:110)? at android.os.Looper.loop(Looper.java:193)? at android.app.ActivityThread.main(ActivityThread.java:5332)? at java.lang.reflect.Method.invokeNative(Native Method)? at java.lang.reflect.Method.invoke(Method.java:515)? at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)? at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)? at dalvik.system.NativeStart.main(Native Method)? 可是可以看见,我的MainActivity.java中并没有错,出现了Error信息与实际情况不符合的现象。很奇怪,我认为应该是我创建title时出了问题。
我的引入的图片如下:
解决方案
activity 和 v7包下的AppCompatActivity 去掉title是有区别的!楼主将AppCompatActivity改为Activity或者在AndroidManifest.xml中加个theme
解决方案二:
AndroidRuntimeException: requestFeature() must be called before adding content 报错的这句话意思就是requestFeature()方法要在你oncreat的setcontentview之前调用。看你贴的代码并没有问题啊。代码跟错误日志没有对应起来啊
解决方案三:
你的代码跟自定义控件什么关系???
解决方案四:
你的代码跟自定义控件什么关系???
解决方案五:
http://www.cnblogs.com/kissazi2/archive/2012/10/04/2711479.html
解决方案六:
将AppCompatActivity改为Activity
解决方案七:
代码顺序问题,改变一下setcontent和request的顺序
时间: 2024-10-31 04:58:58