无法将数据从活动传递到片段

时间:2016-03-15 15:15:43

标签: android android-fragments

我首先要说的是我是Android编程的新手,在阅读了无数的帖子后,我仍然无法确定我在尝试时做错了什么。将一些参数从Fragment传递给Activity,任何帮助都会非常有用。

我的活动课程:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static final MasterFragment newInstance(String numberOfItems) {
        MasterFragment masterFragment = new MasterFragment();
        Bundle bundle = new Bundle();
        bundle.putString("numberOfItems", numberOfItems);
        masterFragment.setArguments(bundle);
        return masterFragment;
    }

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

        if (savedInstanceState != null) {
            MasterFragment masterFragment = new MasterFragment();
            masterFragment.setArguments(getIntent().getExtras());
        }

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /*getting some buttons here

        */

        //not sure if relevant, but just in case I'll add this here too
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.drawable.ic_menu_white_24dp);

        if (toolbar != null) {
            toolbar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SlidingPaneLayout mSlidingLayout;
                    mSlidingLayout = (SlidingPaneLayout) findViewById(R.id.mainSlidingPaneLayout);

                    if (mSlidingLayout.isOpen()) {
                        mSlidingLayout.closePane();
                        getSupportActionBar().setIcon(R.drawable.ic_menu_white_24dp);
                    } else {
                        mSlidingLayout.openPane();
                        getSupportActionBar().setIcon(R.drawable.ic_arrow_back_white_24dp);
                    }
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getApplicationContext(), ProductsScrollingActivity
                .class);

        ArrayList<String> products = new ArrayList<String>();
        switch (v.getId()) {
            case R.id.baby: {
                //getting the products ArrayList here
                break;
            }
        }
        intent.putExtra("products", products);
        startActivity(intent);
    }
}

My Fragment课程:

public class MasterFragment extends ListFragment {

    String noOfItems;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_master, container);
        noOfItems = getArguments().getString("numberOfItems", "0");
        setListAdapter(new MenuListAdapter(R.layout.row_menu_action_item, getActivity(), MenuActionItem.values(), noOfItems));
        return view;
    }
}

我的content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    android:background="#ffffff"
    android:orientation="horizontal"
    android:id="@+id/mainSlidingPaneLayout"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.MainActivity"
    tools:showIn="@layout/activity_main">

    <!--Master fragment-->
    <fragment
        android:id="@+id/fragment_master"
        android:name="com.example.MasterFragment"
        android:layout_width="@dimen/mini_drawer_fragment_width"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_master">
    </fragment>

    <!-- A ScrollView goes here with some other stuff-->
</android.support.v4.widget.SlidingPaneLayout>

堆栈追踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: android.view.InflateException: Binary XML file line #25: Binary XML file line #20: Error inflating class fragment
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                Caused by: android.view.InflateException: Binary XML file line #25: Binary XML file line #20: Error inflating class fragment
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                                   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                                                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                                   at com.example.MainActivity.onCreate(MainActivity.java:59)
                                                                                   at android.app.Activity.performCreate(Activity.java:6237)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:148) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                                Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class fragment
                                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                   at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
                                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
                                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                                   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                                                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                                   at com.example.MainActivity.onCreate(MainActivity.java:59) 
                                                                                   at android.app.Activity.performCreate(Activity.java:6237) 
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:148) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String, java.lang.String)' on a null object reference
                                                                                   at com.example.MasterFragment.onCreateView(MasterFragment.java:26)
                                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1226)
                                                                                   at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1328)
                                                                                   at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2284)
                                                                                   at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
                                                                                   at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
                                                                                   at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                                                   at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
                                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                                   at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971) 
                                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:831) 
                                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                                   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                                                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                                   at com.example.MainActivity.onCreate(MainActivity.java:59) 
                                                                                   at android.app.Activity.performCreate(Activity.java:6237) 
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:148) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:1)

当您在XML中定义片段时,您无法在片段膨胀之前在片段上设置参数 - 因此在onViewCreated中的片段代码中,getArguments()为null,这会导致此崩溃。如果要为片段添加参数,请将XML中的<fragment>标记替换为FrameLayout,在活动中定义片段(正如您已经做的那样),并使用FragmentManager将其添加到FrameLayout:

getSupportFragmentManager().beginTransaction()
    .add(R.id.fragment_container, /*id of your frame layout*/
         fragment /*instance of the fragment created in your activity*/)
    .commit();