【发布时间】:2019-01-30 12:54:27
【问题描述】:
我有七个 TextViews 和一个数组中的七个元素。现在,我想在七个不同的 TextView 中设置这些数组元素。但是,当这个 Activity 打开时,我的应用程序会停止。
这是我的 Activity.xml
<RelativeLayout 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:gravity="center"
tools:context=".ProfileActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="50dp">
<include
android:id="@+id/main_page_toolbar"
layout="@layout/main_app_bar_layout" />
</android.support.design.widget.AppBarLayout>
<TextView
android:id="@+id/profile_name_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:text="Name"
android:textColor="@color/colorPrimaryDark"
android:textSize="26sp" />
<TextView
android:id="@+id/profile_email_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp"
android:text="Email"
android:textSize="18sp" />
<TextView
android:id="@+id/profile_id_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:text="ID"
android:textColor="@color/spots_dialog_color"
android:textSize="24sp" />
<LinearLayout
android:id="@+id/linearLayoutCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="50dp"
android:layout_marginTop="223dp"
android:paddingLeft="30dp"
android:paddingRight="30dp">
<TextView
android:id="@+id/profile_invite_code1"
android:layout_width="5dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="1"
android:textSize="20sp" />
<TextView
android:id="@+id/profile_invite_code2"
android:layout_width="5dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="2"
android:textSize="20sp" />
<TextView
android:id="@+id/profile_invite_code3"
android:layout_width="5dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="3"
android:textSize="20sp" />
<TextView
android:id="@+id/profile_invite_code4"
android:layout_width="5dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="4"
android:textSize="20sp" />
<TextView
android:id="@+id/profile_invite_code5"
android:layout_width="5dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="e"
android:textSize="20sp" />
<TextView
android:id="@+id/profile_invite_code6"
android:layout_width="5dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="r"
android:textSize="20sp" />
<TextView
android:id="@+id/profile_invite_code7"
android:layout_width="5dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="s"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
这里是 Activity.java
public class ProfileActivity extends AppCompatActivity {
// toolbar instance
private Toolbar mToolbar;
RelativeLayout rootLayout;
//Array for invite code digits
public char inviteArray[] = new char[7];
// Text view Instances
private TextView mUserConnectionId;
private TextView mProfileUsernameView;
private TextView mProfileEmailView;
public TextView mInviteCodetext1;
public TextView mInviteCodetext2;
public TextView mInviteCodetext3;
public TextView mInviteCodetext4;
public TextView mInviteCodetext5;
public TextView mInviteCodetext6;
public TextView mInviteCodetext7;
TextView [] inviteTextView = { mInviteCodetext1 ,
mInviteCodetext2 ,
mInviteCodetext3 ,
mInviteCodetext4 ,
mInviteCodetext5 ,
mInviteCodetext6 ,
mInviteCodetext7 };
//Firebase
private DatabaseReference mUserDataBaseRef;
private FirebaseAuth mAuth;
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set contentView
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Arkhip_font.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
setContentView(R.layout.activity_profile);
//Custom Toolbar
mToolbar = findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar()
.setTitle("Profile");
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mUserConnectionId = (TextView) findViewById(R.id.profile_id_text);
mProfileUsernameView = (TextView) findViewById(R.id.profile_name_view);
mProfileEmailView = (TextView) findViewById(R.id.profile_email_view);
//Invite code Text view
mInviteCodetext1 = (TextView) findViewById(R.id.profile_invite_code1);
mInviteCodetext2 = (TextView) findViewById(R.id.profile_invite_code2);
mInviteCodetext3 = (TextView) findViewById(R.id.profile_invite_code3);
mInviteCodetext4 = (TextView) findViewById(R.id.profile_invite_code4);
mInviteCodetext5 = (TextView) findViewById(R.id.profile_invite_code5);
mInviteCodetext6 = (TextView) findViewById(R.id.profile_invite_code6);
mInviteCodetext7 = (TextView) findViewById(R.id.profile_invite_code7);
mInviteCodetext1.setBackground(getResources().getDrawable(R.drawable.edittext_background));
mInviteCodetext2.setBackground(getResources().getDrawable(R.drawable.edittext_background));
mInviteCodetext3.setBackground(getResources().getDrawable(R.drawable.edittext_background));
mInviteCodetext4.setBackground(getResources().getDrawable(R.drawable.edittext_background));
mInviteCodetext5.setBackground(getResources().getDrawable(R.drawable.edittext_background));
mInviteCodetext6.setBackground(getResources().getDrawable(R.drawable.edittext_background));
mInviteCodetext7.setBackground(getResources().getDrawable(R.drawable.edittext_background));
}
@Override
protected void onStart() {
super.onStart();
mAuth = FirebaseAuth.getInstance();
if(mAuth.getCurrentUser() != null){
mUserDataBaseRef = FirebaseDatabase.getInstance().getReference()
.child("Users").child(mAuth.getCurrentUser().getUid());
}
mUserDataBaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String username = dataSnapshot.child("name").getValue().toString();
String email = dataSnapshot.child("email").getValue().toString();
String inviteCode = dataSnapshot.child("uniqueID").getValue().toString();
mUserConnectionId.setText(inviteCode);
mProfileUsernameView.setText(username);
mProfileEmailView.setText(email);
for (int i = 0; i < inviteCode.length(); ++i) {
inviteArray[i] = inviteCode.charAt(i); // This array giving me correct result
}
for (int i=0 ; i<inviteArray.length; ++i){
inviteTextView[i].setText(inviteArray[i]);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
我的 logact 给出了这些错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
at com.example.ankit.controlchild.ProfileActivity$1.onDataChange(ProfileActivity.java:134)
at com.google.android.gms.internal.to.zza(Unknown Source)
at com.google.android.gms.internal.vj.zzHX(Unknown Source)
at com.google.android.gms.internal.vp.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
请告诉我我在这里做错了什么。还有,有没有更好的方法。
【问题讨论】:
-
上面使用了recyclerview或listview。
-
发布您的错误日志。
-
inviteCode字符串中有什么值? -
试试
invitetextView[i].setText(inviteArray[I]+""); -
发布 LogCat 报告,以便我们找到您到底做错了什么
标签: java android arrays textview