尝试调用虚拟方法“void 安卓”。小装置。文本视图。在空对象引用上追加(java.lang.CharSequence)'
我无法通过按钮从EditText到TextView获取数据(文本)。 逻辑应该很好,我真的找不到错误
我一点击触发“sendMessage(视图v)”的按钮就得到了错误。 我不能在“聊天”中添加文字,但我不知道为什么
主要活动。爪哇:
package com.chattr.neonardo.chattr;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.TextView;
import 安卓.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");
}
}
我试过用OnClick Listener做些什么,但没用,所以我把它注释掉了
主要活动。xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/activity_main"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.chattr.neonardo.chattr.MainActivity">
<ImageView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
app:srcCompat="@drawable/chattr"
安卓:id="@+id/imageView"
安卓:layout_alignParentTop="true"
安卓:layout_alignParentLeft="true"
安卓:layout_alignParentStart="true" />
<Button
安卓:id="@+id/button"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentBottom="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginBottom="91dp"
安卓:onClick="onMatchTap"
安卓:text="@string/start_chat" />
我正在使用两种不同的xml,因为我正在改变场景
聊天室。xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:orientation="vertical" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:weightSum="1">
<TextView
安卓:id="@+id/chat"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_weight="1.00"
安卓:autoLink="web"
安卓:text="@string/chat_connection"
安卓:visibility="visible" />
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal">
<EditText
安卓:id="@+id/message"
安卓:layout_width="301dp"
安卓:layout_height="wrap_content"
安卓:ems="10"
安卓:inputType="text"
安卓:text="@string/defaultMessage" />
<Button
安卓:id="@+id/send"
安卓:layout_width="wrap_content"
安卓:layout_height="match_parent"
安卓:elevation="0dp"
安卓:onClick="sendMessage"
安卓:text="Send" />
</LinearLayout>
这就是错误:
Process: com.chattr.neonardo.chattr, PID: 24340
java.lang.IllegalStateException: Could not execute method for 安卓:onClick
at 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at 安卓.view.View.performClick(View.java:5637)
at 安卓.view.View$PerformClick.run(View.java:22429)
at 安卓.os.Handler.handleCallback(Handler.java:751)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:154)
at 安卓.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at 安卓.view.View.performClick(View.java:5637)
at 安卓.view.View$PerformClick.run(View.java:22429)
at 安卓.os.Handler.handleCallback(Handler.java:751)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:154)
at 安卓.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.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 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at 安卓.view.View.performClick(View.java:5637)
at 安卓.view.View$PerformClick.run(View.java:22429)
at 安卓.os.Handler.handleCallback(Handler.java:751)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:154)
at 安卓.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:776)
# 1 楼答案
此文本视图在聊天室主界面中定义。xml文件,并尝试将其绑定到主要活动中。这就是为什么它会给你空点异常