您好Stackoverflow社区! 我是Java和Android编程的初学者,但我学得很快。最近我有一个非常奇怪的错误。我不知道这个错误是什么。绝对没有代码或logcat错误。我认为整个情况可能是关于ProfileFragment意图连接FollowFragment(在一个奇怪的事件转发中故意扩展AppCompatActivity类)。我认为我的Firebase使用中也存在一个错误,但我真的不知道。但是这里有一个对这个错误的解释:在所说的intent之后,FollowFragment什么都没显示,只是白色空格。应该显示以下用户。没有发生崩溃,也没有logcat错误,一般都没有。这有什么问题?这是我的代码: ProfileFragment:
public class ProfileFragment extends Fragment {
private static final String TAG = "ProfileFragment";
public interface OnGridImageSelectedListener{
void onGridImageSelected(Photo photo, int activityNumber);
}
OnGridImageSelectedListener mOnGridImageSelectedListener;
private static final int ACTIVITY_NUM = 4;
private static final int NUM_GRID_COLUMNS = 3;
//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private FirebaseMethods mFirebaseMethods;
//widgets
private TextView mPosts, mFollowers, mFollowing, mDisplayName, mUsername, mWebsite, mDescription;
private ProgressBar mProgressBar;
private CircleImageView mProfilePhoto;
private GridView gridView;
private Toolbar toolbar;
private ImageView profileMenu;
private BottomNavigationViewEx bottomNavigationView;
private Context mContext;
static class ViewHolder{
User user = new User();
}
ViewHolder holder = new ViewHolder();
//vars
private int mFollowersCount = 0;
private int mFollowingCount = 0;
private int mPostsCount = 0;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
mDisplayName = (TextView) view.findViewById(R.id.display_name);
mUsername = (TextView) view.findViewById(R.id.username);
mWebsite = (TextView) view.findViewById(R.id.website);
mDescription = (TextView) view.findViewById(R.id.description);
mProfilePhoto = (CircleImageView) view.findViewById(R.id.profile_photo);
mPosts = (TextView) view.findViewById(R.id.tvPosts);
mFollowers = (TextView) view.findViewById(R.id.tvFollowers);
mFollowing = (TextView) view.findViewById(R.id.tvFollowing);
mProgressBar = (ProgressBar) view.findViewById(R.id.profileProgressBar);
gridView = (GridView) view.findViewById(R.id.gridView);
toolbar = (Toolbar) view.findViewById(R.id.profileToolBar);
profileMenu = (ImageView) view.findViewById(R.id.profileMenu);
bottomNavigationView = (BottomNavigationViewEx) view.findViewById(R.id.bottomNavViewBar);
mContext = getActivity();
mFirebaseMethods = new FirebaseMethods(getActivity());
Log.d(TAG, "onCreateView: stared.");
setupBottomNavigationView();
setupToolbar();
setupFirebaseAuth();
setupGridView();
getFollowersCount();
getFollowingCount();
getPostsCount();
TextView editProfile = (TextView) view.findViewById(R.id.textEditProfile);
editProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to " + mContext.getString(R.string.edit_profile_fragment));
Intent intent = new Intent(getActivity(), AccountSettingsActivity.class);
intent.putExtra(getString(R.string.calling_activity), getString(R.string.profile_activity));
startActivity(intent);
getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
});
// TextView mFollowing = (TextView) view.findViewById(R.id.tvFollowing);
mFollowing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getActivity(), FollowingFragment.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
});
return view;
}
@Override
public void onAttach(Context context) {
try{
mOnGridImageSelectedListener = (OnGridImageSelectedListener) getActivity();
}catch (ClassCastException e){
Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage() );
}
super.onAttach(context);
}
private void setupGridView(){
Log.d(TAG, "setupGridView: Setting up image grid.");
final ArrayList<Photo> photos = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(getString(R.string.dbname_user_photos))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for ( DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Photo photo = new Photo();
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
try {
photo.setCaption(objectMap.get(getString(R.string.field_caption)).toString());
photo.setTags(objectMap.get(getString(R.string.field_tags)).toString());
photo.setPhoto_id(objectMap.get(getString(R.string.field_photo_id)).toString());
photo.setUser_id(objectMap.get(getString(R.string.field_user_id)).toString());
photo.setDate_created(objectMap.get(getString(R.string.field_date_created)).toString());
photo.setImage_path(objectMap.get(getString(R.string.field_image_path)).toString());
ArrayList<Comment> comments = new ArrayList<Comment>();
for (DataSnapshot dSnapshot : singleSnapshot
.child(getString(R.string.field_comments)).getChildren()) {
Comment comment = new Comment();
comment.setUser_id(dSnapshot.getValue(Comment.class).getUser_id());
comment.setComment(dSnapshot.getValue(Comment.class).getComment());
comment.setDate_created(dSnapshot.getValue(Comment.class).getDate_created());
comments.add(comment);
}
photo.setComments(comments);
List<Like> likesList = new ArrayList<Like>();
for (DataSnapshot dSnapshot : singleSnapshot
.child(getString(R.string.field_likes)).getChildren()) {
Like like = new Like();
like.setUser_id(dSnapshot.getValue(Like.class).getUser_id());
likesList.add(like);
}
photo.setLikes(likesList);
photos.add(photo);
}catch(NullPointerException e){
Log.e(TAG, "onDataChange: NullPointerException: " + e.getMessage() );
}
}
//setup our image grid
int gridWidth = getResources().getDisplayMetrics().widthPixels;
int imageWidth = gridWidth/NUM_GRID_COLUMNS;
gridView.setColumnWidth(imageWidth);
ArrayList<String> imgUrls = new ArrayList<String>();
for(int i = 0; i < photos.size(); i++){
imgUrls.add(photos.get(i).getImage_path());
}
GridImageAdapter adapter = new GridImageAdapter(getActivity(),R.layout.layout_grid_imageview,
"", imgUrls);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mOnGridImageSelectedListener.onGridImageSelected(photos.get(position), ACTIVITY_NUM);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled: query cancelled.");
}
});
}
private void getFollowersCount(){
mFollowersCount = 0;
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child(getString(R.string.dbname_followers))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found follower:" + singleSnapshot.getValue());
mFollowersCount++;
}
mFollowers.setText(String.valueOf(mFollowersCount));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
/*Query userQuery = mReference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(getItem(position).getUser_id());
userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found user: " +
singleSnapshot.getValue(User.class).getUsername());
holder.user = singleSnapshot.getValue(User.class);
}
} */
private void getFollowingCount(){
mFollowingCount = 0;
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child(getString(R.string.dbname_following))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found following user:" + singleSnapshot.getValue());
mFollowingCount++;
}
mFollowing.setText(String.valueOf(mFollowingCount));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void getPostsCount(){
mPostsCount = 0;
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference.child(getString(R.string.dbname_user_photos))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found post:" + singleSnapshot.getValue());
mPostsCount++;
}
mPosts.setText(String.valueOf(mPostsCount));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void setProfileWidgets(UserSettings userSettings) {
//Log.d(TAG, "setProfileWidgets: setting widgets with data retrieving from firebase database: " + userSettings.toString());
//Log.d(TAG, "setProfileWidgets: setting widgets with data retrieving from firebase database: " + userSettings.getSettings().getUsername());
//User user = userSettings.getUser();
UserAccountSettings settings = userSettings.getSettings();
UniversalImageLoader.setImage(settings.getProfile_photo(), mProfilePhoto, null, "");
mDisplayName.setText(settings.getDisplay_name());
mUsername.setText(settings.getUsername());
mWebsite.setText(settings.getWebsite());
mDescription.setText(settings.getDescription());
mProgressBar.setVisibility(View.GONE);
}
/**
* Responsible for setting up the profile toolbar
*/
private void setupToolbar(){
((ProfileActivity)getActivity()).setSupportActionBar(toolbar);
profileMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to account settings.");
Intent intent = new Intent(mContext, AccountSettingsActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
});
}
/**
* BottomNavigationView setup
*/
private void setupBottomNavigationView(){
Log.d(TAG, "setupBottomNavigationView: setting up BottomNavigationView");
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationView);
BottomNavigationViewHelper.enableNavigation(mContext,getActivity() ,bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
/*
------------------------------------ Firebase ---------------------------------------------
*/
/**
* Setup the firebase auth object
*/
private void setupFirebaseAuth(){
Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//retrieve user information from the database
setProfileWidgets(mFirebaseMethods.getUserSettings(dataSnapshot));
//retrieve images for the user in question
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
}
FollowingFragment.java
public class FollowingFragment extends AppCompatActivity{
private ArrayList<Photo> mPhotos;
private ArrayList<Photo> mPaginatedPhotos;
private ArrayList<String> mFollowing;
private ListView mListView;
private MainfeedListAdapter mAdapter;
private int mResults;
TextView username;
CircleImageView profilePhoto;
@Nullable
public View onCreateView(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_following);
mListView = (ListView) findViewById(R.id.listView1);
mFollowing = new ArrayList<>();
mPhotos = new ArrayList<>();
getIncomingIntent();
getFollowing();
return mListView;
}
private void getItDone(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_followrow, container, false);
username = (TextView) view.findViewById(R.id.following_username2);
profilePhoto = (CircleImageView) view.findViewById(R.id.following_profile_image2);}
private void getIncomingIntent(){
Intent intent = getIntent();
}
private void getFollowing(){
Log.d(TAG, "getFollowing: searching for following");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(getString(R.string.dbname_following))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found user: " +
singleSnapshot.child(getString(R.string.field_user_id)).getValue());
mFollowing.add(singleSnapshot.child(getString(R.string.field_user_id)).getValue().toString());
mFollowing.add(FirebaseAuth.getInstance().getCurrentUser().getUid());
username.setText(singleSnapshot.getValue(UserAccountSettings.class).getUsername());
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(
singleSnapshot.getValue(UserAccountSettings.class).getProfile_photo(),
profilePhoto);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:-1)
添加Toast以检查值是否正在检索 -
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Toast.makeText(this, ""+ dataSnapshot, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onDataChange: found user: " +
singleSnapshot.child(getString(R.string.field_user_id)).getValue());
mFollowing.add(singleSnapshot.child(getString(R.string.field_user_id)).getValue().toString());
mFollowing.add(FirebaseAuth.getInstance().getCurrentUser().getUid());
username.setText(singleSnapshot.getValue(UserAccountSettings.class).getUsername());
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(
singleSnapshot.getValue(UserAccountSettings.class).getProfile_photo(),
profilePhoto);
}