我想问,为什么我的代码在设备上运行时不起作用,但是当我像 Genymotion 一样在模拟器 android 上运行时,它运行得很好..
有人在这个 链接 上这样说:除非在某些情况下,否则你不能在 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;
}
原文由 Iam ByeBlogs 发布,翻译遵循 CC BY-SA 4.0 许可协议
为了确保返回并继续之前的活动,我刚刚添加并检查 getContext()!=null
这是一个很好的例子:
块前
更好地替换 getActivity()!=null
例如:
我认为这解决了所有与我的问题相同的错误的问题!