我想问一下,为什么我的代码在设备上运行时不起作用,但是当我在模拟器上运行时像Genymotion一样,它的工作完美…
有人在这个link上这样说过:除非在某些情况下,你才能在super.onCreate()之后调用Activity超类上的方法.请推迟你的promptsView初始化,直到调用super.onCreate()之后.
我仍然没有得到它,请告诉我你是否有同样的问题..
无论如何,如果我的解释不好,我很抱歉..
public class DestinationListAdapter extends ArrayAdapter<DestinationModel> { Context context; int layoutResourceId; List<DestinationModel> data = Collections.emptyList(); public DestinationListAdapter(Context context,int layoutResourceId,List<DestinationModel> data) { super(context,layoutResourceId,data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } @Override public View getView(int position,View convertView,ViewGroup parent) { View row = convertView; DestinationHolder holder = null; if(row == null) { // Predicted Error At Line Below LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId,parent,false); holder = new DestinationHolder(); holder.destination_id = (TextView) row.findViewById(R.id.destination_id); holder.destination_name = (TextView) row.findViewById(R.id.destination_name); row.setTag(holder); } else { holder = (DestinationHolder)row.getTag(); } DestinationModel weather = data.get(position); holder.destination_id.setText(weather.getDestination_id()); holder.destination_name.setText(weather.getDestination_name()); return row; }
解决方法
确保何时返回&继续以前的活动
我刚加了&检查getContext()!= null
我刚加了&检查getContext()!= null
这是一个很好的例子:
在阻止之前
adapter = new ExampleAdapter(getContext()); adapter.setData(items); listView.setAdapter(adapter);
最好替换getActivity()!= null
例如:
if (getActivity()!=null){ adapter = new ExampleAdapter(getActivity()); adapter.setData(items); listView.setAdapter(adapter); }