片段不显示布局

时间:2014-12-22 10:06:56

标签: android android-layout android-fragments android-view

我有一个像这样的片段

public class ContactFragment extends Fragment {

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);

        }

        @Override
        public void onDetach() {
            super.onDetach();
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            inflater.inflate(R.layout.contacts_picker_activity, null);

            return super.onCreateView(inflater, container, savedInstanceState);
        }
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {

            super.onViewCreated(view, savedInstanceState);
        }
    }

contacts_picker_activity 布局:

<?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" >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="this a test "
            android:id="@+id/textView" android:layout_gravity="center"/>
</LinearLayout>

问题是当我运行我的应用程序时, onCreateView 方法运行但没有显示布局。
我的logcat没有任何错误。

3 个答案:

答案 0 :(得分:2)

试试这种方式

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

您应该返回虚增的View

答案 1 :(得分:1)

变化

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

            return super.onCreateView(inflater, container, savedInstanceState);
  }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {


          return inflater.inflate(R.layout.contacts_picker_activity, container , false);
 }

您需要返回想要在onCreateView

中充气的视图

答案 2 :(得分:0)

只需返回膨胀的视图。

public class ContactFragment extends Fragment {

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);

        }

        @Override
        public void onDetach() {
            super.onDetach();
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
           View v= inflater.inflate(R.layout.contacts_picker_activity, null);

            return v;
        }
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {

            super.onViewCreated(view, savedInstanceState);
        }
    }