0

I have the following problem, I have a ViewPager, the Viewpager contains a Fragment and this fragment contains a textview that contains an int value (8000 in this case). What i want is a way to make gain 500 when the button _Mas500 is pressed. When i run the proyect it appears this error:

08-06 22:42:15.687: E/AndroidRuntime(8639): FATAL EXCEPTION: Thread-10

08-06 22:42:15.687: E/AndroidRuntime(8639): java.lang.IllegalStateException: Must be called from main thread of process

Any ideas are accepted! Thanks!

Heres my code:

public class MainActivity extends ActionBarActivity {
    private int _JugLP;
    private DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
    private Button _Mas500;
    ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    declarar();
    _Mas500.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int n = _JugLP + 500;
            mDemoCollectionPagerAdapter.setLP(mViewPager.getCurrentItem(),
                    n);
            mDemoCollectionPagerAdapter.notifyDataSetChanged();
        }
    });
}

@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    declarar();
}

public void declarar() {
    mViewPager = (ViewPager) findViewById(R.id.pager);
    _JugLP = 8000;
    mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(
            getSupportFragmentManager(), _JugLP);
    mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    _Mas500 = (Button) findViewById(R.id.btn_mas500);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public static class DemoCollectionPagerAdapter extends
        FragmentStatePagerAdapter {
    int _LP;
    DemoObjectFragment[] fragments;

    public DemoCollectionPagerAdapter(FragmentManager fm, int n) {
        super(fm);
        _LP = n;
         fragments = new DemoObjectFragment[n];
        Bundle args = new Bundle();
        args.putInt(DemoObjectFragment.ARG_OBJECT, _LP);
        fragments[0].setArguments(args);
        fragments[1].setArguments(args);
    }

    @Override
    public Fragment getItem(int i) {
        return fragments[i];
    }

    public void setLP(int i, int LP) {
        fragments[i].setLP(_LP);
    }

    @Override
    public int getItemPosition(Object object) {
        // TODO Auto-generated method stub
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        // For this contrived example, we have a 100-object collection.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "Titulo";
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DemoObjectFragment extends Fragment {

    public static final String ARG_OBJECT = "object";
    private TextView _TxtVwLP;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.fragment_collection_object, container, false);
        Bundle args = getArguments();
        _TxtVwLP = (TextView) rootView.findViewById(android.R.id.text1);
        _TxtVwLP.setText(Integer.toString(args.getInt(ARG_OBJECT)));
        return rootView;
    }

    public void setLP(int LP) {
        _TxtVwLP.setText(String.valueOf(LP));
    }
}

}

  • remove @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); declarar(); } – mmlooloo Aug 07 '14 at 01:52
  • Thanks! mmlooloo, I did that and added the following code in public DemoCollectionPagerAdapter: `fragments[0]= new DemoObjectFragment(); fragments[1]=new DemoObjectFragment();` However, the text is still not changing, i click the button and the value on the textview doesnt change? Can someone tell pls whats wrong with my code? – Betun Herrera Alanis Aug 07 '14 at 02:54

2 Answers2

1

I think your problem is in notifyDataSetChanged. because when you call notifyDataSetChanged you tell the viewPager hey viewPager i have removed or created some children for you, reload yourself. and when he reload himself he creates fragments without any changed you have already made. so I think you should change your logic.

look at these for solution: Android FragmentStatePagerAdapter, how to tag a fragment to find it later

Is it possible to access the current Fragment being viewed by a ViewPager?

Community
  • 1
  • 1
mmlooloo
  • 18,937
  • 5
  • 45
  • 64
0

Thanks mmlooloo! After reading the useful documents you posted I was able to generate an answer. Here's what i Changed:

_Mas500.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int n = _JugLP + 500;
            _JugLP=n;
            DemoObjectFragment x = (DemoObjectFragment) mDemoCollectionPagerAdapter
                    .instantiateItem(mViewPager,
                            mViewPager.getCurrentItem());
            x.setLP(n);
        }
    });

After trying it several times, the textview finally changed! It seems to work fine and I think that solves my problem for now. Thanks mmlooloo!