原因:java.lang.NullPointerException:单击侦听器设置

时间:2019-12-07 06:40:07

标签: java android xml android-studio

我正在尝试使用按钮打开聊天活动,但是无法打开。

  

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)”           在com.example.dost.AfflictedChatActivity.onCreate(AfflictedChatActivity.java:38)           在android.app.Activity.performCreate(Activity.java:7009)           在android.app.Activity.performCreate(Activity.java:7000)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6494)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)           com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

聊天活动XML代码如下:

<androidx.constraintlayout.widget.ConstraintLayout 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=".AfflictedChatActivity">

    <Button
        android:id="@+id/afflictedSendButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="36dp"
        android:text="Send"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <ListView
        android:id="@+id/chatActivityList"
        android:layout_width="376dp"
        android:layout_height="596dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.578"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/messageEditText"
        android:layout_width="295dp"
        android:layout_height="46dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="11dp"
        android:layout_marginEnd="4dp"
        android:layout_marginBottom="11dp"
        android:ems="10"
        android:hint="Type Message here..."
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/afflictedSendButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/chatActivityList" />
</androidx.constraintlayout.widget.ConstraintLayout> 

聊天活动Java代码如下:


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class AfflictedChatActivity extends AppCompatActivity {

    Button sendButton;
    EditText messageEditText;
    String afflictedUserMessage;

    ListView chatActivityList;
    ArrayList<String> afflictedChatArrayList;
    ArrayAdapter afflictedArrayAdapter;
    //client afflictedClient;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().setTitle("Chat Room");

        sendButton = findViewById(R.id.afflictedSendButton);
        messageEditText = findViewById(R.id.messageEditText);

        chatActivityList = findViewById(R.id.chatActivityList);
        afflictedChatArrayList = new ArrayList<>();
        afflictedArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, afflictedChatArrayList);

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                afflictedUserMessage = messageEditText.getText().toString();
                //afflictedClient.writer.println(afflictedUserMessage);
                //afflictedClient.writer.flush();
                afflictedChatArrayList.add("> You: " + afflictedUserMessage);
                chatActivityList.setAdapter(afflictedArrayAdapter);
            }
        });
    }
}

0 个答案:

没有答案