android 导航栏-新手请教android底部导航栏问题

问题描述

新手请教android底部导航栏问题

andriod底部导航栏每当移到第一个时就报错,出现空指针异常,希望哪位大神能指教一下,到底哪错了
错误信息:
09-29 22:09:21.098: E/AndroidRuntime(863): java.lang.NullPointerException
09-29 22:09:21.098: E/AndroidRuntime(863): at com.example.producer.MainActivity$MyOnPageChangeListener.onPageSelected(MainActivity.java:240)
java文件:
package com.example.producer;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends Activity {

public static MainActivity instance = null;

private ViewPager mTabPager;
private ImageView mTabImg;// 动画图片
private ImageView mTab1,mTab2,mTab3,mTab4,mTab5;
private int currIndex = 0;// 当前页卡编号
private int zero = 0;// 动画图片偏移量
private int one;//单个水平动画位移
private int two;
private int three;
private int four;
private LinearLayout mClose;
private LinearLayout mCloseBtn;
private View layout;
private boolean menu_display = false;
private PopupWindow menuWindow;
private LayoutInflater inflater;
//private Button mRightBtn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     //启动activity时不自动弹出软键盘
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    instance = this;
    /*
    mRightBtn = (Button) findViewById(R.id.right_btn);
    mRightBtn.setOnClickListener(new Button.OnClickListener()
    {   @Override
        public void onClick(View v)
        {   showPopupWindow (MainWeixin.this,mRightBtn);
        }
      });*/

    mTab1 = (ImageView) findViewById(R.id.img_weixin);
    mTab2 = (ImageView) findViewById(R.id.img_address);
    mTab3 = (ImageView) findViewById(R.id.img_friends);
    mTab4 = (ImageView) findViewById(R.id.img_settings);
    mTab5 = (ImageView) findViewById(R.id.img_settings2);
    mTabImg = (ImageView) findViewById(R.id.img_tab_now);
    mTab1.setOnClickListener(new MyOnClickListener(0));
    mTab2.setOnClickListener(new MyOnClickListener(1));
    mTab3.setOnClickListener(new MyOnClickListener(2));
    mTab4.setOnClickListener(new MyOnClickListener(3));
    mTab5.setOnClickListener(new MyOnClickListener(4));
    Display currDisplay = getWindowManager().getDefaultDisplay();//获取屏幕当前分辨率
    int displayWidth = currDisplay.getWidth();
    int displayHeight = currDisplay.getHeight();
    one = displayWidth/5; //设置水平动画平移大小
    two = one*2;
    three = one*3;
    four = one*4;
    //Log.i("info", "获取的屏幕分辨率为" + one + two + three + "X" + displayHeight);
    mTabPager = (ViewPager)findViewById(R.id.tabpager);
    mTabPager.setOnPageChangeListener(new MyOnPageChangeListener());
    //InitImageView();//使用动画
  //将要分页显示的View装入数组中
    LayoutInflater mLi = LayoutInflater.from(this);
    View view1 = mLi.inflate(R.layout.test2, null);
    View view2 = mLi.inflate(R.layout.trip, null);
    View view3 = mLi.inflate(R.layout.firstpage, null);
    View view4 = mLi.inflate(R.layout.activity_help, null);
    View view5 = mLi.inflate(R.layout.home_page, null);

  //每个页面的view数据
    final ArrayList<View> views = new ArrayList<View>();
    views.add(view1);
    views.add(view2);
    views.add(view3);
    views.add(view4);
    views.add(view5);
  //填充ViewPager的数据适配器
    PagerAdapter mPagerAdapter = new PagerAdapter() {

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager)container).removeView(views.get(position));
        }

        //@Override
        //public CharSequence getPageTitle(int position) {
            //return titles.get(position);
        //}

        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager)container).addView(views.get(position));
            return views.get(position);
        }
    };

    mTabPager.setAdapter(mPagerAdapter);
}
/**
 * 头标点击监听
 */
public class MyOnClickListener implements View.OnClickListener {
    private int index = 0;

    public MyOnClickListener(int i) {
        index = i;
    }
    public void onClick(View v) {
        mTabPager.setCurrentItem(index);
    }
};

 /* 页卡切换监听(原作者:D.Winter)
 */
public class MyOnPageChangeListener implements OnPageChangeListener {
    public void onPageSelected(int arg0) {
        Animation animation = null;
        switch (arg0) {
        case 0:
            mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_pressed));
            if (currIndex == 1) {
                animation = new TranslateAnimation(one, 0, 0, 0);
                mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
            } else if (currIndex == 2) {
                animation = new TranslateAnimation(two, 0, 0, 0);
                mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
            }
            else if (currIndex == 3) {
                animation = new TranslateAnimation(three, 0, 0, 0);
                mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
            }
            else if (currIndex == 5) {
                animation = new TranslateAnimation(three, four, 0, 0);
                mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
            }
            break;
        case 1:
            mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_pressed));
            if (currIndex == 0) {
                animation = new TranslateAnimation(zero, one, 0, 0);
                mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
            } else if (currIndex == 2) {
                animation = new TranslateAnimation(two, one, 0, 0);
                mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
            }
            else if (currIndex == 3) {
                animation = new TranslateAnimation(three, one, 0, 0);
                mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
            }
            else if (currIndex == 5) {
                animation = new TranslateAnimation(one, four, 0, 0);
                mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
            }
            break;
        case 2:
            mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_pressed));
            if (currIndex == 0) {
                animation = new TranslateAnimation(zero, two, 0, 0);
                mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
            } else if (currIndex == 1) {
                animation = new TranslateAnimation(one, two, 0, 0);
                mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
            }
            else if (currIndex == 3) {
                animation = new TranslateAnimation(three, two, 0, 0);
                mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
            }
            else if (currIndex == 5) {
                animation = new TranslateAnimation(two, four, 0, 0);
                mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
            }
            break;
        case 3:
            mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_pressed));
            if (currIndex == 0) {
                animation = new TranslateAnimation(zero, three, 0, 0);
                mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
            } else if (currIndex == 1) {
                animation = new TranslateAnimation(one, three, 0, 0);
                mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
            }
            else if (currIndex == 2) {
                animation = new TranslateAnimation(two, three, 0, 0);
                mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
            }
            else if (currIndex == 5) {
                animation = new TranslateAnimation(three, four, 0, 0);
                mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
            }
            break;
        case 4:
            mTab5.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_pressed));
            if (currIndex == 0) {
                animation = new TranslateAnimation(zero, four, 0, 0);
                mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
            } else if (currIndex == 1) {
                animation = new TranslateAnimation(one, four, 0, 0);
                mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
            }
            else if (currIndex == 2) {
                animation = new TranslateAnimation(two, four, 0, 0);
                mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
            }
            else if (currIndex == 3) {
                animation = new TranslateAnimation(three, four, 0, 0);
                mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
            }
            break;
        }

// currIndex = arg0;
animation.setFillAfter(true);// True:图片停在动画结束位置
animation.setDuration(200);//检索或设置对象动画的持续时间
mTabImg.startAnimation(animation);
}

    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }
    public void onPageScrollStateChanged(int arg0) {
    }
}

}

解决方案

不错,不错,学习到了很多

时间: 2024-10-30 21:27:49

android 导航栏-新手请教android底部导航栏问题的相关文章

Android程序开发之Fragment实现底部导航栏实例代码_Android

流行的应用的导航一般分为两种,一种是底部导航,一种是侧边栏. 说明 IDE:AS,Android studio; 模拟器:genymotion; 实现的效果,见下图. 具体实现 为了讲明白这个实现过程,我们贴出来的代码多一写,这样更方便理解 [最后还会放出完整的代码实现] .看上图的界面做的比较粗糙,但实现过程的骨架都具有了,想要更完美的设计,之后自行完善吧 ^0^. 布局 通过观察上述效果图,发现任意一个选项页面都有三部分组成: 顶部去除ActionBar后的标题栏: 中间一个Fragment

Android程序开发之Fragment实现底部导航栏实例代码

流行的应用的导航一般分为两种,一种是底部导航,一种是侧边栏. 说明 IDE:AS,Android studio; 模拟器:genymotion; 实现的效果,见下图. 具体实现 为了讲明白这个实现过程,我们贴出来的代码多一写,这样更方便理解 [最后还会放出完整的代码实现] .看上图的界面做的比较粗糙,但实现过程的骨架都具有了,想要更完美的设计,之后自行完善吧 ^0^. 布局 通过观察上述效果图,发现任意一个选项页面都有三部分组成: 顶部去除ActionBar后的标题栏: 中间一个Fragment

Android design包自定义tablayout的底部导航栏的实现方法

以前做项目大多用的radiobutton,今天用tablayout来做一个tab切换页面的的效果. 实现的效果就是类似QQ.微信的页面间(也就是Fragment间)的切换.如图: 布局只要一个tablayout <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id=&

Android仿微信页面底部导航效果代码实现_Android

大家在参考本地代码的时候要根据需要适当的修改,里面有冗余代码小编没有删除.好了,废话不多说了,一切让代码说话吧! 关键代码如下所示: .java里面的主要代码 public class MainActivity extends BaseActivity implements TabChangeListener { private Fragment[] fragments; private FragZaiXianYuYue fragZaiXianYuYue; private FragDaoLuJi

Android带数字或红点的底部导航拦和联网等待加载动画示例

Android带数字或红点的底部导航拦和联网等待加载动画 首先展示一下截图效果,下载地址在文章最后 一.Android带红点的底部导航拦 1.首先写底部导航栏的界面view_main_tab.xml. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" and

FragmentTabHost FrameLayout实现底部导航栏

app经常用到底部导航栏,早前使用过RadioGroup+FrameLayout实现或者RadioGroup+ViewPager实现,现在基本使用FragmentTabHost+FrameLayout来实现,因为使用起来简单易用.下面写一个小例子简要地总结一下这个组合. 首先,看一下例子的最终运行效果图 这5个图标的效果其实都是一样的,只要做出来一个,以此类推就可以写出其他几个 第一步, FragmentTabHost+FrameLayout布局,先看一下代码: <?xml version=&quo

浅谈主导航、次导航和底部导航的优化设置方法

导航是网站的指南针,是用户快速寻找所需信息的快捷入口.一个网站中能否留住用户,首先看导航的设置是否符合用户浏览习惯及是否足够的清晰,让用户一目了然的了解全网站相关内容.当然在网站优化中,导航同样占据着不可忽视的作用.一个导航是否利于蜘蛛爬行则影响着网站内页是否被收录,所以,网站导航的设置在优化中的重要性可想而知了.而且一个网站中分别存在三个导航,怎么去设置这些导航才利于SEO优化呢?今天笔者来谈谈自己的经验心得. 一.主导航(菜单栏) 首先主导航其实就是咱们常说的菜单栏,这个导航条在网站中占据的

三步搞定android应用底部导航栏

很多android应用底部都有一个底部导航栏,方便用户在使用过程中随意切换.目前常用的做法有三种:一种是使用自定义tabHost,一种是使用activityGroup,一种是结合FrameLayout实现.笔者再做了多款应用后,为了节约开发周期,封装了一个抽象类,只要三步便可完成底部栏的生成及不同页面的调用. public class ActivitycollectiondemoActivity extends ActivityCollection { /** Called when the a

Android底部导航栏实现(三)之TextView+LinearLayout

这里简单记录下通过TextView+LinearLayout+Fragment来实现Android底部导航栏. 布局 <!--fragment_text_tab.xml-->    <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"