尝试在空对象引用上调用虚拟方法“void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapt
Posted
技术标签:
【中文标题】尝试在空对象引用上调用虚拟方法“void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)”【英文标题】:Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference 【发布时间】:2015-10-27 14:55:04 【问题描述】:我正在尝试使用 android 材料设计实现滑动标签布局。但它给了我NullPointerException
。到目前为止,这是我的代码:
MainActivity.java
public class MainActivity extends ActionBarActivity
private Toolbar toolbar;
private ViewPager pager;
private SlidingTabLayout nLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer_fragment);
drawerFragment.setUp(R.id.navigation_drawer_fragment, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
nLayout = (SlidingTabLayout) findViewById(R.id.tabs);
nLayout.setViewPager(pager);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
if (id == R.id.navigate)
startActivity(new Intent(this, SubActivity.class));
return super.onOptionsItemSelected(item);
class MyPagerAdapter extends FragmentPagerAdapter
String tabs[];
public MyPagerAdapter(FragmentManager fm)
super(fm);
tabs = getResources().getStringArray(R.array.tabs);
@Override
public Fragment getItem(int position)
MyFragment myFragment = MyFragment.getInstance(position);
return myFragment;
@Override
public CharSequence getPageTitle(int position)
return tabs[position];
@Override
public int getCount()
return 3;
public static class MyFragment extends Fragment
private TextView textView;
public static MyFragment getInstance(int position)
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("position", position);
myFragment.setArguments(args);
return myFragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View layout = inflater.inflate(R.layout.fragment_my, container, false);
textView = (TextView) layout.findViewById(R.id.position);
Bundle bundle = getArguments();
if (bundle != null)
textView.setText("The page selected is " + bundle.getInt("position"));
return layout;
activity_main.xml
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical"
tools:context="carsaleapp.shanaka.com.carsaleapp.MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<app.shanaka.tabs.SlidingTabLayout
android:id="@+id/tabs"
android:layout_
android:layout_ />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_
android:layout_
android:layout_weight="1" />
</LinearLayout>
这是我运行应用程序后遇到的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfocarsaleapp.shanaka.com.carsaleapp/carsaleapp.shanaka.com.carsaleapp.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at carsaleapp.shanaka.com.carsaleapp.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
他们一直说pager
对象为空。但对我来说,我已经正确设置了它。
pager = (ViewPager) findViewById(R.id.pager);
【问题讨论】:
【参考方案1】:您的视图寻呼机位于activity_main.xml
,而您的活动布局是
setContentView(R.layout.activity_main_appbar)
。您在activity_main_appbar.xml
中没有查看寻呼机。
findViewById
在当前视图层次结构中查找视图。我想应该是setContentView(R.layout.activity_main);
,但请检查您在包含的布局中拥有哪些视图app_bar.xml
【讨论】:
当然是。不知道我是怎么错过这一点的。无论如何它在setContentView(R.layout.activity_main);
之后完美运行谢谢。
我有同样的错误,但因为我错过了 pager = (ViewPager) findViewById(R.id.pager);部分【参考方案2】:
可能是您传递了错误的 viewPager id。请从 XML 文件中检查您的 id
【讨论】:
这对问题没有任何价值。 解决了我的问题。谢谢。以上是关于尝试在空对象引用上调用虚拟方法“void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapt的主要内容,如果未能解决你的问题,请参考以下文章
尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'
NullPointerException 错误尝试在空对象引用上调用虚拟方法错误
AsyncTask +数据库尝试在空对象引用上调用虚拟方法[重复]
数据库错误:尝试在空对象引用上调用虚拟方法“Cursor .getScene()”[重复]
尝试在空对象引用上调用虚拟方法 'android.view.SurfaceHolder android.view.SurfaceView.getHolder()'