Моя сборка завершена успешно, но приложение не работает.Кто-нибудь напишет учебник о том, как связать Firebase Auth с Facebook Login в Android Studio, используя Viewpager и Fragments, как в моем коде?Мне нужен слайдер в моем приложении.
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitenter code herey_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
enter code here
mViewPager.setAdapter(mSectionsPagerAdapter);
mAuth = FirebaseAuth.getInstance();
// Initialize Facebook Login button
mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = findViewById(R.id.login_button);
//Here problem start
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new
FacebookCallback<LoginResult>() {
@Override
enter code here
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
// ...
}
@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
// ...
}
});
// ...
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null){
updateUI();
}
}
private void updateUI() {
Toast.makeText(MainActivity.this, "You are logged in!" ,
Toast.LENGTH_LONG).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result back to the Facebook SDK
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed."[enter image description here][1],
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_second, container, false);
break;
}
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;`enter code here`
}
}
Приложение без фрагментов работает отлично.Кто-нибудь знает, почему?
Это исключение:
2019-01-19 11: 13: 05.129 8439-8439 / jimmy.jimmyfindgymbuddyptl.com.myapplication E / AndroidRuntime: ФАТАЛЬНОЕ ИСКЛЮЧЕНИЕ: main Процесс: jimmy.jimmyfindgymbuddyptl.com.myapplication, PID: 8439 java.lang.RuntimeException: Невозможно запустить действие ComponentInfo {jimmy.jimmyfindgymbuddyptl.com.myapplication / jimmy.jimmyfindgymbuddyptl.com.myapplan}. jain.jainNullPointerException: попытка вызвать виртуальный метод void com.facebook.login.widget.LoginButton.setReadPermissions (java.lang.String []) 'для ссылки на пустой объект в android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2646)на android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2707) на android.app.ActivityThread.-wrap12 (ActivityThread.java) на android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1460) на android.os.Handler.dispatchMessage (Handler.java:102) в android.os.Looper.loop (Looper.java:154) в android.app.ActivityThread.main (ActivityThread.java:6077) в java.lang.reflect.Method.invoke (собственный метод) в com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:866) вcom.android.internal.os.ZygoteInit.main (ZygoteInit.java:756) Причина: java.lang.NullPointerException: Попытка вызвать виртуальный метод void com.facebook.login.widget.LoginButton.setReadPermissions (java.lang.String []) 'для ссылки на пустой объект на jimmy.jimmyfindgymbuddyptl.com.myapplication.MainActivity.onCreate (MainActivity.java:92) в android.app.Activity.performCreate (Activity.java:6662) в android.app.Instrumentation..callActivityOnCreate (Instrumentation.java:1118) в android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2599) в android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2707) в android.app.ActivityThread.read wread12 Activity (.java) в android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1460) в android.os.Handler.dispatchMessage (Handler.java: 102) в android.os.Looper.loop (Looper.java:154) в android.app.ActivityThread.main (ActivityThread.java:6077) в java.lang.reflect.Method.invoke (собственный метод) в.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:866) на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:756)
эта кнопка была в xml:
<com.facebook.login.widget.LoginButton android:id="@+id/login_button"
android:layout_width="190dp" android:layout_height="wrap_content"
android:layout_above="@+id/textView7" android:layout_alignParentStart="true"
android:layout_marginStart="95dp" android:layout_marginBottom="6dp" />