我无法通过Button从EditText获取数据(文本)到TextView。 逻辑应该很好,我无法找到错误。
一旦我点击触发按钮" sendMessage(查看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)
答案 0 :(得分:1)
删除setContentView(R.layout.chat_main);
您应该使用Intent
在onClick
方法中打开新活动。
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");
}
答案 1 :(得分:0)
chat = (TextView) findViewById(R.id.chat);
此TextView在您的chat_main.xml文件中定义,并尝试将其绑定在您的主活动中。这就是为什么它会为您提供 NullPointerException 。