我刚开始使用Java编程,在这个应用程序上我已经创建了注册和登录,但是现在我试图在RelativeLayout内的TextView上显示每个登录用户的姓名和年龄。
我正在使用RecyclerView来膨胀具有TextView的RelativeLayout,其中将包含用户信息,但是当我编写所有代码并对其进行编译时,它将使应用程序崩溃。
我希望我已经足够清楚了,即使不只是问我,我也会尝试提供更多细节。
我的 MainActivity.java
package com.pap.diogo.pilltrack;
import android.accounts.Account;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
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.ValueEventListener;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private RelativeLayout pills_layout, appoint_layout, add_pills_layout, add_appoints_layout, account_layout, add_button;
private View pills, appointments, add_pills, add_appoints, account;
private TextView AccountName0, AccountAge0;
private RecyclerView AccountUsers;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Home();
return true;
case R.id.navigation_pills:
Pills();
return true;
case R.id.navigation_appointment:
Appointment();
return true;
case R.id.navigation_account:
Account();
return true;
}
return false;
}
};
public MainActivity() {
}
private void Account(){
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) add_button.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.account);
pills_layout.removeView(pills);
appoint_layout.removeView(appointments);
add_pills_layout.removeView(add_pills);
add_appoints_layout.removeView(add_appoints);
//account_layout.removeAllViews();
//account_layout.addView(account);
add_button.setVisibility(View.VISIBLE);
getUserInfo();
}
private void Appointment() {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) add_button.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.add_appoints);
pills_layout.removeView(pills);
appoint_layout.removeView(appointments);
add_pills_layout.removeView(add_pills);
account_layout.removeView(account);
add_appoints_layout.removeAllViews();
add_appoints_layout.addView(add_appoints);
add_button.setVisibility(View.VISIBLE);
}
private void Pills() {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) add_button.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.add_pills);
pills_layout.removeView(pills);
appoint_layout.removeView(appointments);
add_appoints_layout.removeView(add_appoints);
account_layout.removeView(account);
add_pills_layout.removeAllViews();
add_pills_layout.addView(add_pills);
add_button.setVisibility(View.VISIBLE);
}
private void Home() {
add_appoints_layout.removeView(add_appoints);
add_pills_layout.removeView(add_pills);
account_layout.removeView(account);
pills_layout.removeAllViews();
pills_layout.addView(pills);
appoint_layout.removeAllViews();
appoint_layout.addView(appointments);
add_button.setVisibility(View.GONE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pills_layout = findViewById(R.id.pills_layout);
appoint_layout = findViewById(R.id.appoint_layout);
add_pills_layout = findViewById(R.id.add_pills);
add_appoints_layout = findViewById(R.id.add_appoints);
account_layout = findViewById(R.id.account);
add_button = findViewById(R.id.add);
AccountName0 = findViewById(R.id.AccountName0);
AccountAge0 = findViewById(R.id.AccountAge0);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pills = inflater.inflate(R.layout.pills, null);
appointments = inflater.inflate(R.layout.appointments, null);
add_pills = inflater.inflate(R.layout.add_pills, null);
add_appoints = inflater.inflate(R.layout.add_appoint, null);
account = inflater.inflate(R.layout.account, null);
pills_layout.addView(pills);
appoint_layout.addView(appointments);
add_button.setVisibility(View.GONE);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
public void getUserInfo(){
AccountUsers = findViewById(R.id.AccountList);
AccountUsers.setHasFixedSize(true);
AccountUsers.setLayoutManager(new LinearLayoutManager(getBaseContext()));
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final String userid = user.getUid();
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
FirebaseRecyclerOptions<AccountLayout> AccountQ = new FirebaseRecyclerOptions.Builder<AccountLayout>().setQuery(ref, AccountLayout.class).setLifecycleOwner(this).build();
FirebaseRecyclerAdapter<AccountLayout, AccountInfo> AccountAdapter = new FirebaseRecyclerAdapter<AccountLayout, AccountInfo>(AccountQ){
@NonNull
@Override
public AccountInfo onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new AccountInfo(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.account_layout, viewGroup, false));
}
@Override
protected void onBindViewHolder(@NonNull final AccountInfo holder, int position, @NonNull final AccountLayout model) {
ref.child(userid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final String name = dataSnapshot.child("name").getValue().toString();
final String age = dataSnapshot.child("idade").getValue().toString();
holder.setName(name);
holder.setAge(age);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
};
AccountUsers.setAdapter(AccountAdapter);
}
public static class AccountInfo extends RecyclerView.ViewHolder{
View AccountL;
public AccountInfo(@NonNull View itemView) {
super(itemView);
AccountL = itemView;
}
public void setName(String name){
TextView AccountName = AccountL.findViewById(R.id.AccountName0);
AccountName.setText(name);
}
public void setAge(String age){
TextView AccountAge = AccountL.findViewById(R.id.AccountAge0);
AccountAge.setText(age);
}
}
}
我的 AccountLayout.java
package com.pap.diogo.pilltrack;
public class AccountLayout {
private String name, idade;
public AccountLayout() {
}
public AccountLayout(String name, String idade) {
this.name = name;
this.idade = idade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdade() {
return idade;
}
public void setIdade(String idade) {
this.idade = idade;
}
}
我的 account.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/AccountList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
我的 account_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/AccountUser"
android:layout_width="match_parent"
android:layout_height="163dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:background="@drawable/edit_bg"
android:padding="15dp">
<RelativeLayout
android:id="@+id/AccountImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_user"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/AccountInfos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/AccountImage"
android:layout_toEndOf="@id/AccountImage"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp">
<TextView
android:id="@+id/AccountName0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Nome1"
android:textColor="@color/colorWhite"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/AccountAge0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/AccountName0"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Idade1"
android:textColor="@color/colorWhite"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/AccountChangePass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/AccountAge0"
android:text="Mudar Palavra-Passe"
android:textColor="@color/colorWhite"
android:textSize="18sp"
android:textStyle="bold"
android:textAllCaps="false"
android:padding="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/change_password"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
我的 Logcat
2019-02-22 17:26:43.872 12425-12425/com.pap.diogo.pilltrack E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pap.diogo.pilltrack, PID: 12425
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at com.pap.diogo.pilltrack.MainActivity.getUserInfo(MainActivity.java:162)
at com.pap.diogo.pilltrack.MainActivity.Account(MainActivity.java:80)
at com.pap.diogo.pilltrack.MainActivity.access$300(MainActivity.java:33)
at com.pap.diogo.pilltrack.MainActivity$1.onNavigationItemSelected(MainActivity.java:57)
at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:204)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
at android.support.design.internal.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:115)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)