尝试调用虚拟方法';java.lang.Object android.content.Context.getSystemService(java.lang.String)

尝试调用虚拟方法';java.lang.Object android.content.Context.getSystemService(java.lang.String),java,android,Java,Android,我想问,为什么我的代码在设备上运行时不起作用,但当我像Genymotion一样在仿真器android上运行时,它工作得很好 有人这样说:除非在某些情况下,否则在super.onCreate()之后才能调用Activity超类上的方法。请将promptsView的初始化推迟到调用super.onCreate()之后 我还是不明白,如果你有同样的问题,请告诉我 无论如何,如果我的解释不好,我很抱歉 public class DestinationListAdapter extends ArrayAd

我想问,为什么我的代码在设备上运行时不起作用,但当我像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;
    }
公共类DestinationListAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
List data=Collections.emptyList();
公共目的地静态适配器(上下文上下文、内部布局资源ID、列表数据){
超级(上下文、布局资源ID、数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个数据=数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
DestinationHolder=null;
if(行==null)
{
//下一行的预测误差
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
holder=新目的地holder();
holder.destination\u id=(TextView)row.findViewById(R.id.destination\u id);
holder.destination\u name=(TextView)row.findViewById(R.id.destination\u name);
row.setTag(支架);
}
其他的
{
holder=(DestinationHolder)行。getTag();
}
DestinationModel weather=data.get(位置);
holder.destination_id.setText(weather.getDestination_id());
holder.destination_name.setText(weather.getDestination_name());
返回行;
}

确保返回并继续之前的活动 我刚刚添加并检查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);
}

我认为这解决了所有和我的问题一样的错误!

事实上,当出现空字节时,这个问题就会出现。 当我们使用“回收者”视图时,它通常会出现。因为大多数人混淆了“回收服务水”的onCreateView。 一些代码编写者将布局充气器编写为选项卡的布局。但在这里,我们将使用另一个布局,其中我们将提到我们的内部Recyclerview布局

view= LayoutInflater.from(mcontext).inflate(R.layout.**item_tab14**,parent,false);

请注意您在生命周期中的位置。
getContext()
的值可能还不可用


例如,在
DialogFragment
中,上下文在
onCreateDialog()之前不可用
被调用。因此不要尝试在构造函数中创建适配器,因为此时上下文仍然为空。

请编辑您的问题并发布整个Java堆栈跟踪。您在哪里初始化
DestinationListAdapter
?我调用我的类扩展片段并编写我称之为DestinationListAdapter的代码:adapter=new DestinationListAdapter(getContext(),R.layout.destination_listview,data_destination);@ByeWebster更改了我的答案,请检查一下。getActivity()返回null的情况是什么?@SlickSlime返回并继续上一个活动、行为模拟器genymotion和设备不一样