切换标签布局/片段时Android应用崩溃

时间:2018-11-01 20:56:05

标签: java android firebase android-fragments android-recyclerview

我是Android开发的新手,遇到了问题。我的应用程序崩溃了,我有2个活动,分别是Home和Main,Main活动有4个片段,当我从第一个切换到第二个时,它正在工作,但是当我切换到第三个和/或第四个时,应用程序又回到了我家庭活动,但该应用程序不会引发错误,因此基本上我对正在发生的事情一无所知。

我正在第四个选项卡/片段中填充一个RecyclerView,这是我不明白的,如果数据库中的表为空或没有东西要扔到RecyclerView中,则这些选项卡正在工作,并且我可以切换这四个片段中的任何一个但是如果数据库有一个值抛出该问题,即在发生问题时将其填充到RecyclerView中,则它将返回到“家庭活动”,就像崩溃了一样,但它没有关闭应用程序

主要活动

    if (mAuth.getCurrentUser() != null) {

        mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());

    }


    //TABS
    mViewPager = (ViewPager) findViewById(R.id.main_tabPager);
    mSectionsPagerAdapter= new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager.setAdapter(mSectionsPagerAdapter);

    mTabLayout = (TabLayout) findViewById(R.id.mainTabs);
    mTabLayout.setupWithViewPager(mViewPager);

}

@Override
protected void onResume() {
    super.onResume();
    FirebaseUser currentUser = mAuth.getCurrentUser();

    if(currentUser == null)
    {
        sendToStart();
    }
    else
    {
        mUserRef.child("online").setValue(true);
    }
}

@Override
protected void onPause() {
    super.onPause();
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if(currentUser != null) {

        mUserRef.child("online").setValue(ServerValue.TIMESTAMP);

    }
}


private void sendToStart()
{
    Intent startIntent = new Intent(MainActivity.this, LoginActivity.class);
    startActivity(startIntent);
    finish();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main_menu, menu);

    return true;
}

第四个片段

public FriendsFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    mMainView = inflater.inflate(R.layout.fragment_friends, container, false);

    mFriendsList = (RecyclerView) mMainView.findViewById(R.id.friends_list);
    mAuth = FirebaseAuth.getInstance();

    mCurrent_user_id = mAuth.getCurrentUser().getUid();

    mFriendsDatabase = FirebaseDatabase.getInstance().getReference().child("Friends").child(mCurrent_user_id);
    mFriendsDatabase.keepSynced(true);
    mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
    mUsersDatabase.keepSynced(true);

    //mFriendsList.setHasFixedSize(true);
    mFriendsList.setLayoutManager(new LinearLayoutManager(getContext()));

    return mMainView;
}

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

    FirebaseRecyclerAdapter<Friends, FriendsViewHolder> friendsRecyclerViewAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(

            Friends.class,
            R.layout.users_single_layout,
            FriendsViewHolder.class,
            mFriendsDatabase


    ) {
        @Override
        protected void populateViewHolder(final FriendsViewHolder friendsViewHolder, Friends friends, int i) {

            friendsViewHolder.setDate(friends.getDate());

            final String list_user_id = getRef(i).getKey();

            mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    final String userName = dataSnapshot.child("name").getValue().toString();
                    String userThumb = dataSnapshot.child("thumb_image").getValue().toString();

                    if(dataSnapshot.hasChild("online")) {

                        String userOnline = dataSnapshot.child("online").getValue().toString();
                        friendsViewHolder.setUserOnline(userOnline);

                    }

                    friendsViewHolder.setName(userName);
                    friendsViewHolder.setUserImage(userThumb, getContext());

                    friendsViewHolder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            CharSequence options[] = new CharSequence[]{"Open Profile", "Send message"};

                            final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

                            builder.setTitle("Select Options");
                            builder.setItems(options, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                    //Click Event for each item.
                                    if(i == 0){

                                        Intent profileIntent = new Intent(getContext(), ProfileActivity.class);
                                        profileIntent.putExtra("user_id", list_user_id);
                                        startActivity(profileIntent);

                                    }

                                    if(i == 1){

                                        Intent chatIntent = new Intent(getContext(), Home.class);
                                        chatIntent.putExtra("user_id", list_user_id);
                                        chatIntent.putExtra("user_name", userName);
                                        startActivity(chatIntent);

                                    }

                                }
                            });

                            builder.show();

                        }
                    });


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    };

    mFriendsList.setAdapter(friendsRecyclerViewAdapter);

已编辑。.我在logcat上查找,并在我的“软件包名称(DEAD)”上找到了它,单击此错误后,由于错误来自片段,我删除了SectionsAdapter。

java.lang.NullPointerException: Attempt to invoke virtual method 
'android.view.View android.view.View.findViewById(int)' on a null object 
reference
    at 
 com.example.gab.quadrantms.FriendsFragment$FriendsViewHolder
.setDate(FriendsFragment.java:177)
    at 
 com.example.gab.quadrantms.FriendsFragment$1.populateViewHolder
(FriendsFragment.java:89)
    at com.example.gab.quadrantms.FriendsFragment$1.populateViewHolder
(FriendsFragment.java:85)

也是这种错误吗?

11-03 02:27:09.593 27447-27447/com.example.gab.quadrantms 
E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist!

0 个答案:

没有答案