Cosmin
Cosmin

Reputation: 2394

Unable to pass data from activity to a fragment

I'll start by saying that I'm new to Android programming, and after reading countless posts around here, I'm still unable to determine what I'm doing wrong when trying to pass some arguments from a Fragment to an Activity and any help would be extremely helpful.

My Activity class:

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 class:

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

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

Stack trace:

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) 

Upvotes: 2

Views: 728

Answers (1)

Eric Brynsvold
Eric Brynsvold

Reputation: 3090

When you define a fragment in XML, you are not able to set the arguments on the fragment before the fragment is inflated - so in your fragment's code in onViewCreated, getArguments() is null, which causes this crash. If you want to add arguments to your fragment, replace the <fragment> tag in XML with a FrameLayout, define the fragment in your activity (as you are doing already) and add it to the FrameLayout with the FragmentManager:

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

Upvotes: 1

Related Questions