繁体   English   中英

Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference in Android Studio

[英]Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference in Android Studio

我正在 Android Studio 的聊天应用程序中制作。 我试图在从 firebase 数据库检索到的私人聊天中显示用户名和头像,我的应用程序崩溃并抛出如下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newsfeedapplication/com.example.newsfeedapplication.ChatActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java. lang.CharSequence)' 在 null object 参考

这是我的代码示例:

public class ChatActivity extends AppCompatActivity
{
    private Toolbar mChatToolbar;
    private ImageButton mSendMsgBtn, mSendImageFileBtn;
    private EditText mUserMsgInput;
    private RecyclerView mUserMsgList;
    private String messageReceiverID, messageReceiverName;
    private TextView mReceivername;
    private CircleImageView mReceiverProfileImage;
    private DatabaseReference mRootRef;

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

        //link to the whole firebase database
        mRootRef = FirebaseDatabase.getInstance().getReference();

        /*getting the receiver name and ID from ParentsActivity when user click on
        the pop-up dialog and choose 'Send Message'
        */
        messageReceiverID = getIntent().getExtras().get("visit_user_id").toString();
        messageReceiverName = getIntent().getExtras().get("userName").toString();

        mChatToolbar = (Toolbar) findViewById(R.id.chat_bar_layout);
        setSupportActionBar(mChatToolbar);
        mSendMsgBtn = (ImageButton) findViewById(R.id.send_message_btn);
        mSendImageFileBtn = (ImageButton) findViewById(R.id.send_image_file_btn);
        mUserMsgInput = (EditText) findViewById(R.id.input_message);
        mReceivername = (TextView) findViewById(R.id.custom_profile_name);
        mReceiverProfileImage = (CircleImageView) findViewById(R.id.custom_profile_image);

        //connect chat custom bar xml to chat activity
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);

        LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View action_bar_view = layoutInflater.inflate(R.layout.chat_custom_bar, null);
        actionBar.setCustomView(action_bar_view);
        DisplayReceiverInformation();
    }

    private void DisplayReceiverInformation()
    {
        mReceivername.setText(messageReceiverName);
        mRootRef.child("Users").child(messageReceiverID).addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            {
                if(dataSnapshot.exists())
                {
                    final String profileImage = dataSnapshot.child("profileimage").getValue().toString();
                    Picasso.get().load(profileImage).into(mReceiverProfileImage);
                }
            }

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

}

这是我的 chat_custom_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/custom_profile_image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/profile"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="15dp"
        android:cropToPadding="true"/>

    <TextView
        android:id="@+id/custom_profile_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Display username"
        android:textColor="@android:color/background_light"
        android:textSize="18sp"
        android:layout_marginTop="8dp"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"/>

</RelativeLayout>

这是我的activity_chat.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"
    tools:context=".ChatActivity">

    <include
        android:id="@+id/chat_bar_layout"
        layout = "@layout/app_bar_layout">

    </include>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/messages_list_users"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/chat_bar_layout"
        android:layout_above="@+id/myLinearLayout"
        android:background="@android:color/darker_gray"/>

    <LinearLayout
        android:id="@+id/myLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background = "@android:color/background_light"
        android:orientation="horizontal"
        >

        <ImageButton
            android:id="@+id/send_image_file_btn"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_select_image"
            />

        <EditText
            android:id="@+id/input_message"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:hint="Wrtie Message Here..."
            android:padding="17dp"
            />

        <ImageButton
            android:id="@+id/send_message_btn"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_send_message"
            />

    </LinearLayout>


</RelativeLayout>

但是,当我在OnCreate()方法中注释掉DisplayReceiverInformation() ) 方法时,应用程序没有崩溃并且能够在chat_custom_bar.xml中显示默认值和配置文件图像。 我错过了什么吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM