Attempt to invoke virtual method #39;void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)#39; on a null object reference(尝试在空对象引用上调用虚拟方法 void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdap
问题描述
我正在尝试使用 android 材料设计实现滑动标签布局.但它给了我NullPointerException
.到目前为止,这是我的代码:
I'm trying to implement sliding tab layout using android material design. But it gives me NullPointerException
. Here is my code so far:
MainActivity.java
activity_main.xml
这是我运行应用程序后遇到的错误:
This is the error I got after run my application:
他们一直说 pager
对象为空.但对我来说,我已经正确设置了它.
They keep on saying that the pager
object is null. But for me I have set it correctly.
推荐答案
您在 activity_main.xml
中有您的视图分页器,而您的活动布局是setContentView(R.layout.activity_main_appbar)
.activity_main_appbar.xml
中没有查看寻呼机.
You have your view pager in activity_main.xml
while your layout for activity is
setContentView(R.layout.activity_main_appbar)
. You do not have view pager in activity_main_appbar.xml
.
findViewById
在当前视图层次结构中查找视图.我想它应该是 setContentView(R.layout.activity_main);
但请检查包含的布局 app_bar.xml
findViewById
looks for a view in the current view hierarchy. I guess it should be setContentView(R.layout.activity_main);
but do check what views you have in included layout app_bar.xml
这篇关于尝试在空对象引用上调用虚拟方法 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!