-1

People, I'm trying to send data from a clicked item in a ListView (which is loaded into a Fragment of MainActivity, which in turn has a SlidingTabLayout structure) to load details in another Fragment, which is loaded in ItemActivity (which also has a structure of SlidingTabLayout). I'm trying to pass data between different Fragments of different Activities, NOT only between Activities (that I got to do, between LazyAdapter and ItemActivity activities).

For a few tries, the following error occurred:

27804-27804/br.com.toyansk.swipeabletabs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: br.com.toyansk.swipeabletabs, PID: 27804
java.lang.NullPointerException
        at br.com.toyansk.swipeabletabs.Detail_InfoFragment.onCreateView(Detail_InfoFragment.java:47)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
        at android.view.View.measure(View.java:16540)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
        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:16540)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
        at android.view.View.measure(View.java:16540)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
        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:16540)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16540)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
        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:16540)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
        at android.view.View.measure(View.java:16540)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
        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:5086)
        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:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

Here are the codes that currently I have:

LazyAdapter (which sends data at the click of the item in ListViewFragment):

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

Activity activity;
ArrayList<HashMap<String, String>> data;
LayoutInflater inflater=null;
ImageLoader imageLoader;
HashMap<String, String> pdv = new HashMap<String, String>();

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, parent, false);

    pdv = data.get(position);

    TextView Nome = (TextView)vi.findViewById(R.id.Nome);
    TextView Endereco = (TextView)vi.findViewById(R.id.Endereco);
    ImageView Logo =(ImageView)vi.findViewById(R.id.Logo);

    // Setting all values in listview
    Nome.setText(pdv.get(ListViewFragment.TAG_NOME));
    Endereco.setText(pdv.get(ListViewFragment.TAG_ENDERECO));
    imageLoader.DisplayImage(pdv.get(ListViewFragment.TAG_LOGO), Logo);

    vi.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Get the position
            pdv = data.get(position);
            Intent intent = new Intent(activity, ItemActivity.class);
            intent.putExtra("ID", pdv.get(ListViewFragment.TAG_ID));
            intent.putExtra("Nome", pdv.get(ListViewFragment.TAG_NOME));
            intent.putExtra("Endereco", pdv.get(ListViewFragment.TAG_ENDERECO));
            intent.putExtra("Logo", pdv.get(ListViewFragment.TAG_LOGO));
            activity.startActivity(intent);

        }
    });

    return vi;
}
}

ItemActivity:

import br.com.toyansk.swipeabletabs.adapter.SlidingTabLayout;
import br.com.toyansk.swipeabletabs.adapter.SubPagerAdapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.List;

public class ItemActivity extends ActionBarActivity {

Toolbar toolbar;
ViewPager pager;
SubPagerAdapter adapter;
List<Fragment> mFragments;
SlidingTabLayout tabs;
CharSequence Titles[]={"Informações", "Serviços", "Avaliações"};
int Numboftabs =3;

//To load on Fragment
String ID;
String Nome_PDV;
String Endereco;
String Logo;
ImageLoader imageLoader = new ImageLoader(this);
public ImageView imageViewRound;

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

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

    adapter =  new SubPagerAdapter(getSupportFragmentManager(),mFragments,Titles,Numboftabs);
    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);

    imageViewRound=(ImageView)findViewById(R.id.Logo);

    Intent i = getIntent();

    ID = i.getStringExtra("ID");
    Nome = i.getStringExtra("Nome");
    Endereco = i.getStringExtra("Endereco");
    Logo = i.getStringExtra("Logo");

    setTitle(Nome);

    Detail_InfoFragment info = new Detail_InfoFragment();
    info.setArguments(getIntent().getExtras());
    getSupportFragmentManager().beginTransaction().add(android.R.id.content, info).commit();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

adapter.SubPagerAdapter:

import br.com.toyansk.swipeabletabs.ListServicesFrag;
import br.com.toyansk.swipeabletabs.Detail_InfoFragment;
import br.com.toyansk.swipeabletabs.AvalFragment;
import br.com.toyansk.swipeabletabs.R;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List;


public class SubPagerAdapter extends FragmentStatePagerAdapter {

CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs;
List<Fragment> fragmentList;

public SubPagerAdapter(FragmentManager fm, List<Fragment> fragments, CharSequence mTitles[], int mNumbOfTabsumb) {

    super(fm);

    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;
    fragmentList = fragments;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            return new ListServicesFrag();
        case 1:
            return new Detail_InfoFragment();
        case 2:
            return new AvalFragment();
    }

    return null;
}
// This method return the titles for the Tabs in the Tab Strip

@Override
public CharSequence getPageTitle(int position) {
    return Titles[position];
}

// This method return the Number of tabs for the tabs Strip

@Override
public int getCount() {
    return NumbOfTabs;
}
}

And finally, Detail_InfoFragment (that should receive the data coming from the click of ListViewFragment item):

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class Detail_InfoFragment extends Fragment {
String ID;
String Nome;
String Endereco;
String Logo;
ImageLoader imageLoader = new ImageLoader(getActivity());

private ImageView imageViewRound;

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

    View rootView = inflater.inflate(R.layout.single_item, container, false);

    imageViewRound=(ImageView)rootView.findViewById(R.id.Logo);

    Bundle bundle = getArguments();
    String ID = bundle.getString("ID"); //=> Here is the error
    String Nome_PDV = bundle.getString("Nome");
    String Endereco = bundle.getString("Endereco");
    String Logo = bundle.getString("Logo");

    TextView txt_Nome = (TextView) rootView.findViewById(R.id.Nome);
    TextView txt_Endereco = (TextView) rootView.findViewById(R.id.Endereco); 
    ImageView imgLogo = (ImageView) rootView.findViewById(R.id.Logo);

    // Set results to the TextViews
    txt_Nome.setText(Nome);
    txt_Endereco.setText(Endereco);
    imageLoader.DisplayImage(Logo, imgLogo);

    return rootView;
}
}

What's wrong and how to fix it? Thanks in advance!

Atoyansk
  • 275
  • 4
  • 17
  • possible duplicate of [How do I pass data between activities in Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – Cedar Jul 29 '15 at 13:22
  • @NI Cedar, I know how to pass data between Activities (please see, I can send the data to the activity ItemActivity). My problem is when I try to send between Fragments of differents Activities. – Atoyansk Jul 29 '15 at 13:31

1 Answers1

0

Try the following answer.

I think it has what you are looking for.

Community
  • 1
  • 1
Cedar
  • 121
  • 10
  • @NI Cedar, I know how to pass data between Activities (please see, I can send the data to the activity ItemActivity). My problem is when I try to send between different Fragments of differents Activities. – Atoyansk Jul 29 '15 at 13:45