Android Studio:java.lang.NullPointerException

时间:2019-05-31 23:49:09

标签: java android android-studio

这是我的第一篇帖子,对任何错误表示歉意

大家好,我目前正在制作我的第一个android应用,它是一个基于组的位置跟踪器(受ATAK启发)。

到目前为止,我正在尝试实现自动密钥生成器,该密钥生成器在创建后会上传到我的Firebase数据库。目前它在基本级别上起作用。

我还有一个“我的个人资料”部分,可让我查看我的帐户用户名和电子邮件。但是,当我查看个人资料并生成团队代码时,我的应用程序崩溃了。反之亦然,因此,如果我生成团队代码然后查看我的个人资料,该应用程序也会崩溃。要使该应用程序再次正常运行,我必须从我的Firebase数据库中删除团队代码,否则无法查看我的个人资料。

预先感谢您:)

它给我的错误是

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.project.teamtrack.User.getName()' on a null object reference

CreateTeam.java

import java.util.Random;

public class CreateTeam extends AppCompatActivity implements View.OnClickListener {
    private Button buttonRegister;
    private Button buttonMap;
    private TextView textCode;

    DatabaseReference mRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.createteam);

        textCode = (TextView) findViewById(R.id.textCode);
        buttonRegister = (Button) findViewById(R.id.buttonRegister);
        buttonMap = (Button) findViewById(R.id.buttonMap);
        buttonMap.setOnClickListener(this);
        buttonRegister.setOnClickListener(this);

        mRef = FirebaseDatabase.getInstance().getReference().child("Teams");

    }
    public void TeamCode () {

        //generates team code
        Random code = new Random();
        int number1 = code.nextInt(10);
        int number2 = code.nextInt(10);
        int number3 = code.nextInt(10);
        int number4 = code.nextInt(10);
        int number5 = code.nextInt(10);
        int number6 = code.nextInt(10);
        String myString = String.valueOf(number1);
        String myString2 = String.valueOf(number2);
        String myString3 = String.valueOf(number3);
        String myString4 = String.valueOf(number4);
        String myString5 = String.valueOf(number5);
        String myString6 = String.valueOf(number6);

        String finalCode = myString+myString2+myString3+myString4+myString5+myString6;


        textCode.setText(finalCode);
        String TeamCode = textCode.getText().toString().trim();

        mRef.push().setValue(TeamCode);
        Toast.makeText(CreateTeam.this, "Team has been registered", Toast.LENGTH_LONG).show();


    }

    @Override
    public void onClick(View view){
        if (view == buttonRegister){
            TeamCode();
            buttonRegister.setEnabled(false);
        }

        if (view == buttonMap){
            finish();
            startActivity(new Intent(this, MapsActivity.class));
        }
    }

}

ProfileActivity.java

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "ProfileActivity";

    private TextView textViewUsername;
    private TextView textViewEmail;
    private Button buttonLogout;
    private Button buttonMap;

    private String userID;
    private FirebaseDatabase mFirebaseDatabase;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private DatabaseReference myRef;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        textViewEmail = (TextView) findViewById(R.id.textViewEmail);
        textViewUsername = (TextView) findViewById(R.id.textViewUsername);

        buttonMap = (Button) findViewById(R.id.buttonMap);
        buttonLogout = (Button) findViewById(R.id.buttonLogout);

        buttonLogout.setOnClickListener(this);
        buttonMap.setOnClickListener(this);

        mAuth = FirebaseAuth.getInstance();
        mFirebaseDatabase = FirebaseDatabase.getInstance();
        myRef = mFirebaseDatabase.getReference();
        FirebaseUser user = mAuth.getCurrentUser();
        userID = user.getUid();

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user !=null) {

                } else {

                }

                //...
            }
        };

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                ShowData(dataSnapshot);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }


    private void ShowData(DataSnapshot dataSnapshot){
        for (DataSnapshot ds : dataSnapshot.getChildren()){
            User user = new User();
            user.setName(ds.child(userID).getValue(User.class).getName());
            user.setEmail(ds.child(userID).getValue(User.class).getEmail());

            Log.d(TAG, "ShowData: Name: " + user.getName());
            Log.d(TAG, "ShowData: Email: " + user.getEmail());

            //grabs users name/email and displays it
            textViewEmail.setText("Email: "+user.getEmail());
            textViewUsername.setText("Username: "+user.getName());

        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }
    @Override
    public void onClick(View view){
        if (view == buttonMap){
            finish();
            startActivity(new Intent(this, MapsActivity.class));

        }

        if (view == buttonLogout){
            mAuth.signOut();
            finish();
            startActivity(new Intent(this, LoginActivity.class));

        }


    }


}

User.java

public class User {
    public String name;
    public String email;


    public User(){

    }
    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

编辑:

要解决此问题,我必须使用listviews编写ProfileActivity

public class Profile extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "ProfileActivity";

    private Button buttonLogout;
    private Button buttonMap;

    List<String> userlist;
    ArrayAdapter<String> adapter;
    ListView UserList;

    private FirebaseDatabase mFirebaseDatabase;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private DatabaseReference myRef;
    private String userID;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        UserList = (ListView)findViewById(R.id.UserList);
        userlist = new ArrayList<>();

        buttonMap = (Button) findViewById(R.id.buttonMap);
        buttonLogout = (Button) findViewById(R.id.buttonLogout);
        buttonLogout.setOnClickListener(this);
        buttonMap.setOnClickListener(this);

        mAuth = FirebaseAuth.getInstance();
        myRef = FirebaseDatabase.getInstance().getReference("User");
        FirebaseUser user = mAuth.getCurrentUser();
        userID = user.getUid();


        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@Nullable FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user !=null) {

                } else {

                }
                //...
            }
        };

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@Nullable DataSnapshot dataSnapshot)
            {
                userlist.clear();
                String name = dataSnapshot.child(userID).child("name").getValue().toString();
                String email = dataSnapshot.child(userID).child("email").getValue().toString();

                userlist.add("Name: "+name);
                userlist.add("Email: "+email);

                adapter = new ArrayAdapter<>(Profile.this, android.R.layout.simple_list_item_1,userlist);
                UserList.setAdapter(adapter);


            }

            @Override
            public void onCancelled(@Nullable DatabaseError databaseError) {

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (mAuth.getCurrentUser()== null) {
            finish();
            startActivity(new Intent(this, LoginActivity.class));
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);

        }
    }
    @Override
    public void onClick(View view){
        if (view == buttonMap){
            finish();
            startActivity(new Intent(this, MapsActivity.class));

        }

        if (view == buttonLogout){
            mAuth.signOut();
            finish();
            startActivity(new Intent(this, LoginActivity.class));

        }
    }
}

0 个答案:

没有答案