片段在android studio中不起作用

时间:2017-04-27 10:53:52

标签: android android-studio

我在android的activity_main.xml中写了这段代码。但它给出了一个错误,它无法识别片段。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bignerdranch.android.stockapp.MainActivity"
    android:padding="16dp">

    <TextureView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello world"/>
    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_weight="1"/>
    <Button
        android:id="@+id/search_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="button"/>

</RelativeLayout>

是依赖问题吗?我怎么能解决它?我非常感谢你的建议!

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

错误

FATAL EXCEPTION: main
                                                 Process: com.bignerdranch.android.stockapp, PID: 1267
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.stockapp/com.bignerdranch.android.stockapp.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:136)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                     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:785)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                     at dalvik.system.NativeStart.main(Native Method)
                                                  Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)

1 个答案:

答案 0 :(得分:0)

将Fragment类添加到fragment标签的 name 属性。见下文

     <fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.myapp.fragments.MyFragment"
    />

希望这有效