我有回收站適配器類,並實施了setOnClickListener
更換片段:android.app.Application不能轉換爲android.app.Activity而在回收站使用FragmentManager適配器
public PlacesAdapter(Context mContext, List<Place> placeList) {
this.mContext = mContext;
this.placeList = placeList;
}
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = ((Activity)mContext).getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.mainFragmenLayout, new restaurantsFragment());
Toast.makeText(mContext, "The place is tapped.", Toast.LENGTH_SHORT).show();
}
});
我使用API 25和有以下進口:
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
的上下文中創建如下:
private Context mContext;
public PlacesAdapter(Context mContext, List<Place> placeList) {
this.mContext = mContext;
this.placeList = placeList;
}
並將適配器實例如下:
adapter = new PlacesAdapter(getActivity().getApplicationContext(), placeList);
但下面的錯誤應用程序崩潰和回報:
java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
請告訴你如何分配'mContext' –
你用'getApplicationContext'而不是活動,我敢打賭 –
@ cricket_007請查看更新的問題。 –