如何制作片段?

时间:2016-04-16 13:23:24

标签: java android android-layout android-fragments android-studio

我之前从未制作过片段,并且一直试图让它发挥作用。我遵循了一些在线教程,但那些并没有太多收获。

我的问题是这些方法无序调用。当MyActivity的XML文件被加载时,它还会加载片段并调用其onCreate()方法。这是在我调用片段的newInstance()方法之前完成的,因此onCreate()没有它需要工作的参数。

将参数传递给片段的正确方法是什么?因为显然这不起作用。我试图将代码简化为完全相关的内容。

如果你想要它,这是我得到的具体错误:

android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class fragment

第18行指向片段标记的第一行。

MyFragment.java

public class MyFragment extends Fragment {
        private static final String ARG_PARAM1 = "param";
        private int param

        public static MyFragment newInstance(int param) {
             MyFragment fragment = new MyFragment();
             Bundle args = new Bundle();
             args.putInt(ARG_PARAM1, param);
             fragment.setArguments(args);
             return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             this.param = getArguments().getInt(ARG_PARAM1);
             // do stuff with param, but obviously param is not set
             // since constructor is called.
       }

       public interface OnFragmentInteractionListener {
            void onFragmentInteraction(String username);
       }

}

MyActivity.java

public class MyActivity extends AppCompatActivity implements MyFragment.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_follower_list); // crashes at this line

        Fragment newFragment = ListOfUsersFragment.newInstance(TypeOfUserList.FOLLOWING, "TestingUsername");
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.my_fragment, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void onFragmentInteraction(String username) {
        // TODO (I'm just trying to get it to run first....
    }
}

activity_my.xml

<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"
    tools:context=".activities.MyActivity">

    <!-- The following fragment's onCreate() method is called before newInstance() is called, causing issues. -->
    <fragment
        class ="xyz.mydomain.myproject.fragments.MyFragment"
        android:id="@+id/my_fragment"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"/>

</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

使用getSupportFragmentManager()而不是getFragmentManager():

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_follower_list); // crashes at this line

    Fragment newFragment = ListOfUsersFragment.newInstance(TypeOfUserList.FOLLOWING, "TestingUsername");
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

同样在你的Fragment类中,请确保它继承自Android框架支持片段:

import android.support.v4.app.Fragment;

public class MyFragment extents Fragment {
    private static final String ARG_PARAM1 = "param";
    private int param

    public static MyFragment newInstance(int param) {
         MyFragment fragment = new MyFragment();
         Bundle args = new Bundle();
         args.putInt(ARG_PARAM1, param);
         fragment.setArguments(args);
         return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         this.param = getArguments().getInt(ARG_PARAM1);
         // do stuff with param, but obviously param is not set
         // since constructor is called.
   }

   public interface OnFragmentInteractionListener {
        void onFragmentInteraction(String username);
   }

}

也可以使用这种布局:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MyActivity">
</RelativeLayout>

答案 1 :(得分:0)

在res文件夹中创建xml

<FrameLayout 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"
    tools:context="com.example.androiddeveloper.demoapp.MyFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

这里的java代码。

import android.support.v4.app.Fragment;

public class MyFragment extents Fragment {
    private static final String ARG_PARAM1 = "param";
    private int param

    public static MyFragment newInstance(int param) {
         MyFragment fragment = new MyFragment();
         Bundle args = new Bundle();
         args.putInt(ARG_PARAM1, param);
         fragment.setArguments(args);
         return fragment;
    }
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         this.param = getArguments().getInt(ARG_PARAM1);
         // do stuff with param, but obviously param is not set
         // since constructor is called.
   }

   public interface OnFragmentInteractionListener {
        void onFragmentInteraction(String username);
   }

}

你的活动的xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MyActivity">
</RelativeLayout>

调用YourActivity

public class MyActivity extends AppCompatActivity implements MyFragment.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_follower_list); // crashes at this line

        Fragment newFragment = ListOfUsersFragment.newInstance(TypeOfUserList.FOLLOWING, "TestingUsername");
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.my_fragment, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void onFragmentInteraction(String username) {
        // TODO (I'm just trying to get it to run first....
    }
}

答案 2 :(得分:0)

可能是分号,你没有添加半结肠。

而不是private int param应为private int param;

答案 3 :(得分:0)

使用

defaults.yaml

而不是

getSupportFragmentManager().beginTransaction();