致命异常:主要java.lang.NullPointerException @ onPageSelected

时间:2013-12-18 16:27:48

标签: java android nullpointerexception

尝试连接stringArray / ViewPager后,我收到致命异常/空指针异常。我不确定到底出了什么问题(或者我是否应该对我在logcat中出现的Unexpected value from nativeGetEnabledTags: 0付出任何心思。

如果需要任何其他信息,请告知我们。

logcat的:

12-18 10:49:11.602: W/Trace(19113): Unexpected value from nativeGetEnabledTags: 0
12-18 10:49:11.602: W/Trace(19113): Unexpected value from nativeGetEnabledTags: 0
12-18 10:49:11.603: W/Trace(19113): Unexpected value from nativeGetEnabledTags: 0
12-18 10:49:11.603: E/InputEventReceiver(19113): Exception dispatching input event.
12-18 10:49:11.603: E/MessageQueue-JNI(19113): Exception in MessageQueue callback: handleReceiveCallback
12-18 10:49:11.615: E/MessageQueue-JNI(19113): java.lang.NullPointerException
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at com.idg.omv.ui.phone.Home$2.onPageSelected(Home.java:116)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:567)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2015)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.View.dispatchTouchEvent(View.java:7255)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2235)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1932)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1968)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1420)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.app.Activity.dispatchTouchEvent(Activity.java:2441)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1916)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.View.dispatchPointerEvent(View.java:7435)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3610)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3538)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4790)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4750)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4902)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.os.MessageQueue.nativePollOnce(Native Method)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.os.MessageQueue.next(MessageQueue.java:125)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.os.Looper.loop(Looper.java:140)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at android.app.ActivityThread.main(ActivityThread.java:5297)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at java.lang.reflect.Method.invokeNative(Native Method)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at java.lang.reflect.Method.invoke(Method.java:511)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-18 10:49:11.615: E/MessageQueue-JNI(19113):  at dalvik.system.NativeStart.main(Native Method)
12-18 10:49:11.615: D/AndroidRuntime(19113): Shutting down VM
12-18 10:49:11.615: W/dalvikvm(19113): threadid=1: thread exiting with uncaught exception (group=0x412ca9a8)
12-18 10:49:11.619: E/AndroidRuntime(19113): FATAL EXCEPTION: main
12-18 10:49:11.619: E/AndroidRuntime(19113): java.lang.NullPointerException
12-18 10:49:11.619: E/AndroidRuntime(19113):    at com.idg.omv.ui.phone.Home$2.onPageSelected(Home.java:116)
12-18 10:49:11.619: E/AndroidRuntime(19113):    at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:567)
12-18 10:49:11.619: E/AndroidRuntime(19113):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
12-18 10:49:11.619: E/AndroidRuntime(19113):    at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2015)

第116行是.execute();这里:

new GetYouTubeUserVideosTask(responseHandler, playlist1)
                    .execute();

JAVA:

public class Home extends YouTubeBaseActivity implements

VideoClickListener {

    private VideosListView listView;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    private int mCurrentTabPosition = NO_CURRENT_POSITION;
    private static final int NO_CURRENT_POSITION = -1;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private String[] drawerListViewItems;

    ScrollView mainScrollView;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    String TAG = "DEBUG THIS";
    String playlist = "eminemvevo";
    Activity activity;
    int imageArray[];
    String[] stringArray;
    String runPrevious = "No";
    private OnPageChangeListener mPageChangeListener;
    ImagePagerAdapter adapter = new ImagePagerAdapter();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        final ActionBar actionBar = getActionBar();
        final ViewPager mPager = (ViewPager) findViewById(R.id.view_pager);
        adapter.notifyDataSetChanged();
        mPager.setAdapter(adapter);
        actionBar.setCustomView(R.layout.actionbar_custom_view_home);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        drawerListViewItems = getResources().getStringArray(R.array.items);
        drawerListView = (ListView) findViewById(R.id.left_drawer);
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));
        mPager.setOnPageChangeListener(mPageChangeListener);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, 
                drawerLayout,
                R.drawable.ic_drawer, 
                R.string.drawer_open, 
                R.string.drawer_close 
                );
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        _initMenu();
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        listView = (VideosListView) findViewById(R.id.videosListView);
        listView.setOnVideoClickListener(this);

        new GetYouTubeUserVideosTask(responseHandler, playlist).execute();

        mPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int pos) {

                    String playlist1 = stringArray[pos];
                    new GetYouTubeUserVideosTask(responseHandler, playlist1)
                    .execute();

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int pos) {

            }
    });

        mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
                position = mCurrentTabPosition;
                int oldPos = mPager.getCurrentItem();

                Log.d("PK", "Page selected");

                if (position > oldPos) {

                    System.out.print(position);
                    // Moving to the right
                    String playlist = "TheMozARTGROUP‎";
                    new GetYouTubeUserVideosTask(responseHandler, playlist)
                    .execute();

                } else if (position < oldPos) {
                    // Moving to the Left
                    System.out.print(position);
                    String playlist = "TheMozARTGROUP‎";
                    new GetYouTubeUserVideosTask(responseHandler, playlist)
                    .execute();

                }
                mPager.setOnPageChangeListener(mPageChangeListener);
            }

            private void onTabChanged(PagerAdapter adapter,
                    int mCurrentTabPosition, int position) {

                Log.d("PK", "Tab changed");
                // TODO Auto-generated method stub

            }
        };
    }

    private class MyActivityGetYouTubeUserVideosTask extends
    GetYouTubeUserVideosTask {
        public MyActivityGetYouTubeUserVideosTask(Handler replyTo,
                String username) {
            super(replyTo, username);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onPostExecute(Void result) {
            if (result != null) {
                // TODO update your list data
                adapter.notifyDataSetChanged();
            }

        }
    }

    private void _initMenu() {

        // drawerListView.setOnItemClickListener(new DrawerItemClickListener());

    }

    Handler responseHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            populateListWithVideos(msg);
        };
    };

    private void populateListWithVideos(Message msg) {
        Library lib = (Library) msg.getData().get(
                GetYouTubeUserVideosTask.LIBRARY);
        listView.setVideos(lib.getVideos());

    }

    @Override
    protected void onStop() {
        responseHandler = null;
        super.onStop();
    }

    @Override
    public void onVideoClicked(Video video) {
        Intent intent = new Intent(this, Player.class);
        intent.putExtra("id", video.getId());
        intent.putExtra("title", video.getTitle());
        intent.putExtra("uploader", video.getUploader());
        intent.putExtra("viewCount", video.getviewCount());
        startActivity(intent);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class ImagePagerAdapter extends PagerAdapter implements
    ViewPager.OnPageChangeListener {
        public ImagePagerAdapter(Activity act, int[] mImages,
                String[] stringArra) {
            imageArray = mImages;
            activity = act;
            stringArray = stringArra;
        }

        public ImagePagerAdapter() {
            super();

        }

        private int[] mImages = new int[] { R.drawable.selstation_up_btn,
                R.drawable.classical_up_btn, R.drawable.country_up_btn,
                R.drawable.dance_up_btn, R.drawable.hiphop_up_btn,
                R.drawable.island_up_btn, R.drawable.latin_up_btn,
                R.drawable.pop_up_btn, R.drawable.samba_up_btn };

        private String[] stringArray = new String[] { "vevo",
                "TheMozARTGROUP‎", "TimMcGrawVEVO‎", "TiestoVEVO‎",
        "EminemVEVO‎" };

        @Override
        public int getCount() {
            return mImages.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((ImageView) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = Home.this;
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }

        private class DrawerItemClickListener implements
        ListView.OnItemClickListener {

            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {

                Log.d("pk", "onItemClick");
                // Highlight the selected item, update the title, and close the
                // drawer
                // update selected item and title, then close the drawer
                drawerListView.setItemChecked(position, true);
                setTitle("......");

                String text = "menu click... should be implemented";
                // Toast.makeText(Home.this, text, Toast.LENGTH_LONG).show();
                drawerLayout.closeDrawer(drawerListView);

            }

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
            Log.d("pk", "onPageScrollStateChanged");
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
            Log.d("pk", "onPageScrolled");
        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            Log.d("pk", "onPageSelected");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

String[] stringArray; // is not initialized

和这个

private String[] stringArray = new String[] { "vevo",
                "TheMozARTGROUP‎", "TimMcGrawVEVO‎", "TiestoVEVO‎",
        "EminemVEVO‎" };

ImageAdapter。可能是你想要将其移出ImageAdapter

所以我相信playlist1为空。您还将playList声明为实例变量。

答案 1 :(得分:0)

你确定,第116行之后的行

mPager.setOnPageChangeListener(mPageChangeListener);

在这里正确吗?它似乎属于mPageChangeListener的定义,对我来说看起来很奇怪。