尝试调用虚拟方法 'java.lang.Object android.content.Context.getSystemService(java.lang.String)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39532507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)
提问by Iam ByeBlogs
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 linklike 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.
有人在此链接上这样说:除非在某些情况下,否则在 super.onCreate() 之后才能调用 Activity 超类上的方法。请将 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;
}
采纳答案by Iam ByeBlogs
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 !
我认为这解决了所有与我的问题一样出现相同错误的问题!
回答by Pradeep Sheoran
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. Some code writers write Layout inflater as the layout of Tab. but here we will use another layout where we will mention our internal Recyclerview layout.
实际上,当某些东西作为空字节出现时,就会出现这个问题。当我们使用回收器视图时,它主要出现。因为大多数人在 RecyclerViewAdpater 的 onCreateView 中混淆了。一些代码编写者将 Layout inflater 编写为 Tab 的布局。但在这里我们将使用另一种布局,我们将在其中提及我们的内部 Recyclerview 布局。
view= LayoutInflater.from(mcontext).inflate(R.layout.**item_tab14**,parent,false);
回答by Paul LeBeau
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. So don't try and create an adapter in the constructor, because the context will still be null at that point.
例如,在 a 中DialogFragment
,上下文将不可用,直到onCreateDialog()
被调用。所以不要尝试在构造函数中创建适配器,因为此时上下文仍然为空。