问题描述
- 新手请教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) { } }
}