简体   繁体   English

尝试调用虚拟方法 '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)

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..我想问一下,为什么我的代码在设备上运行时不起作用,但是当我像 Genymotion 一样在模拟器 android 上运行时,它运行良好..

someone said on this link like this : you cannot call methods on the Activity superclass until after super.onCreate(), except in certain situations.有人在此链接上这样说:除非在某些情况下,否则在 super.onCreate() 之后才能调用 Activity 超类上的方法。 Please postpone your initialization of promptsView until after super.onCreate() has been called.请将 promptsView 的初始化推迟到调用 super.onCreate() 之后。

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;
    }

for make sure when return back & continuing previous activity i just Added & checking for getContext()!=null为了确保何时返回并继续之前的活动,我刚刚添加并检查了 getContext()!=null

Here's an good example :这是一个很好的例子:

Before block块前

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

And better replace for getActivity()!=null并更好地替换 getActivity()!=null

For example:例如:

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 !我认为这解决了所有与我的问题一样出现相同错误的问题!

Be conscious of where in the lifecycle you are.意识到您在生命周期中的哪个位置。 The value of getContext() may not be available yet. getContext()的值可能尚不可用。

For example, in a DialogFragment , the context will not be available until onCreateDialog() is called.例如,在DialogFragment ,在调用onCreateDialog()之前,上下文将不可用。 So don't try and create an adapter in the constructor, because the context will still be null at that point.所以不要尝试在构造函数中创建适配器,因为此时上下文仍然为空。

Actually this issue come when something comes as null byte.实际上,当某些东西作为空字节出现时,就会出现这个问题。 and when we are using recycler view then it comes mostly.当我们使用回收器视图时,它主要出现。 Because most people confuse in onCreateView of RecyclerViewAdpater.因为大多数人在 RecyclerViewAdpater 的 onCreateView 中混淆了。 Some code writers write Layout inflater as the layout of Tab.一些代码编写者将 Layout inflater 编写为 Tab 的布局。 but here we will use another layout where we will mention our internal Recyclerview layout.但在这里我们将使用另一种布局,我们将在其中提及我们的内部 Recyclerview 布局。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在空对象引用上调用虚拟方法“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)' on a null object reference 错误:尝试在空对象引用上调用虚方法&#39;java.lang.Object android.content.Context.getSystemService(java.lang.String)&#39; - error:Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference Android 应用程序返回“错误:尝试调用虚拟方法 &#39;java.lang.Object android.content.Context.getSystemService(java.lang.String)” - Android app returns "Error: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)" Flutter:PlatformException(错误,尝试调用虚拟方法'java.lang.Object android.app.Activity.getSystemService(java.lang.String) - Flutter:PlatformException(error, Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' ViewPagerAdapter尝试在空对象引用上调用虚拟方法Context.getSystemService(java.lang.String)&#39; - ViewPagerAdapter Attempt to invoke virtual method Context.getSystemService(java.lang.String)' on a null object reference android.content.Context.getSystemService(java.lang.String)&#39;对空对象的引用 - android.content.Context.getSystemService(java.lang.String)' on a null object reference 尝试调用虚拟方法 &#39;java.lang.Object android.content.Context - Attempt to invoke virtual method 'java.lang.Object android.content.Context 由于尝试调用虚拟方法&#39;java.lang.String android.content.Context.getPackageName(),因此意图失败 - Intent fails because of Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName() 尝试调用虚拟方法&#39;java.lang.String android.content.Context.getPackageName() - Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName() 尝试在空对象引用上调用虚拟方法 &#39;android.content.Context.getSharedPreferences(java.lang.String, int)&#39; - Attempt to invoke virtual method 'android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM