package com.example.sarin.guff;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ServerValue;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.StorageReference;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
private static final int GALLERY_PIC = 1;
private DatabaseReference onlineDatabase;
private static final int MAX_LENGTH = 100;
private TextView displayName;
private TextView phoneNumber;
private ImageView profilePicture;
private StorageReference mStorage;
private Toolbar toolbar;
private ViewPager mViewPager;
private SectionsPagerAdapter mSectionPagerAdapter;
private TabLayout mTabLayout;
private DatabaseReference mDatabaseReference;
private String uId;
private FirebaseUser mFireBaseUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
navigationView.setNavigationItemSelectedListener(this);
// Tabs
mViewPager = findViewById(R.id.main_tabPager);
mSectionPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionPagerAdapter);
mTabLayout = findViewById(R.id.main_tabs);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.setSelectedTabIndicatorColor(Color.parseColor("#4f1a97"));
mTabLayout.setSelectedTabIndicatorHeight((int) (5 * getResources().getDisplayMetrics().density));
mTabLayout.setTabTextColors(Color.parseColor("#4f1a97"), Color.parseColor("#4f1a97"));
//profile
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uId = user.getUid();
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("users").child(uId);
mDatabaseReference.keepSynced(true);
profilePicture = headerView.findViewById(R.id.nav_profilePic);
phoneNumber = headerView.findViewById(R.id.nav_phoneNumber);
displayName = headerView.findViewById(R.id.nav_userName);
currentUser = FirebaseAuth.getInstance().getCurrentUser();
onlineDatabase = FirebaseDatabase.getInstance().getReference().child("users").child(currentUser.getUid()).child("Online");
onlineDatabase.keepSynced(true);
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("Name").getValue().toString();
String image = dataSnapshot.child("Image").getValue().toString();
final String thumbImage = dataSnapshot.child("Thumb_image").getValue().toString();
String v_id = dataSnapshot.child("Id").getValue().toString();
displayName.setText(name);
phoneNumber.setText(v_id);
if(!thumbImage.equals("default")) {
Picasso.with(MainActivity.this).load(thumbImage).placeholder(R.drawable.avatar_default).into(profilePicture);
}
profilePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplication(), FullScreenImageView.class);
intent.setType(thumbImage);
startActivity(intent);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
private void sendToStart(){
Intent startIntent = new Intent(MainActivity.this, StartActivity.class);
startActivity(startIntent);
finish();
}
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if(item.getItemId() == R.id.nav_profile){
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
else if(item.getItemId() == R.id.nav_changePassword){
startActivity(new Intent(MainActivity.this, VerifyPhone .class));
}
else if(item.getItemId() == R.id.nav_delete){
startActivity(new Intent(MainActivity.this, DeleteAccountActivity.class));
}
else if(item.getItemId() == R.id.nav_logout){
if ((onlineDatabase != (null))){
onlineDatabase.setValue(ServerValue.TIMESTAMP);
FirebaseAuth.getInstance().signOut();
sendToStart();
}else
{
DatabaseReference onlineDatabase = FirebaseDatabase.getInstance().getReference().child("users").child(currentUser.getUid()).child("Online");
}
}
else if(item.getItemId() == R.id.nav_findFriends){
startActivity(new Intent(MainActivity.this, AllUsersActivity.class));
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
我在第89行收到错误。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sarin.guff, PID: 3092
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sarin.guff/com.example.sarin.guff.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6577)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.example.sarin.guff.MainActivity.onCreate(MainActivity.java:91)
at android.app.Activity.performCreate(Activity.java:6910)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6577)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
申请已终止。
ERROR:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.sarin.guff / com.example.sarin.guff.MainActivity}: java.lang.NullPointerException:尝试调用虚方法 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'on a null对象引用
有人可以帮我解决这个错误吗?在此先感谢!。
答案 0 :(得分:0)
我希望这对您有所帮助,您也可以点击此链接https://firebase.google.com/docs/auth/android/start/
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//it intialization should be inside onCreate
private FirebaseAuth mAuth ;
FirebaseUser currentUser
private static final int GALLERY_PIC = 1;
private DatabaseReference onlineDatabase;
private static final int MAX_LENGTH = 100;
private TextView displayName;
private TextView phoneNumber;
private ImageView profilePicture;
private StorageReference mStorage;
private Toolbar toolbar;
private ViewPager mViewPager;
private SectionsPagerAdapter mSectionPagerAdapter;
private TabLayout mTabLayout;
private DatabaseReference mDatabaseReference;
private String uId;
private FirebaseUser mFireBaseUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
//rest of your code
}
}
答案 1 :(得分:0)
确保用户在登录之前登录 FirebaseUser.getUid()
答案 2 :(得分:0)
早上好,我的项目也出现了类似的错误。这个错误通常发生在用户未登录时。 我所做的是: 1)尝试了堆栈溢出的所有解决方案,但对我不起作用! 2)然后我去了我的项目的AndroidManifest.xml文件
注意:我的登录页面是.RegistrationActivity 我的主页是..ContactsActivity
Originally it was:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.facehub2">
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.app1">
<activity android:name=".RegistrationActivity"></activity>
<activity android:name=".ContactsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
所以我把它改成了:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.facehub2">
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.app1">
<activity android:name=".RegistrationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ContactsActivity">
</activity>
</application>
</manifest>
我遇到错误的原因是因为 android.intent.action.MAIN 被分配给了主 Activity,即 .ContactsActivity 而不是 .RegistrationActivity
即,每当我尝试启动应用程序时,它都会先尝试显示我的 mainactivity 文件,这就是我收到错误消息的原因:Caused by: java.lang.NullPointerException.So提供给我的注册活动文件,我能够先登录并解决了错误。谢谢。