片段为什么不起作用

时间:2012-08-07 01:59:07

标签: android android-fragments fragment android-fragmentactivity

请帮忙。怎么了?错误的原因是什么,我该如何解决?谢谢你的帮助!

>  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

这是我的代码:主要类......

public class Dashboard extends Activity implements OnClickListener {

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

activity_dashboard ...有更多元素和布局,以及:

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

和...

 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;
        }

    }

1 个答案:

答案 0 :(得分:2)

(总是试着像你一样使用支持库) 第一: 1.应该使用FragmentActivity和NO Activity 2.应该将片段用于您将使用的子组件。 3.在onCreateView上的片段上,只需执行Inflate 4.在Fragment上使用onViewCreated来查找TextView。 样品: //这是活动

public class MainActivity extends FragmentActivity {

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

//这是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>

//这是要显示的片段

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!");
    }
}

//这是片段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>