<bdo id='zd8Sl'></bdo><ul id='zd8Sl'></ul>
  1. <small id='zd8Sl'></small><noframes id='zd8Sl'>

  2. <tfoot id='zd8Sl'></tfoot>

  3. <i id='zd8Sl'><tr id='zd8Sl'><dt id='zd8Sl'><q id='zd8Sl'><span id='zd8Sl'><b id='zd8Sl'><form id='zd8Sl'><ins id='zd8Sl'></ins><ul id='zd8Sl'></ul><sub id='zd8Sl'></sub></form><legend id='zd8Sl'></legend><bdo id='zd8Sl'><pre id='zd8Sl'><center id='zd8Sl'></center></pre></bdo></b><th id='zd8Sl'></th></span></q></dt></tr></i><div id='zd8Sl'><tfoot id='zd8Sl'></tfoot><dl id='zd8Sl'><fieldset id='zd8Sl'></fieldset></dl></div>

      <legend id='zd8Sl'><style id='zd8Sl'><dir id='zd8Sl'><q id='zd8Sl'></q></dir></style></legend>

      尝试在空对象引用上调用虚拟方法 'void android.support.v4.view.ViewPager.

      时间:2023-10-04
      <legend id='e0oV1'><style id='e0oV1'><dir id='e0oV1'><q id='e0oV1'></q></dir></style></legend>

        <tbody id='e0oV1'></tbody>
            <bdo id='e0oV1'></bdo><ul id='e0oV1'></ul>
            • <small id='e0oV1'></small><noframes id='e0oV1'>

              <i id='e0oV1'><tr id='e0oV1'><dt id='e0oV1'><q id='e0oV1'><span id='e0oV1'><b id='e0oV1'><form id='e0oV1'><ins id='e0oV1'></ins><ul id='e0oV1'></ul><sub id='e0oV1'></sub></form><legend id='e0oV1'></legend><bdo id='e0oV1'><pre id='e0oV1'><center id='e0oV1'></center></pre></bdo></b><th id='e0oV1'></th></span></q></dt></tr></i><div id='e0oV1'><tfoot id='e0oV1'></tfoot><dl id='e0oV1'><fieldset id='e0oV1'></fieldset></dl></div>

                <tfoot id='e0oV1'></tfoot>
              1. 本文介绍了尝试在空对象引用上调用虚拟方法 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用 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

                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_width="match_parent"
                    android:layout_height="match_parent"
                    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_width="match_parent"
                        android:layout_height="wrap_content" />
                
                
                    <android.support.v4.view.ViewPager
                        android:id="@+id/pager"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1" />
                
                
                </LinearLayout>
                

                这是我运行应用程序后遇到的错误:

                This is the error I got after run my application:

                java.lang.RuntimeException: Unable to start activity ComponentInfo{carsaleapp.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 对象为空.但对我来说,我已经正确设置了它.

                They keep on saying that the pager object is null. But for me I have set it correctly.

                pager = (ViewPager) findViewById(R.id.pager);
                

                推荐答案

                您在 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)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:圆形 ViewPager.片段在第一轮后无法正常工作 下一篇:平滑滚动在 ViewPager 中不起作用(支持库)

                相关文章

                    <i id='EA8ta'><tr id='EA8ta'><dt id='EA8ta'><q id='EA8ta'><span id='EA8ta'><b id='EA8ta'><form id='EA8ta'><ins id='EA8ta'></ins><ul id='EA8ta'></ul><sub id='EA8ta'></sub></form><legend id='EA8ta'></legend><bdo id='EA8ta'><pre id='EA8ta'><center id='EA8ta'></center></pre></bdo></b><th id='EA8ta'></th></span></q></dt></tr></i><div id='EA8ta'><tfoot id='EA8ta'></tfoot><dl id='EA8ta'><fieldset id='EA8ta'></fieldset></dl></div>
                      <bdo id='EA8ta'></bdo><ul id='EA8ta'></ul>

                  1. <small id='EA8ta'></small><noframes id='EA8ta'>

                  2. <tfoot id='EA8ta'></tfoot>

                    <legend id='EA8ta'><style id='EA8ta'><dir id='EA8ta'><q id='EA8ta'></q></dir></style></legend>