尝试调用虚拟方法'java.lang.Object android.content.Context.getSystemService(java.lang.String) [英] Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)

查看:85
本文介绍了尝试调用虚拟方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下,为什么我的代码在设备上运行时不起作用,但是当我在Genymotion之类的模拟器android上运行时,它运行得很好..

i want to ask, why my code doesnt work when running on device, but when i run on emulator android like a Genymotion, it's working perfectly..

有人在此链接上说这样:您只能在super.onCreate()之后调用Activity超类上的方法,除非在某些情况下.请推迟对hintsView的初始化,直到调用super.onCreate()之后.

someone said on this link like this : you cannot call methods on the Activity superclass until after super.onCreate(), except in certain situations. Please postpone your initialization of promptsView until after super.onCreate() has been called.

我还是不明白,如果您遇到同样的问题,请告诉我..

i still dont get it, please tell me if you have the same problems..

无论如何,对不起我的解释不好..

anyway , i'm sorry if my explanation is bad..

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

for make sure when return back & continuing previous activity i just Added & checking for getContext()!=null

这是一个很好的例子:

在阻止之前

adapter = new ExampleAdapter(getContext());
adapter.setData(items);
listView.setAdapter(adapter);

最好将其替换为getActivity()!= null

And better replace for getActivity()!=null

例如:

if (getActivity()!=null){
    adapter = new ExampleAdapter(getActivity());
    adapter.setData(items);
    listView.setAdapter(adapter);
}

我认为这解决了所有与我的问题有相同错误的问题!

I think this is solved all problem which got the same error like my problems !

这篇关于尝试调用虚拟方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆