Android:无法扩充类片段

时间:2016-08-23 17:56:38

标签: android android-fragments

我试图了解片段是如何工作的,但到目前为止我还没有成功。 这只是一个没有用的例子。  当我尝试将一个片段添加到布局时,我总是得到相同的错误。 我不知道缺少什么

片段类

<div class="view-content">
  <div class="views-row views-row-1"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-2"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-3"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-4"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-5"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-6"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-7"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-8"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-9"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-10"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-11"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-12"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-13"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-14"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
  <div class="views-row views-row-15"><a href="#"><img src="http://www.example.com/image.jpg /><a/></div>
</div>

ActivityClass

  package com.app.danieltavares.teste;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class TestFragment extends Fragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.testefrag,container,false);

    }
}

布局片段

package com.app.danieltavares.teste;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

使用片段的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="diusdb"/>

</LinearLayout>

logcat的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.app.danieltavares.teste.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.app.danieltavares.teste.TestFragment"
        tools:layout="@layout/testefrag"></fragment>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

Caused by: java.lang.IllegalArgumentException: Binary XML file line #16: Must specify unique android:id, android:tag, or have a parent with an id for com.app.danieltavares.teste.TestFragment

android:id属性添加到布局文件中的<fragment>元素。