初始化后TextView Null Pointer异常

时间:2018-06-05 08:05:43

标签: java android firebase exception firebase-realtime-database

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>

1 个答案:

答案 0 :(得分:-1)

在您的XML中,没有ID,如下所示: android:id =&#34; @ + id / fullscreen_content&#34; 如您所知,findViewById可能会返回null,请检查您的视图初始化...

/**
 * Finds a view that was identified by the id attribute from the XML that
 * was processed in {@link #onCreate}.
 *
 * @return The view if found or null otherwise.
 */
@SuppressWarnings("TypeParameterUnusedInFormals")
@Nullable
public abstract <T extends View> T findViewById(@IdRes int id);

演示:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context=".FullscreenActivity">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->
    <TextView
        android:id="@+id/fullscreen_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:keepScreenOn="true"
        android:text="@string/dummy_content"
        android:textColor="#33b5e5"
        android:textSize="50sp"
        android:textStyle="bold" />

    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_dark"
        android:fitsSystemWindows="true">

        <LinearLayout
            android:id="@+id/fullscreen_content_controls"
            style="?metaButtonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:background="@color/black_overlay"
            android:baselineAligned="false"
            android:orientation="vertical"
            tools:ignore="UselessParent">

            <LinearLayout
                android:id="@+id/fullscreen_choose_controls"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/fullscreen_esop"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="@string/vs_esop"
                    android:textAllCaps="false"
                    android:textColor="@android:color/holo_blue_dark"
                    android:textSize="50sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/dummy_button"
                    style="?metaButtonBarButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="0.3"
                    android:ellipsize="end"
                    android:gravity="center"
                    android:text="@string/dummy_button"
                    android:textColor="@android:color/holo_red_dark"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/fullscreen_android"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="@string/vs_android"
                    android:textAllCaps="false"
                    android:textColor="@android:color/holo_blue_dark"
                    android:textSize="50sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <TextView
                android:id="@+id/fullscreen_make_your_choice"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/vs_make_your_choice"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="50sp"
                android:textStyle="bold" />

        </LinearLayout>
    </FrameLayout>

</FrameLayout>