01-22 01:23:35.234 25771-25771/com.example.dell_user.wechat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell_user.wechat, PID: 25771
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell_user.wechat/com.example.dell_user.wechat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.ImageView.getLayoutParams()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2774)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1588)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5955)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:930)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:791)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.ImageView.getLayoutParams()' on a null object reference
at com.example.dell_user.wechat.MainActivity.initLineImage(MainActivity.java:93)
at com.example.dell_user.wechat.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6378)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1124)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2774)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1588)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5955)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:930)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:791)
at com.example.dell_user.wechat.MainActivity.initLineImage(MainActivity.java:93)
使用了null空对象。导致异常了。看看这行代码
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.ImageView.getLayoutParams()' on a null object reference
at com.example.dell_user.wechat.MainActivity.initLineImage(MainActivity.java:93)
at com.example.dell_user.wechat.MainActivity.onCreate(MainActivity.java:40)
第93行有问题,仔细检查一下
public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener,View.OnClickListener {
ViewPager viewPager;
PagerAdapter pagerAdapter;
private List titleList;
private TextView tv1,tv2,tv3,tv4;
private ImageView tab;
private int moveOne =0;
private boolean isScrolling=false;
private boolean isBackScolling=false; //手指离开后的回弹
private long starTime =0;
private long currentTime =0 ;
Listfrag =new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager= (ViewPager) findViewById(R.id.viewpage);
initLineImage();
frag.add(new fragment());
frag.add(new fragment2());
frag.add(new fragment3());
frag.add(new fragment4());
pagerAdapter=new MyPagerAdapter(getSupportFragmentManager(),frag) ;
viewPager.setAdapter(pagerAdapter);
tv1= (TextView) findViewById(R.id.tv1);
tv2= (TextView) findViewById(R.id.tv2);
tv3= (TextView) findViewById(R.id.tv3);
tv4= (TextView) findViewById(R.id.tv4);
viewPager.setCurrentItem(0);
tv1.setTextColor(Color.RED);
tv2.setTextColor(Color.GREEN);
tv3.setTextColor(Color.BLACK);
tv4.setTextColor(Color.BLACK);
viewPager.addOnPageChangeListener(MainActivity.this);
tab= (ImageView) findViewById(R.id.tab);
}
private void movePositionX(int toPostion ,float positionOffestPiels) { //下划线跟随手指的滑动而移动
float curTranslationX=tab.getTranslationX(); //当钱的translationX是的到滑块的位置
float toPostionX=moveOne*toPostion + positionOffestPiels; //
ObjectAnimator animator=ObjectAnimator. ofFloat(tab,"translationX",curTranslationX,toPostionX);
animator.setDuration(500);
animator.start();
}
private void movePositionX(int toPostion){
movePositionX(toPostion,0);
}
private void initLineImage(){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenw = dm.widthPixels;
LayoutParams lp = tab.getLayoutParams();
lp.width = screenw / 4;
tab.setLayoutParams(lp);
moveOne = lp.width;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { //position是偏移量的百分比,positionOffsetPixel是偏移量
currentTime=System.currentTimeMillis();
if (isScrolling&&(currentTime-starTime)>200){
movePositionX(position,moveOne*positionOffset); //moveone 是滑动到第一个界面的距离
starTime=currentTime;
}
if (isBackScolling){
movePositionX(position);
}
}
@Override
public void onPageSelected(int position) { //当界面转换时调用该方法
switch (position) {
case 0:
tv1.setTextColor(Color.BLACK);
tv2.setTextColor(Color.RED);
tv3.setTextColor(Color.GREEN);
tv4.setTextColor(Color.GRAY);
movePositionX(0);
break;
case 1:
tv1.setTextColor(Color.RED);
tv2.setTextColor(Color.BLACK);
tv3.setTextColor(Color.BLUE);
tv4.setTextColor(Color.BLACK);
movePositionX(1);
break;
case 2:
tv1.setTextColor(Color.BLACK);
tv2.setTextColor(Color.BLACK);
tv3.setTextColor(Color.BLUE);
tv4.setTextColor(Color.BLACK);
movePositionX(2);
break;
case 3:
tv1.setTextColor(Color.BLACK);
tv2.setTextColor(Color.BLACK);
tv3.setTextColor(Color.GRAY);
tv4.setTextColor(Color.BLACK);
movePositionX(3);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) { //判断手指什么时候滑动什么时候停止滑动
switch (state){
case 1:
isScrolling=true;
isBackScolling=false; //isBackScolling是表示手指离开
break;
case 2:
isBackScolling=true;
isScrolling=false;
break;
default:
isScrolling=false;
isBackScolling=false;
break;
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt1:
viewPager.setCurrentItem(0);
break;
case R.id.bt2:
viewPager.setCurrentItem(1);
break;
case R.id.bt3:
viewPager.setCurrentItem(2);
break;
case R.id.bt4:
viewPager.setCurrentItem(3);
break;
default:
break;
}
}
}
Mainactivity的代码
爆了空指针异常
MainActivity.java:93
LayoutParams lp = tab.getLayoutParams();出错
通过日志可知
Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.ImageView.getLayoutParams()' on a null objectreference
大意是:getLayoutParams()返回的是一个抽象对象ViewGroup的属性LayoutParams 必须返回ViewGroup的子类的LayoutParams
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tab.getLayoutParams();
tab= (ImageView) findViewById(R.id.tab);这行代码提前,initLineImage();
这个方法里面你都对tab操作了,那个时候tab还没有初始化
像这种nullponit的话是很好找的,从报错地方按照程序执行的相反顺序找他之前的操作就行了