您好Stackoverflow社区, 起初我想说我是初学者。最近我注意到我的代码中出现了很奇怪的错误我刚刚从一个活动到另一个活动写了一个简单的意图,发生了一些非常奇怪
意图部分(意图是FollowActivity)显示常量错误
"无法访问的声明"。
我是否应用TextView mFollowing = (TextView) view.findViewById(R.id.tvFollowing);
它仍显示该错误。我的情况怎么办?最无法解决的问题是之前的意图(AccountSettingActivity intent)工作得非常好。这是我的代码:
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);
}
});
return view;
// TextView mFollowing = (TextView) view.findViewById(R.id.tvFollowing);
mFollowing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to " + mContext.getString(R.string.edit_profile_fragment));
Intent intent = new Intent(getActivity(), FollowingActivity.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);
}
});
}
@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);
}
}
}
答案 0 :(得分:2)
由于return view;
语句将其移至mFollowing.setOnClickListener
之后,如下所示:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
...
// TextView mFollowing = (TextView) view.findViewById(R.id.tvFollowing);
mFollowing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "onClick: navigating to " + mContext.getString(R.string.edit_profile_fragment));
Intent intent = new Intent(getActivity(), FollowingActivity.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);
}
});
return view;
}