NavigationView
데이터베이스에서 가져온 이름과 이메일을 설정하려고 합니다.
방법에 대한 참조를 제공 NavigationView
한 onCreate()
방법 은 다음과 같습니다 .
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
하지만 여전히이 오류가 발생합니다. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.getHeaderView(int)' on a null object reference at com.abc.xyz.MainActivity.fetchDataFromProviderProvided(MainActivity.java:689)
fetchDataFromProviderProvided()
방법은 다음과 같습니다 .
public void fetchDataFromProviderProvided() {
if (authData != null) {
swipeRefreshLayout.setRefreshing(false);
if (authData.getProvider().equals("google")) {
fetchDataFromGoogle();
} else if (authData.getProvider().equals("facebook")) {
onFacebookAccessTokenChange(AccessToken.getCurrentAccessToken());
} else {
// DO SOMETHING TO FETCH USER'S NAME
String Uid = authData.getUid();
Log.d("Uid:", Uid);
Firebase userNameRef = mFirebaseRef.child(Uid + "/userName");
Log.d("userName", String.valueOf(userNameRef));
emailImage = (String) authData.getProviderData().get("profileImageURL");
// error on the line below
View imageView = navigationView.getHeaderView(0);
//
userImage = (ImageView) imageView.findViewById(R.id.userImage);
new emailProfilePicAsync().execute(authData.getUid());
View nameView = navigationView.getHeaderView(0);
userName = (TextView) nameView.findViewById(R.id.userName);
userNameRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userName.setText(String.valueOf(dataSnapshot.getValue()));
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
emailName = userNameText;
emailEmail = (String) authData.getProviderData().get("email");
View emailView = navigationView.getHeaderView(0);
userEmail = (TextView) emailView.findViewById(R.id.userEmail);
userEmail.setText(emailEmail);
// System.out.println(post.getUserName());
// System.out.println(post.getUserEmail());
}
dataLoadingCompleteProgressDialog = ProgressDialog.show(MainActivity.this, "",
"Fetching user's info...", true);
if (dataLoadingCompleteProgressDialog.isShowing()) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
checkIfDataIsFetched();
dataLoadingCompleteProgressDialog.dismiss();
}
}, 5500);
}
} else {
}
}
다음은 activity_main.xml
파일 코드입니다.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
다음은 전체 스택 추적입니다.
FATAL EXCEPTION: main
Process: com.abc.xyz, PID: 19072
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.xyz/com.abc.xyz.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.getHeaderView(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.getHeaderView(int)' on a null object reference
at com.abc.xyz.MainActivity.fetchDataFromProviderProvided(MainActivity.java:694)
at com.abc.xyz.MainActivity.onCreate(MainActivity.java:217)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
여기서 무엇이 잘못되었는지 알려주세요.
질문의 형식이 잘못되어 죄송합니다. 나는 아직 여기에서 초보자입니다.
navigationView
메서드 호출 fetchDataFromProviderProvided()
이 완료 되기 전에 초기화해야합니다 .
이러한 실수를 방지하고 코드의 유지 관리 가능성을 높이려면 ButterKnife
라이브러리를 사용하십시오 . 이것은 사용하기 매우 간단합니다.
onCreate()
메서드 호출 에서 개체 Butterknife.bind()
를 초기화 View
하려면 다음을 수행하십시오.
@Bind(R.id.nav_view)
NavigationView navigationView;
도움이 되었기를 바랍니다.
참조 버터 나이프
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다