0
私はアンドロイドで簡単なゲームを開発しています。私は、サーバーからデータを取得してViewPagerにロードするアクティビティを持っています。これは、アクティビティコードスニペットです:アプリケーションのPagerAdapterがnotifyDataSetChanged()を呼び出さずにアダプタの内容を変更しました
url = new ArrayList<>();
name = new ArrayList<>();
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getApplicationContext(),url,name);
viewPager.setAdapter(viewPagerAdapter);
AppController.socket.emit("getDoodle","")
.on("doodleList", new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONArray data = (JSONArray)args[0];
Log.e("MYTAG",data.toString());
url.clear();
name.clear();
for(int i=0;i<AppController.playersId.size();i++){
try {
if(!data.getJSONObject(i).getString("doodle").equals("null")) {
url.add(data.getJSONObject(i).getString("doodle"));
name.add(data.getJSONObject(i).getString("id"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
viewPagerAdapter.notifyDataSetChanged;
}
});
}
});
これは私のPagerAdapterです:
class ViewPagerAdapter extends PagerAdapter {
Context mContext;
ArrayList<String> mUri,mId;
public ViewPagerAdapter(Context context, ArrayList<String> imageUri, ArrayList<String> sid){
mContext = context;
mUri = imageUri;
this.mId=sid;
Log.e("MYTAG",mUri.toString());
}
@Override
public int getCount() {
return mUri.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
LayoutInflater mLayoutInflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = mLayoutInflater.inflate(R.layout.pager_child, container, false);
final ImageView imageView = (ImageView) itemView.findViewById(R.id.doodleView);
TextView nameText = (TextView) itemView.findViewById(R.id.ownerText);
String temp = mUri.get(position);
if(!temp.equals("")){
Log.e("replace","2");
Glide.with(mContext)
.load(temp)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(imageView);
nameText.setText(mId.get(position));
}
Log.d("pager",mUri.get(position));
itemView.setTag(position);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return false;
}}
マイエラーログ:
FATAL EXCEPTION: main
Process: com.nirup.fuku, PID: 10421
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 1 Pager id: com.nirup.fuku:id/viewPager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.nirup.fuku.ViewPagerAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:1167)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19857)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
at android.view.View.measure(View.java:19857)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
私は私が間違ってやっているかわかりません。..どんな助けもありがたい。
私はそれを試みました。上記のようにコンストラクタのnotifydatasetchanged()で正確なコードを使用しました..アプリがまだクラッシュしました..リストの変更があるたびに新しいViewPagerAdapterオブジェクトを渡してみましたが、それでもクラッシュします –
'PagerAdapter'ではなく' FragmentStatePagerAdapter'を拡張しようとします。あなたが呼び出すことを確認してください.. '' ViewPagerAdapter.notifyDataSetChanged() '...あなたがアダプタをリフレッシュした後、リストが変更されるたびに必ず新しいViewPagerAdapterオブジェクトを渡す必要はない...私はちょうど例を示している – rafsanahmad007