login950518
login950518
2016-01-31 13:50
采纳率: 80%
浏览 1.7k

Android自定义控件时遇到的奇怪bug:

《第一行代码——Android》第三章 3.4 自定义控件 问题
示例代码需要新建一个布局:title.xml
我进行了如下操作:
(1)
图片说明
(2)
<br>
图片说明

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时出了问题。


我的引入的图片如下:

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • Acmen99
    AgBaoの未来 2016-02-03 08:19
    已采纳

    activity 和 v7包下的AppCompatActivity 去掉title是有区别的!楼主将AppCompatActivity改为Activity或者在AndroidManifest.xml中加个theme

    点赞 评论
  • crazy1235
    月盡天明 2016-01-31 14:00

    你的代码跟自定义控件什么关系???

    点赞 评论
  • crazy1235
    月盡天明 2016-01-31 14:00

    你的代码跟自定义控件什么关系???

    点赞 评论
  • sinat_30616713
    sinat_30616713 2016-02-01 02:54

    AndroidRuntimeException: requestFeature() must be called before adding content 报错的这句话意思就是requestFeature()方法要在你oncreat的setcontentview之前调用。看你贴的代码并没有问题啊。代码跟错误日志没有对应起来啊

    点赞 评论
  • Theresa_zxx
    ProXin 2016-02-01 06:26
    点赞 评论
  • huaheshangxo
    苍蝇小黑 2016-02-02 07:24

    将AppCompatActivity改为Activity

    点赞 评论
  • zrf1335348191

    代码顺序问题,改变一下setcontent和request的顺序

    点赞 评论

相关推荐