bugfix> java > 投稿

フラグメントを使用して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 件
  • @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は私の友人ですか?そして、デフォルト 場合。宣言されたフラグメントのいずれかを返すだけです。

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

    3回実行されます。 0、1、および2ポジションの場合、ただしケース2の場合、nullのデフォルトフラグメントが返されます。 NULLポインター例外が発生します。

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

    これを変更する

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

あなたの答え