gpt4 book ai didi

java - 尝试在空对象引用上调用虚拟方法 'void android.widget.TextView.append(java.lang.CharSequence)'

转载 作者:行者123 更新时间:2023-12-02 13:34:34 27 4
gpt4 key购买 nike

我无法通过按钮从 EditText 获取数据(文本)到 TextView。逻辑应该没问题,我实在找不到错误。

当我单击触发“sendMessage(View v)”的按钮时,我立即收到错误消息。我无法将文本附加到“聊天”,但我不知道为什么。

MainActivity.java:

package com.chattr.neonardo.chattr;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

TextView chat;
EditText message;
Button send;

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

chat = (TextView) findViewById(R.id.chat);
message = (EditText) findViewById(R.id.message);
send = (Button) findViewById(R.id.send);

Log.d("test", "test2");

/* send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chat.append("\n" + message.getText().toString());
}
});*/
}

public void onMatchTap(View v) {
Toast myToast = Toast.makeText(getApplicationContext(), R.string.searching, Toast.LENGTH_LONG);
myToast.show();
setContentView(R.layout.chat_main);
Log.d("test", "test4");
}

public void sendMessage(View v) {
Log.d("test", "test6");
chat.append("\n" + message.getText().toString());
Log.d("test", "test7");
}

}

我尝试了 onClickListener 的一些操作,但它不起作用,所以我将其注释掉了。

main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.chattr.neonardo.chattr.MainActivity">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/chattr"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="91dp"
android:onClick="onMatchTap"
android:text="@string/start_chat" />

我正在使用两个不同的 xml,因为我正在改变场景。

chat_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<TextView
android:id="@+id/chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.00"
android:autoLink="web"
android:text="@string/chat_connection"
android:visibility="visible" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<EditText
android:id="@+id/message"
android:layout_width="301dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="@string/defaultMessage" />

<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:elevation="0dp"
android:onClick="sendMessage"
android:text="Send" />
</LinearLayout>

这就是错误:

    Process: com.chattr.neonardo.chattr, PID: 24340
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
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)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
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) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference
at com.chattr.neonardo.chattr.MainActivity.sendMessage(MainActivity.java:47)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
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)

最佳答案

删除setContentView(R.layout.chat_main);

您应该使用 IntentonClick 方法中打开一个新 Activity 。

Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
startActivity(intent);

应该是

public void onMatchTap(View v) {
Toast myToast = Toast.makeText(getApplicationContext(), R.string.searching, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,ChatMain.class);
startActivity(intent);
Log.d("test", "test4");
}

关于java - 尝试在空对象引用上调用虚拟方法 'void android.widget.TextView.append(java.lang.CharSequence)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43068069/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com