我在我的应用程序中完全使用片段。第一个活动被加载然后片段来到这个活动然后销毁。我的问题是我在点击列表项目内的按钮时尝试在listview适配器内加载支持mapfragment ,它第一次工作正常,但第二次
gmap = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap();
这一行在我的适配器中返回null。我应该怎么做才能解决这个问题。 我在我的适配器的oncreateviewholder方法中调用了适配器中的map片段。
公共类ProductListAdapter扩展了RecyclerView.Adapter {
private Activity context;
List<ListDetails> products;
SharedPreference sharedPreference;
public static com.android.volley.toolbox.ImageLoader imageLoader;
LayoutInflater inflater;
ViewHolder holder;
protected String Day = "";
public static View alertLayout;
private static GMapV2Direction gmapdirection;
public static GoogleMap gmap;
protected Document doc;
public ProductListAdapter(Context context, List<ListDetails> products) {
this.context = (Activity)context;
this.products = products;
sharedPreference = new SharedPreference();
imageLoader = AppController.getInstance().getImageLoader();
inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
TextView productNameTxt;
TextView Ratingsno;
TextView productPriceTxt;
TextView timedist;
TableLayout distancesection;
TextView categoryname;
ImageView favoriteImg;
RatingBar rb;
ImageView OfferImage;
ImageView MapImage;
NetworkImageView listimage;
public TextView distance;
public ImageView indicator;
TextView timer;
TextView Postedon;
public ContactViewHolder(View v) {
super(v);
productNameTxt = (TextView) v
.findViewById(R.id.name);
productPriceTxt = (TextView) v
.findViewById(R.id.listview_description);
distance = (TextView) v
.findViewById(R.id.listview_distane);
timedist = (TextView) v
.findViewById(R.id.timedist);
Ratingsno = (TextView) v
.findViewById(R.id.Ratingsno);
Postedon = (TextView) v
.findViewById(R.id.Postedon);
favoriteImg = (ImageView) v
.findViewById(R.id.fav_checkbox);
rb = (RatingBar) v
.findViewById(R.id.ratingbar);
timer = (TextView) v.findViewById(R.id.time);
categoryname = (TextView) v
.findViewById(R.id.Categoryname);
listimage = (NetworkImageView) v
.findViewById(R.id.list_image);
distancesection = (TableLayout) v
.findViewById(R.id.distancesection);
MapImage = (ImageView) v
.findViewById(R.id.imgmap);
}
}
@Override
public int getItemCount() {
return products.size();
}
@Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, final int position) {
try {
String text = "<font color=#222222>"+products.get(position).getShopName()+"</font>"+"<small><font color=#0091EA> ( </font></small>"+" <small><font color=#0091EA>"+products.get(position).getAreaName()+"</font></small>"+"<small><font color=#0091EA> ) </font></small>";
contactViewHolder.productNameTxt.setText(Html.fromHtml(text));
//holder.productNameTxt.setText(product.getShopName() + " " + "( " + Html.fromHtml("<font color='#0091EA'>" + product.getAreaName() + "</font>") + " )");
contactViewHolder.productPriceTxt.setText(products.get(position).getDeal() + "");
contactViewHolder.rb.setRating(Float.valueOf(products.get(position).getRatingAvg()));
contactViewHolder.Ratingsno.setText(products.get(position).getTotalRatings() + " Ratings");
contactViewHolder.timedist.setText(products.get(position).getTime());
contactViewHolder.distance.setText(String.valueOf(products.get(position).getDistance())+ " Km");
contactViewHolder.categoryname.setText(products.get(position).getCategoryName());
contactViewHolder.listimage.setImageUrl(products.get(position).getListImage(), imageLoader);
contactViewHolder.MapImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
final Dialog dialog = new Dialog(context);
if (alertLayout != null) {
ViewGroup parentViewGroup = (ViewGroup) alertLayout
.getParent();
if (parentViewGroup != null) {
parentViewGroup.removeView(alertLayout);
}
}
//dialog.setContentView(R.layout.list_map_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(alertLayout);
// dialog.setTitle("Map Directions");
ImageView cancel = (ImageView) dialog
.findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
dialog.cancel();
}
});
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
LoadPlaceDetails(products.get(position).getLat(),products.get(position).getLng(),products.get(position).getAreaName(),products.get(position).getCategoryName());
} catch (Exception e) {
e.printStackTrace();
PostLogcatErrors ple = new PostLogcatErrors();
ple.PostLogcatErorrs(e);
}
}
});
if (checkFavoriteItem(products.get(position))) {
contactViewHolder.favoriteImg.setImageResource(R.drawable.checked);
contactViewHolder.favoriteImg.setTag(context.getString(R.string.red_favcolor));
} else {
contactViewHolder.favoriteImg.setImageResource(R.drawable.unchecked);
contactViewHolder.favoriteImg.setTag(context.getString(R.string.grey_favcolor));
}
contactViewHolder.Postedon.setText("Posted On" + " " + products.get(position).getPostedDate());
contactViewHolder.timer.setText(products.get(position).getOfferEndTime());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
View itemView = LayoutInflater.
from(arg0.getContext()).
inflate(R.layout.list_item4, arg0, false);
LayoutInflater li = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Here i am adding Map layout
if (alertLayout == null)
alertLayout = li.inflate(R.layout.list_map_dialog, null);
return new ContactViewHolder(itemView);
}
public boolean checkFavoriteItem(ListDetails checkProduct) {
boolean check = false;
List<ListDetails> favorites = sharedPreference.getFavorites(context);
if (favorites != null) {
for (ListDetails product : favorites) {
if (product.equals(checkProduct)) {
check = true;
break;
}
}
}
return check;
}
private void LoadPlaceDetails(double destlat, double destlng, String areaname,String Category) {
try {
LatLng fromPosition = new LatLng(SplashScreen.nwLocation.getLatitude(), SplashScreen.nwLocation.getLongitude());
LatLng toPosition = new LatLng(destlat, destlng);
gmapdirection = new GMapV2Direction();
FragmentManager fm = ((FragmentActivity) context)
.getSupportFragmentManager();
//Here i am loading map after click on Map button
gmap = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap();
gmap.clear();
gmap.animateCamera(CameraUpdateFactory.newLatLngZoom(fromPosition,13));
gmap.addMarker(new MarkerOptions()
.position(fromPosition)
.title("Current Location")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))).showInfoWindow();;
switch (Category) {
case "book_store":
gmap.addMarker(new MarkerOptions()
.position(toPosition)
.title(areaname)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_book, ""))));
break;
case "beauty_salon":
gmap.addMarker(new MarkerOptions()
.position(toPosition)
.title(areaname)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_fashion, ""))));
break;
case "electronics_store":
gmap.addMarker(new MarkerOptions()
.position(toPosition)
.title(areaname)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_electronic, ""))));
break;
case "food":
gmap.addMarker(new MarkerOptions()
.position(toPosition)
.title(areaname)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_food, ""))));
break;
case "home_goods_store":
gmap.addMarker(new MarkerOptions()
.position(toPosition)
.title(areaname)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_home, ""))));
break;
case "restaurant":
gmap.addMarker(new MarkerOptions()
.position(toPosition)
.title(areaname)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_job, ""))));
break;
case "real_estate_agency":
gmap.addMarker(new MarkerOptions()
.position(toPosition)
.title(areaname)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_realestate, ""))));
break;
case "travel_agency":
gmap.addMarker(new MarkerOptions()
.position(toPosition)
.title(areaname)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_vehicle, ""))));
break;
}
Document doc = gplaces.getDocument(fromPosition, toPosition,
GMapV2Direction.MODE_DRIVING,gmapdirection,gmap, context);
} catch (NumberFormatException e) {
e.printStackTrace();
PostLogcatErrors ple = new PostLogcatErrors();
ple.PostLogcatErorrs(e);
}
}
}
我在这做错了吗?建议我这个...感谢期待答案 0 :(得分:0)
您可能正在删除R.id.map中的SupportMapFragment。但是你向我们展示的代码太少,无法知道出了什么问题。