运行代码时,执行SendCredentials
无效时会出错。
错误是:-
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.messager, PID: 16131
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6614)
at android.view.View.performClickInternal(View.java:6591)
at android.view.View.access$3100(View.java:786)
at android.view.View$PerformClick.run(View.java:25948)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6614)
at android.view.View.performClickInternal(View.java:6591)
at android.view.View.access$3100(View.java:786)
at android.view.View$PerformClick.run(View.java:25948)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.DataInputStream.readLine()'
on a null object reference
at com.example.messager.MainActivity.SendCredentials(MainActivity.java:69)
at com.example.messager.MainActivity.SendMessage(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6614)
at android.view.View.performClickInternal(View.java:6591)
at android.view.View.access$3100(View.java:786)
at android.view.View$PerformClick.run(View.java:25948)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
我的代码是
package com.example.messager;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.net.*;
import java.io.*;
public class MainActivity extends AppCompatActivity {
EditText stopPort;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void SendMessage(View view)throws IOException{
EditText emailText = (EditText) findViewById(R.id.Email);
String Username = emailText.getText().toString();
EditText passwordText = (EditText) findViewById(R.id.Password);
String Password = passwordText.getText().toString();
System.out.println("Your username is " + Username + "Your Password is " + Password);
SendCredentials();
}
public void SendCredentials()throws IOException {
System.out.println("Work");
Socket socket = new Socket("192.168.0.114", 4321);
System.out.println("Work");
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
dOut.writeByte(1);
dOut.writeUTF("This is a test");
dOut.flush();
System.out.println("Work");
}
public void Check(){
}
}
`
答案 0 :(得分:0)
查看上面的代码和异常跟踪2可能是错误的。
检查您的代码是否 java.lang.NullPointerException:尝试在
在xml布局文件中,您是否在android:onClick上声明了“检查”方法 即android:onClick =“ check”,如果是,则您的检查方法应如下所示
公共无效检查(查看){ }
您是否也可以共享xml布局文件。
答案 1 :(得分:0)
是的,我的xml代码是'
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=".MainActivity">
<TextView
android:id="@+id/Welcome"
android:layout_width="198dp"
android:layout_height="52dp"
android:layout_marginStart="104dp"
android:layout_marginTop="68dp"
android:text="Welcome"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/colorPrimaryDark"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.TextInputLayout
android:id="@+id/passwordWrapper"
android:layout_width="387dp"
android:layout_height="56dp"
android:layout_marginStart="8dp"
android:layout_marginTop="444dp"
android:hint="Password"
app:hintEnabled="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/Password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Password"
android:inputType="textPassword"
android:textSize="18sp"
tools:text="" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/emailWrapper"
android:layout_width="397dp"
android:layout_height="56dp"
android:layout_marginStart="8dp"
android:layout_marginTop="368dp"
android:hint="Email"
app:hintEnabled="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/Email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Email"
android:inputType="textWebEmailAddress"
android:textAlignment="textStart"
android:textSize="18sp"
tools:text=""/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/LoginButton"
android:layout_width="348dp"
android:layout_height="45dp"
android:layout_marginStart="28dp"
android:layout_marginTop="520dp"
android:text="LOGIN"
android:onClick="SendMessage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
'
答案 2 :(得分:-1)
public void SendMessage(View view) throws IOException {
EditText emailText = (EditText) findViewById(R.id.Email);
String Username = emailText.getText().toString();
EditText passwordText = (EditText) findViewById(R.id.Password);
String Password = passwordText.getText().toString();
System.out.println("Your username is " + Username + "Your Password is " + Password);
SendCredentials();
}
public void SendCredentials() throws IOException {
new MyAsyncTask().execute();
}
public void Check() {
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
Activity activity;
IOException ioException;
MyAsyncTask() {
super();
}
@Override
protected Void doInBackground(Void... voids) {
StringBuilder sb = new StringBuilder();
try {
System.out.println("Work");
Socket socket = new Socket("192.168.0.114", 4321);
System.out.println("Work");
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
dOut.writeByte(1);
dOut.writeUTF("This is a test");
dOut.flush();
System.out.println("Work");
socket.close();
} catch (IOException e) {
this.ioException = e;
}
} }