为什么片段不起作用 [英] Why the Fragment doesn't work

查看:318
本文介绍了为什么片段不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙。怎么了?什么是错误的原因,我怎么能解决呢?感谢你的帮助!

 >了java.lang.RuntimeException:无法启动活动
> ComponentInfo {com.example.interviewhelpers / com.example.interviewhelpers.Dashboard}:
> android.view.InflateException:二进制XML文件行#192:错误
>充气类片段
android.view.InflateException:引起二进制XML文件行#192:错误充气类片段

这是我的code:Main类...

 公共类仪表盘扩展活动实现OnClickListener {    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_dashboard);

activity_dashboard ...有更多的元素和设计,而这:

 <片段
        机器人:ID =@ + ID / listFragment
        机器人:layout_width =150dip
        机器人:layout_height =match_parent
        机器人:名字=com.example.interviewhelpers.ClientDetailActivity>< /片断>

和...

 包com.example.interviewhelpers;    进口android.os.Bundle;
    进口android.support.v4.app.FragmentActivity;
    进口android.view.LayoutInflater;
    进口android.view.View;
    进口android.view.ViewGroup;
    进口android.widget.TextView;    公共类ClientDetailActivity扩展FragmentActivity {        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
            查看查看= inflater.inflate(R.layout.cliente_detailed,集装箱,FALSE);
            TextView中的TextView =(TextView中)view.findViewById(R.id.detailsText);
            返回视图。
        }    }


解决方案

(请尽量使用支持库,你正在做的)
第一:
1.应使用FragmentActivity无活动
2.应使用一个片段您将要使用的子组件。
3.在onCreateView的片段只是做了夸大
4.在片段使用onViewCreated找到TextView的。
样品:
//这是活动

 公共类MainActivity扩展FragmentActivity {    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    }
}

//这是layout.activity_main

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    <片段
        机器人:名字=com.example.fragmentapp.ChildFragment
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / textView1
        机器人:layout_alignParentTop =真/>    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:填充=@扪/ padding_medium
        机器人:文字=@字符串/参考hello world
        工具:上下文=。MainActivity/>< / RelativeLayout的>

//这是要显示的片段

 公共类ChildFragment扩展片段{    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        返回inflater.inflate(R.layout.child_frag,NULL);
    }    @覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);
        TextView的tvText =(TextView中)view.findViewById(R.id.textView1);
        tvText.setText(找到了!);
    }
}

//这是布局的片段layout.child_fragment

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=TextView的/>< / LinearLayout中>

Please help. What happens? What is the cause of error, and how could I solve it? Thanks for your help!

>  java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.interviewhelpers/com.example.interviewhelpers.Dashboard}:
> android.view.InflateException: Binary XML file line #192: Error
> inflating class fragment


Caused by: android.view.InflateException: Binary XML file line #192: Error inflating class fragment

This is my code: Main class...

public class Dashboard extends Activity implements OnClickListener {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);

activity_dashboard... Has more elements and layouts, and this:

<fragment
        android:id="@+id/listFragment"
        android:layout_width="150dip"
        android:layout_height="match_parent"
        android:name="com.example.interviewhelpers.ClientDetailActivity" ></fragment>

And...

 package com.example.interviewhelpers;

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    public class ClientDetailActivity extends FragmentActivity  {

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.cliente_detailed, container, false);
            TextView textView = (TextView) view.findViewById(R.id.detailsText);
            return view;
        }

    }

解决方案

(Always try to use the support libraries as you are doing) First: 1. Should use a FragmentActivity and NO Activity 2. Should use a Fragment to the child components you'll be using. 3. On the Fragments on the onCreateView just do the Inflate 4. On the Fragment use the onViewCreated to find the TextView. Sample: //This is the Activity

public class MainActivity extends FragmentActivity {

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

//This is the layout.activity_main

<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" >

    <fragment
        android:name="com.example.fragmentapp.ChildFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</RelativeLayout>

//This is the Fragment to be shown

public class ChildFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.child_frag, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView tvText = (TextView)view.findViewById(R.id.textView1);
        tvText.setText("Found!");
    }
}

//This is the layout for the fragment layout.child_fragment

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

这篇关于为什么片段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆