此ImagePager Adapter
班级来自Fragment
而不是Activity
。我将Activity
添加到getFragmentManager()
;
public class ImagePagerAdapter extends PagerAdapter{
Context context;
LayoutInflater _inflater = null;
ImageLoader imageLoaderProfile;
Activity activity;
private LayoutInflater inflater;
private List<Profile> movieItems;
Button gpsBtn;
Bundle bundle = new Bundle();
public ImagePagerAdapter(Context context, List<Profile> movieItems) {
super();
this.context = context;
_inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.movieItems = movieItems;
imageLoaderProfile = new ImageLoader(activity);
}
@Override
public Object instantiateItem(final ViewGroup container, int position) {
final LinearLayout layout = (LinearLayout) _inflater.inflate(R.layout.list_profile, null);
final Profile m = movieItems.get(position);
TextView nameView = (TextView) layout.findViewById(R.id.profileTitle);
ImageView cardView = (ImageView) layout.findViewById(R.id.profileCard);
nameView.setText(m.getTitle());
ImageView profileCard = (ImageView) layout.findViewById(R.id.profileCard);
Log.d("Card Name: ", String.valueOf(m.getTitle()));
imageLoaderProfile.DisplayImage(m.getThumbnailUrl(), profileCard);
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Card Name: ", m.getTitle());
bundle.putString("nombre", m.getTitle());
InfoProfile frag = new InfoProfile();
//frag.setArguments(bundle);
FragmentManager manager = activity.getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.myLayout, frag, "VivzFragment");
transaction.addToBackStack(null);
// manager.popBackStack();
transaction.commit();
}
});
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view.equals(obj);
}
}
我试图在&#34; PageAdpater&#34;中打开一个片段:
FragmentManager manager = activity.getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.myLayout, frag, "VivzFragment");
transaction.addToBackStack(null);
// manager.popBackStack();
transaction.commit();
导致此错误:
at icomm.cardboard.adapters.ImagePagerAdapter$1.onClick(ImagePagerAdapter.java:74)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)