login950518 2016-01-31 13:50 采纳率: 80%
浏览 1660
已采纳

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条回答 默认 最新

  • 007筑梦者 2016-02-03 08:19
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数