0

Good day guys, I have a program that passes the value of the value of variable from activity class to fragment, i did too many resarch like this one Send data from activity to fragment in android but it always gives me an error, I dont know why,

here's my activity CompanyPageActivity.java

private SectionsPagerAdapter mSectionsPagerAdapter;

String companyName, companyDescripiton, lat, lng, contact1, contact2, contact3, contact4, email, tag1, tag2, tag3, tag4,tag5,website;

/**
 * The {@link ViewPager} that will host the section contents.
 */
private ViewPager mViewPager;

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


    Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("BitmapImage");

    companyName = getIntent().getExtras().getString("CompanyName");
    companyDescripiton = getIntent().getExtras().getString("Description");
    lat  = getIntent().getExtras().getString("latitude");
    lng = getIntent().getExtras().getString("longtitude");
    contact1 = getIntent().getExtras().getString("contact1");
    contact2 = getIntent().getExtras().getString("contact2");
    contact3 = getIntent().getExtras().getString("contact3");
    contact4 = getIntent().getExtras().getString("contact4");
    email = getIntent().getExtras().getString("email");
    tag1 = getIntent().getExtras().getString("tag1");
    tag2 = getIntent().getExtras().getString("tag2");
    tag3 = getIntent().getExtras().getString("tag3");
    tag4 = getIntent().getExtras().getString("tag4");
    tag5 = getIntent().getExtras().getString("tag5");
    website = getIntent().getExtras().getString("website");

    Bundle bundleTitle = new Bundle();
    bundleTitle.putString("CompanyName", companyName);
    bundleTitle.putString("Description", companyDescripiton);
    bundleTitle.putString("contact1",contact1);
    bundleTitle.putString("contact2",contact2);
    bundleTitle.putString("contact3",contact3);
    bundleTitle.putString("contact4",contact4);
    bundleTitle.putString("tag1",tag1);
    bundleTitle.putString("tag2",tag2);
    bundleTitle.putString("tag3",tag3);
    bundleTitle.putString("tag4",tag4);
    bundleTitle.putString("tag5",tag5);
    bundleTitle.putString("website",website);
    bundleTitle.putString("email",email);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    bundleTitle.putByteArray("logo", byteArray);

    TabTitle tabTitle = new TabTitle();
    tabTitle.setArguments(bundleTitle);

    Bundle bundleLocation = new Bundle();
    bundleLocation.putString("lat", lat);
    bundleLocation.putString("lng", lng);
    bundleLocation.putString("companyName", companyName);

    TabLocation tabLocation = new TabLocation();
    tabLocation.setArguments(bundleLocation);


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

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

And here is my Fragment

ImageView logo;
TextView companyNametxt, companyDescripitontxt, contacts, emailtxt, tags, websitetxt;

String companyName, companyDescripiton, contact1, contact2, contact3, contact4, email, tag1, tag2, tag3, tag4,tag5,website;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.company_title, container, false);

    companyNametxt = (TextView)rootView.findViewById(R.id.companyName);
    companyDescripitontxt = (TextView)rootView.findViewById(R.id.companyDescription);
    contacts = (TextView)rootView.findViewById(R.id.contact);
    emailtxt = (TextView)rootView.findViewById(R.id.email);
    tags = (TextView)rootView.findViewById(R.id.tag);
    websitetxt = (TextView)rootView.findViewById(R.id.website);
    logo = (ImageView) rootView.findViewById(R.id.logo);

    companyName = getArguments().getString("CompanyName"); //line number 42
    companyDescripiton = getArguments().getString("Description");
    contact1 = getArguments().getString("contact1");
    contact2 = getArguments().getString("contact2");
    contact3 = getArguments().getString("contact3");
    contact4 = getArguments().getString("contact4");
    email = getArguments().getString("email");
    tag1 = getArguments().getString("tag1");
    tag2 = getArguments().getString("tag2");
    tag3 = getArguments().getString("tag3");
    tag4 = getArguments().getString("tag4");
    tag5 = getArguments().getString("tag5");
    website = getArguments().getString("website");

    byte[] byteArray  = getArguments().getByteArray("image");
    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    companyNametxt.setText(companyName);
    companyDescripitontxt.setText(companyDescripiton);
    contacts.setText("Contact number(s): \n"+contact1+"\t"+contact2+"\n"+contact3+"\t"+contact4);
    tags.setText("Tags: "+tag1+" "+tag2+" "+tag3+" "+tag4+" "+tag5);
    websitetxt.setText(website);
    emailtxt.setText(email);

    logo.setImageBitmap(bitmap);

    return rootView;
}

But it always gives me this error

java.lang.NullPointerException
                                                                          at com.gawapa.servicefinder.pageTabs.TabTitle.onCreateView(TabTitle.java:42)
                                                                          at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                                                                          at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                                                                          at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
                                                                          at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
                                                                          at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                          at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
                                                                          at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
                                                                          at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
                                                                          at android.view.View.measure(View.java:16677)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
                                                                          at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:700)
                                                                          at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                                                                          at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1364)
                                                                          at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:765)
                                                                          at android.view.View.measure(View.java:16677)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
                                                                          at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                          at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                          at android.view.View.measure(View.java:16677)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
                                                                          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                                                          at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                                                          at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                                                          at android.view.View.measure(View.java:16677)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
                                                                          at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                          at android.view.View.measure(View.java:16677)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
                                                                          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                                                          at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                                                          at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                                                          at android.view.View.measure(View.java:16677)
                                                                          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
                                                                          at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                          at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2421)
                                                                          at android.view.View.measure(View.java:16677)
                                                                          at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1927)
                                                                          at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1119)
                                                                          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1301)
                                                                          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1006)
                                                                          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5652)
                                                                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                                          at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                                          at android.view.Choreographer.doFrame(Choreographer.java:544)
                                                                          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                                          at android.os.Handler.handleCallback(Handler.java:733)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:136)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5433)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)

help me please T_T I'm stuck in here. I already tried this code`

companyName = this.getArguments().getString("CompanyName");

and this one

 Bundle bundle = this.getArguments();

    companyName = bundle.getString("CompanyName");

but it always gives me a same error again and again

This is my SectionsPagerAdapter class

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                TabTitle tabTitle = new TabTitle();
                return tabTitle;
            case 1:
                TabPosts tabPosts = new TabPosts();
                return tabPosts;
            case 2:
                TabLocation tabLocation = new TabLocation();
                return tabLocation;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "ABOUT";
            case 1:
                return "POSTS";
            case 2:
                return "LOCATION";
        }
        return null;
    }
}
Dylan
  • 1,121
  • 1
  • 13
  • 28
  • i ran into the same problem I broadcast data from my first tab to second tab and activity then use data from the activity – phpdroid Sep 20 '17 at 16:33
  • see [this](https://stackoverflow.com/questions/27247065/null-pointer-exception-passing-data-from-activity-to-fragment) page i hope it help you . – White Druid Sep 20 '17 at 16:35
  • 1) You're not using the bundle that contains the fields you're looking for 2) The instance of `TabLocation` that you create is not the one that's being used in the ViewPager. Post your adapter code (SectionsPagerAdapter) – Daniel Nugent Sep 20 '17 at 18:47
  • @DanielNugent done sir, can you help me how to fix that ? I'm just begr at android programming – Dylan Sep 20 '17 at 19:24

1 Answers1

0

As @daniel-nugent says, you need to pass the bundle to the Fragment created by the ViewPager adapter. The error happens because you're creating another fragment and passing the bundle outside the adapter with:

Bundle bundleTitle = new Bundle();
...
TabTitle tabTitle = new TabTitle();
tabTitle.setArguments(bundleTitle);

Bundle bundleLocation = new Bundle();
...
TabLocation tabLocation = new TabLocation();
tabLocation.setArguments(bundleLocation);

But in the adapter, you didn't pass any bundle (please read the comment):

public class SectionsPagerAdapter extends FragmentPagerAdapter {
  ...
  @Override
  public Fragment getItem(int position) {
    switch (position) {
      case 0:
        TabTitle tabTitle = new TabTitle();
        // Where is my Arguments?
        return tabTitle;
      case 1:
        TabPosts tabPosts = new TabPosts();
        return tabPosts;
      case 2:
        TabLocation tabLocation = new TabLocation();
        // Please, give me the arguments!
        return tabLocation;
      default:
        return null;
    }
  }
  ...
}

So, you need to create and set the arguments to the adapter. You can pass the Bundle via SectionsPagerAdapter constructor. So, create a constructor with the Bundle parameters:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

  private Bundle bundleTitle;
  private Bundle bundleLocation;

    public SectionsPagerAdapter(FragmentManager fm, Bundle bundleTitle, Bundle bundleLocation) {
        super(fm);

      // save the bundle to local variable
      this.bundleTitle = bundleTitle;
      this.bundleLocation = bundleLocation;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                TabTitle tabTitle = new TabTitle();
                // set the argument here
                tabTitle.setArguments(bundleTitle);
                return tabTitle;
            case 1:
                TabPosts tabPosts = new TabPosts();
                return tabPosts;
            case 2:
                TabLocation tabLocation = new TabLocation();
                // set the argument here
                tabTitle.setArguments(bundleLocation);
                return tabLocation;
            default:
                return null;
        }
    }

    ...
}

Now you can use constructor to pass the Bundle:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), bundleTitle, bundleLocation);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96