viewpager能运行,也提示错误,分析不错问题所在
澳门银河官网
澳门银河官网
当前位置 : 澳门银河官网 > 澳门银河官网下载

viewpager能运行,也提示错误,分析不错问题所在

这运行时正常的,就是一大段错误....能告诉我怎么回事吗?

package com.example.viewpagerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {

    private LinearLayout mWeiXin;
    private LinearLayout mFriend;
    private LinearLayout mContact;
    private LinearLayout mSetting;

    private ImageButton mImgWeixin;
    private ImageButton mlmgFriend;
    private ImageButton mImgContact;
    private ImageButton mImgSetting;

    private PagerAdapter adapter;
    private LayoutInflater mInflater;
    private List<View> views = new ArrayList<View>;
    private ViewPager viewpager;


    @Override
    protected void onCreateBundle savedInstanceState {
        super.onCreatesavedInstanceState;
        requestWindowFeatureWindow.FEATURE_NO_TITLE;
        setContentViewR.layout.activity_main;


        initView;
        viewpager.setAdapteradapter;
        initEvent;

    }

    private void initEvent {
        mWeiXin.setOnClickListenerthis;
        mContact.setOnClickListenerthis;
        mFriend.setOnClickListenerthis;
        mSetting.setOnClickListenerthis;

        viewpager.addOnPageChangeListenernew ViewPager.OnPageChangeListener {


            @Override
            public void onPageSelectedint position {

                resetImg;
                switch position {

                    case 0:
                        mImgWeixin.setImageResourceR.mipmap.tab_weixin_pressed;
                        break;

                    case 1:
                        mlmgFriend.setImageResourceR.mipmap.tab_find_frd_pressed;

                        break;

                    case 2:
                        mImgContact.setImageResourceR.mipmap.tab_address_pressed;

                        break;
                    case 3:
                        mImgSetting.setImageResourceR.mipmap.tab_settings_pressed;
                        break;

                }
            }

            @Override
            public void onPageScrolledint position, float positionOffset, int positionOffsetPixels {

            }

            @Override
            public void onPageScrollStateChangedint state {

            }
        };


    }

    private void initView {
        viewpager = ViewPager findViewByIdR.id.viewPager;
        mWeiXin = LinearLayout findViewByIdR.id.weixin;
        mFriend = LinearLayout findViewByIdR.id.Friend;
        mContact = LinearLayout findViewByIdR.id.Contact;
        mSetting = LinearLayout findViewByIdR.id.setting;

        mImgWeixin = ImageButton findViewByIdR.id.img_weixin;
        mImgContact = ImageButton findViewByIdR.id.img_Contact;
        mlmgFriend = ImageButton findViewByIdR.id.img_Friend;
        mImgSetting = ImageButton findViewByIdR.id.img_setting;
        mImgWeixin.setImageResourceR.mipmap.tab_weixin_pressed;

        mInflater = LayoutInflater.fromMainActivity.this;
        View view01 = mInflater.inflateR.layout.tab01, null;
        View view02 = mInflater.inflateR.layout.tab02, null;
        View view03 = mInflater.inflateR.layout.tab03, null;
        View view04 = mInflater.inflateR.layout.tab04, null;
        views.addview01;
        views.addview02;
        views.addview03;
        views.addview04;


        adapter = new PagerAdapter {
            @Override
            public int getCount {
                return views.size;
            }

            @Override
            public boolean isViewFromObjectView view, Object object {
                return view == object;
            }

            @Override
            public void destroyItemViewGroup container, int position, Object object {

                container.removeViewviews.getposition;

            }

            @Override
            public Object instantiateItemViewGroup container, int position {

                View view = views.getposition;
                container.addViewview;
                return view;
            }
        };
    }


    @Override
    public void onClickView view {

        resetImg;
        switch view.getId {

            case R.id.weixin:
                viewpager.setCurrentItem0;
                mImgWeixin.setImageResourceR.mipmap.tab_weixin_pressed;

                break;

            case R.id.Friend:
                viewpager.setCurrentItem1;

                mlmgFriend.setImageResourceR.mipmap.tab_find_frd_pressed;

                break;

            case R.id.Contact:
                viewpager.setCurrentItem2;
                mImgContact.setImageResourceR.mipmap.tab_address_pressed;


                break;

            case R.id.setting:
                viewpager.setCurrentItem3;
                mImgSetting.setImageResourceR.mipmap.tab_settings_pressed;
                break;
        }
    }

    private void resetImg {

        mImgWeixin.setImageResourceR.mipmap.tab_weixin_normal;
        mImgContact.setImageResourceR.mipmap.tab_address_normal;
        mlmgFriend.setImageResourceR.mipmap.tab_find_frd_normal;
        mImgSetting.setImageResourceR.mipmap.tab_settings_normal;

    }
}

错误信息:
01-09 23:55:06.437 7131-7131/com.example.viewpagerdemo E/AndroidRuntime: FATAL EXCEPTION: main

                                                                     Process: com.example.viewpagerdemo, PID: 7131
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.viewpagerdemo/com.example.viewpagerdemo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method void android.support.v4.view.ViewPager.addOnPageChangeListenerandroid.support.v4.view.ViewPager$OnPageChangeListener on a null object reference
                                                                         at android.app.ActivityThread.performLaunchActivityActivityThread.java:2368
                                                                         at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2430
                                                                         at android.app.ActivityThread.access$900ActivityThread.java:154
                                                                         at android.app.ActivityThread$H.handleMessageActivityThread.java:1321
                                                                         at android.os.Handler.dispatchMessageHandler.java:102
                                                                         at android.os.Looper.loopLooper.java:135
                                                                         at android.app.ActivityThread.mainActivityThread.java:5307
                                                                         at java.lang.reflect.Method.invokeNative Method
                                                                         at java.lang.reflect.Method.invokeMethod.java:372
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:904
                                                                         at com.android.internal.os.ZygoteInit.mainZygoteInit.java:699
                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method void android.support.v4.view.ViewPager.addOnPageChangeListenerandroid.support.v4.view.ViewPager$OnPageChangeListener on a null object reference
                                                                         at com.example.viewpagerdemo.MainActivity.initEventMainActivity.java:56
                                                                         at com.example.viewpagerdemo.MainActivity.onCreateMainActivity.java:42
                                                                         at android.app.Activity.performCreateActivity.java:6865
                                                                         at android.app.Instrumentation.callActivityOnCreateInstrumentation.java:1106
                                                                         at android.app.ActivityThread.performLaunchActivityActivityThread.java:2321
                                                                         at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2430 
                                                                         at android.app.ActivityThread.access$900ActivityThread.java:154 
                                                                         at android.app.ActivityThread$H.handleMessageActivityThread.java:1321 
                                                                         at android.os.Handler.dispatchMessageHandler.java:102 
                                                                         at android.os.Looper.loopLooper.java:135 
                                                                         at android.app.ActivityThread.mainActivityThread.java:5307 
                                                                         at java.lang.reflect.Method.invokeNative Method 
                                                                         at java.lang.reflect.Method.invokeMethod.java:372 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:904 
                                                                         at com.android.internal.os.ZygoteInit.mainZygoteInit.java:699 

01-09 23:57:39.085 10770-10770/com.example.viewpagerdemo E/AndroidRuntime: FATAL EXCEPTION: main

                                                                       Process: com.example.viewpagerdemo, PID: 10770
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.viewpagerdemo/com.example.viewpagerdemo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method void android.support.v4.view.ViewPager.addOnPageChangeListenerandroid.support.v4.view.ViewPager$OnPageChangeListener on a null object reference
                                                                           at android.app.ActivityThread.performLaunchActivityActivityThread.java:2368
                                                                           at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2430
                                                                           at android.app.ActivityThread.access$900ActivityThread.java:154
                                                                           at android.app.ActivityThread$H.handleMessageActivityThread.java:1321
                                                                           at android.os.Handler.dispatchMessageHandler.java:102
                                                                           at android.os.Looper.loopLooper.java:135
                                                                           at android.app.ActivityThread.mainActivityThread.java:5307
                                                                           at java.lang.reflect.Method.invokeNative Method
                                                                           at java.lang.reflect.Method.invokeMethod.java:372
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:904
                                                                           at com.android.internal.os.ZygoteInit.mainZygoteInit.java:699
                                                                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method void android.support.v4.view.ViewPager.addOnPageChangeListenerandroid.support.v4.view.ViewPager$OnPageChangeListener on a null object reference
                                                                           at com.example.viewpagerdemo.MainActivity.initEventMainActivity.java:56
                                                                           at com.example.viewpagerdemo.MainActivity.onCreateMainActivity.java:42
                                                                           at android.app.Activity.performCreateActivity.java:6865
                                                                           at android.app.Instrumentation.callActivityOnCreateInstrumentation.java:1106
                                                                           at android.app.ActivityThread.performLaunchActivityActivityThread.java:2321
                                                                           at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2430 
                                                                           at android.app.ActivityThread.access$900ActivityThread.java:154 
                                                                           at android.app.ActivityThread$H.handleMessageActivityThread.java:1321 
                                                                           at android.os.Handler.dispatchMessageHandler.java:102 
                                                                           at android.os.Looper.loopLooper.java:135 
                                                                           at android.app.ActivityThread.mainActivityThread.java:5307 
                                                                           at java.lang.reflect.Method.invokeNative Method 
                                                                           at java.lang.reflect.Method.invokeMethod.java:372 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:904 
                                                                           at com.android.internal.os.ZygoteInit.mainZygoteInit.java:699 
请输入代码

好神奇,第二天重新打开电脑,重新运行,就没这个问题.....

把XML也贴一下

广告位

澳门银河官网