Android:未分配部分页面适配器

时间:2018-04-20 17:34:05

标签: android debugging

我有以下代码:

package com.example.me.echoboard;

import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private SectionsPageAdapter mSectionsPageAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
        mViewPager = findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    }

    private void setupViewPager(ViewPager viewPager){
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Tab1Fragment(), "Message Board");
        adapter.addFragment(new Tab2Fragment(), "Statistics");
        adapter.addFragment(new Tab3Fragment(), "My Notes");
        viewPager.setAdapter(adapter);
    }
}

我以某种方式破坏了我的项目。调试的错误是在最后一行,我将viewPager设置为适配器,它应该是null。

IDE确实证实了这一点。当将鼠标悬停在private SectionsPageAdapter mSectionsPageAdapter;中的mSectionsPageAdapter上时,我被告知它已被分配但从未被访问过(这对于它为何为空是有意义的)。但是,这显然是不正确的,正在分配mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

我看不出问题出在哪里。同样,代码的结构很好,它完美地工作,直到我开始使用一些数据可视化工具,然后出现错误消息。

有没有人知道从哪里开始寻找?

谢谢!

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.me.echoboard, PID: 16132
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.me.echoboard/com.example.me.echoboard.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:2924)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
                      at android.app.ActivityThread.-wrap14(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6692)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                   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 com.example.me.echoboard.MainActivity.setupViewPager(MainActivity.java:31)
                      at com.example.me.echoboard.MainActivity.onCreate(MainActivity.java:20)
                      at android.app.Activity.performCreate(Activity.java:6912)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) 
                      at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6692) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

1 个答案:

答案 0 :(得分:1)

mSectionsPageAdapter未使用的作业问题虽然属实,但却是一个红色的鲱鱼。根据您发布的异常文本,在将该适配器分配给ViewPager期间会抛出NullPointerException,这意味着ViewPager的引用为空。

该null引用的起源位于mViewPager = findViewById(R.id.container);行,因此请在布局文件中确保该视图寻呼机的ID确实为id/container