程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> java-Android自定義控件時遇到的奇怪bug:

java-Android自定義控件時遇到的奇怪bug:

編輯:編程解疑
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時出了問題。


我的引入的圖片如下:

圖片說明

最佳回答:


activity 和 v7包下的AppCompatActivity 去掉title是有區別的!樓主將AppCompatActivity改為Activity或者在AndroidManifest.xml中加個theme

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved