我有以下问题,我有一个ViewPager,Viewpager包含一个Fragment,这个片段包含一个包含int值的textview(在本例中为8000)。我想要的是按下_Mas500按钮时获得500的方法。当我运行proyect时,会出现此错误:
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
接受任何想法!谢谢!
继承我的代码:
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));
}
}
}
答案 0 :(得分:1)
我认为您的问题出在notifyDataSetChanged中。因为当你调用notifyDataSetChanged时,你告诉viewPager hey viewPager我已经删除或创建了一些子代,你自己重新加载。当他重新加载自己时,他创造的碎片没有你已经做过的任何改变。所以我认为你应该改变你的逻辑。
看看这些解决方案: Android FragmentStatePagerAdapter, how to tag a fragment to find it later
Is it possible to access the current Fragment being viewed by a ViewPager?
答案 1 :(得分:0)
谢谢mmlooloo!在阅读了您发布的有用文档后,我能够生成答案。这是我改变的内容:
_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);
}
});
经过多次尝试后,textview终于改变了!它似乎工作正常,我认为这解决了我现在的问题。谢谢mmlooloo!