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