public class PostDetails extends AppCompatActivity {
TextView like,dislike,date,time,name,username;
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
private ArrayList<PostInfo> postInfos;
int i;
ImageView glideImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
i = getIntent().getIntExtra("intVariableName", 0);
postInfos = new ArrayList<>();
like = findViewById(R.id.like);
dislike = findViewById(R.id.dislike);
name = findViewById(R.id.name);
username = findViewById(R.id.username);
date= findViewById(R.id.date);
time = findViewById(R.id.time);
glideImage = findViewById(R.id.glideImage);
getData();
}
public void getData(){
reference.addValueEventListener (new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot posts = dataSnapshot.child ("Posts");
for (DataSnapshot time: posts.getChildren ()){
DataSnapshot url = time.child ("Url");
DataSnapshot name = time.child ("Name");
DataSnapshot username = time.child ("Username");
DataSnapshot date = time.child ("Date");
DataSnapshot likes = time.child("Likes");
DataSnapshot times = time.child("Time");
DataSnapshot dislikes = time.child("DisLikes");
PostInfo postInfo = new PostInfo (String.valueOf (url.getValue ())
,String.valueOf (name.getValue ())
,String.valueOf (username.getValue ())
,String.valueOf (date.getValue ())
,time.getKey()
,String.valueOf(likes.getValue())
,String.valueOf(dislikes.getValue())
,String.valueOf(times.getValue()));
postInfos.add (postInfo);
}
like.setText(postInfos.get(i).likes);
dislike.setText(postInfos.get(i).dislikes);
name.setText(postInfos.get(i).name);
username.setText(postInfos.get(i).username);
date.setText(postInfos.get(i).date);
time.setText(postInfos.get(i).time);
Glide.with(getApplicationContext()).load(postInfos.get(i).Url).into(glideImage);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText (PostDetails.this, "Error 411: " + databaseError.getMessage (), Toast.LENGTH_SHORT).show ();
}
});
}
Вот шесть TextView, которые инициализируются, но когда я пытаюсь записать значения, используя setText, он показывает эту ошибку:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at lifeline.learn.com.hotornot.PostDetails$1.onDataChange(PostDetails.java:67)
at com.google.android.gms.internal.zzejp.zza(Unknown Source)
at com.google.android.gms.internal.zzelk.zzcal(Unknown Source)
at com.google.android.gms.internal.zzelq.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:6776)
at java.lang.reflect.Method.invoke(Native Method)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Я пытался много вещей, чтобы избавиться от этого исключения, но не удаетсякаждый раз.Я новичок в программировании Android, так что выведите меня из этого исключения.Я отфильтровал много вопросов о переполнении, но не нашел подходящего ответа, который мог бы разрешить это исключение. Вот мой XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".PostDetails">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:id="@+id/glideImage"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="@+id/glideImage"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:text="@string/likes"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/like"
android:layout_marginStart="10dp"
android:text="@string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:text="@string/dislikes"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/dislike"
android:layout_marginStart="10dp"
android:text="@string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:text="@string/date"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/date"
android:layout_marginStart="10dp"
android:text="@string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:text="@string/time"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:text="@string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:text="@string/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:text="@string/value"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:text="@string/username"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:text="@string/value"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>