ViewPager致命异常:主要

时间:2018-06-30 15:17:08

标签: java android android-studio android-viewpager runtime-error

我用片段创建了ViewPager。但是,当我在本活动中切换到另一个片段时,应用程序崩溃了。调试器显示了很长的错误列表。当我点击页面标题时,该应用程序立即崩溃,但是当我滑动它时,该应用程序在多次滑动后崩溃。这是我的应用程序中的调试控制台日志,Java和XML代码。如果您需要查看更多代码,请在注释中写出内容。

调试控制台

E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                        at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:392)
                        at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:387)
                        at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:104)
                        at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1004)
                        at android.support.v4.view.ViewPager.populate(ViewPager.java:1218)
                        at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
                        at android.support.v4.view.ViewPager$3.run(ViewPager.java:267)
                        at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1999)
                        at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:2129)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1965)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2386)
                        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1730)
                        at android.app.Activity.dispatchTouchEvent(Activity.java:2757)
                        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
                        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
                        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2347)
                        at android.view.View.dispatchPointerEvent(View.java:8681)
                        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
                        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
                        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
                        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
                        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
                        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
                        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5817)
                        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5791)
                        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5762)
                        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5907)
                        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                        at android.os.MessageQueue.nativePollOnce(Native Method)
                        at android.os.MessageQueue.next(MessageQueue.java:143)
                        at android.os.Looper.loop(Looper.java:122)
                        at android.app.ActivityThread.main(ActivityThread.java:5345)
                        at java.lang.reflect.Method.invoke(Native Method)
                        at java.lang.r
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.chatting, PID: 13816
                  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                      at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:392)
                      at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:387)
                      at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:104)
                      at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1004)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1218)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
                      at android.support.v4.view.ViewPager$3.run(ViewPager.java:267)
                      at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1999)
                      at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:2129)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1965)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2438)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2060)
                      at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2386)
                      at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1730)
                      at android.app.Activity.dispatchTouchEvent(Activity.java:2757)
                      at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
                      at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
                      at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2347)
                      at android.view.View.dispatchPointerEvent(View.java:8681)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
                      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
                      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
                      at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5817)
                      at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5791)
                      at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5762)
                      at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5907)
                      at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                      at android.os.MessageQueue.nativePollOnce(Native Method)
                      at android.os.MessageQueue.next(MessageQueue.java:143)
                      at android.os.Looper.loop(Looper.java:122)
                      at android.app.ActivityThread.main(ActivityThread.java:5345)
                    at

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private android.support.v7.widget.Toolbar mToolbar;
    private FirebaseAuth mAuth;

    private ViewPager myViewPager;
    private TabLayout myTabLayout;
    private TabsPagerAdapter myTabsPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAuth = FirebaseAuth.getInstance();

        //Tabs for MainActivity
        myViewPager = (ViewPager) findViewById(R.id.main_tabs_pager);
        myTabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        myViewPager.setAdapter(myTabsPagerAdapter);
        myTabLayout = (TabLayout) findViewById(R.id.main_tabs);
        myTabLayout.setupWithViewPager(myViewPager);


        mToolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.main_page_toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("Chatting");
    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseUser currentUser = mAuth.getCurrentUser();

        if (currentUser == null){

            LogOutUser();
        }
    }

    private void LogOutUser() {

        Intent startPageIntent = new Intent(MainActivity.this, StartPageActivity.class);
        startPageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(startPageIntent);
        finish();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        if (item.getItemId() == R.id.main_logout_button){

            mAuth.signOut();

            LogOutUser();
        }

        return true;
    }
}

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include
            layout="@layout/app_bar_layout"
            android:id="@+id/main_page_toolbar">
        </include>

        <android.support.design.widget.TabLayout
            android:id="@+id/main_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        </android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/main_tabs_pager"
        android:layout_width="match_parent"
        android:layout_height="464dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"

        android:layout_alignParentLeft="true"></android.support.v4.view.ViewPager>


</RelativeLayout>

TabsPagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

class TabsPagerAdapter extends FragmentPagerAdapter {



    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        switch (position){
            case 0:
                RequestsFragment requestsFragment = new RequestsFragment();
                return requestsFragment;

            case 1:
                ChatsFragment chatsFragment = new ChatsFragment();
                return chatsFragment;

            case 3:
                FriendsFragment friendsFragment = new FriendsFragment();
                return friendsFragment;

            default:
                return null;

        }

    }

    @Override
    public int getCount() {
        return 3;
    }

    public CharSequence getPageTitle(int position){

        switch (position){

            case 0:
                return "Requests";

            case 1:
                return "Chats";

            case 2:
                return "Friends";

            default:
                return null;

        }
    }

}

1 个答案:

答案 0 :(得分:1)

@Override
    public Fragment getItem(int position) {

        switch (position){
            case 0:
                RequestsFragment requestsFragment = new RequestsFragment();
                return requestsFragment;

            case 1:
                ChatsFragment chatsFragment = new ChatsFragment();
                return chatsFragment;

            case 3:
                FriendsFragment friendsFragment = new FriendsFragment();
                return friendsFragment;

            default:
                return null;

        }

我的朋友,情况2在哪里?并且在默认情况下不要返回null。只需返回任何一个声明的片段。

@Override
    public int getCount() {
        return 3;
    }

将使其运行三次。对于0,1和2位置,但对于情况2,将返回默认片段,该片段为null。导致空指针异常。

case 3:
                    FriendsFragment friendsFragment = new FriendsFragment();
                    return friendsFragment;

将此更改为

case 2:
                    FriendsFragment friendsFragment = new FriendsFragment();
                    return friendsFragment;