错误:尝试在空对象引用上调用虚方法

时间:2017-05-02 22:10:26

标签: android-studio

在运行我的应用程序时,我收到错误消息:尝试在空对象引用上调用虚方法。并且根据错误行,该方法是setVisibilty方法。 重要的代码是:

  

public class MainActivity extends ActionBarActivity {
>     private Button btn;
>         private Button btn1;
>         private Button btn3;
>     
>     
>     
>     tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager){
>                 @Override
>                 public void onTabSelected(TabLayout.Tab tab) {
>     
>                     switch (tab.getPosition()) {
>                         
>                         case 0:
>                             btn = (Button) findViewById(R.id.button15);
>     btn.setVisibility(View.GONE);
>                             break;
>     
>                         case 1:
>                             btn1 = (Button) findViewById(R.id.button16);
>                             btn1.setVisibility(View.GONE);
>                             break;
>     
>                         case 2:
>     btn3 = (Button) findViewById(R.id.button17);
>                             btn3.setVisibility(View.GONE);
>                             break;
>     
>     
>     
>                         }
>                     super.onTabSelected(tab);
>     
>                 }
>             }
>             );
>     }

error方法是setVisibilty方法。 任何帮助将受到高度赞赏。感谢

1 个答案:

答案 0 :(得分:0)

你可以更具体一点我试过那个完美适合我的代码可以让你对错误更加具体。这是我的工作代码。 addOnTabSelectedListener()可以替换为 setOnTabSelectedListener(),就像你的情况一样。

public class MainActivity extends ActionBarActivity {

    private Button btn;
    private Button btn1;
    private Button btn3;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                visibilityChange(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                visibilityChange(tab);
            }
        });
    }

    private void visibilityChange(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                btn = (Button) findViewById(R.id.button);
                if (btn.getVisibility() == View.GONE)
                    btn.setVisibility(View.VISIBLE);
                else
                    btn.setVisibility(View.GONE);
                break;

            case 1:
                btn1 = (Button) findViewById(R.id.button2);
                if (btn1.getVisibility() == View.GONE)
                    btn1.setVisibility(View.VISIBLE);
                else
                    btn1.setVisibility(View.GONE);
                break;

            case 2:
                btn3 = (Button) findViewById(R.id.button3);
                if (btn3.getVisibility() == View.GONE)
                    btn3.setVisibility(View.VISIBLE);
                else
                    btn3.setVisibility(View.GONE);
                break;
        }
    }
}