错误(日志中的前几行imp行)
09-11 00:25:48.250 19159-19159/com.example.nick.grab I/Process: Sending signal. PID: 19159 SIG: 9
09-11 00:50:27.871 19269-19269/com.example.nick.grab W/System: ClassLoader referenced unknown path: /data/app/com.example.nick.grab-2/lib/x86
09-11 00:50:29.105 19269-19269/com.example.nick.grab W/System: ClassLoader referenced unknown path: /data/app/com.example.nick.grab-2/lib/x86
09-11 00:50:29.313 19269-19269/com.example.nick.grab D/AndroidRuntime: Shutting down VM
09-11 00:50:29.313 19269-19269/com.example.nick.grab E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nick.grab, PID: 19269
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.nick.grab/com.example.nick.grab.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:72)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:191)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:183)
at com.example.nick.grab.MainActivity.<init>(MainActivity.java:10)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
MainActivity.java
package com.example.nick.grab;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText nput = (EditText) findViewById(R.id.editText);
TextView log = (TextView) findViewById(R.id.textView4);
TextView out = (TextView) findViewById(R.id.textView2);
TextView inp = (TextView) findViewById(R.id.textView3);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Server serverone = new Server(inp, out, log);
serverone.serverRun();
}
public void send (View view)
{
Client clientone = new Client(inp, out, log);
clientone.clientRun(nput.getText().toString());
}
}
Server.java
package com.example.nick.grab;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static Socket socket;
TextView k,l,m ;
public Server(TextView k, TextView l, TextView m)
{
this.k = k;
this.l = l;
this.m = m;
}
public void serverRun()
{
try
{
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
m.setText("Server Started and listening to port"+port);
while(true) // running
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String number = br.readLine();
l.setText("Message received from client is "+number);
//Multiplying the number by 2 and forming the return message
String returnMessage;
try
{
int numberInIntFormat = Integer.parseInt(number);
int returnValue = numberInIntFormat*2;
returnMessage = String.valueOf(returnValue) + "\n";
}
catch(NumberFormatException e)
{
//Input was not a number. Sending proper message back to client.
returnMessage = "Please send a proper number\n";
}
//Sending the response back to the client.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage);
l.setText("Message sent to the client is "+returnMessage);
bw.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}
Client.java
package com.example.nick.grab;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client extends MainActivity{
private static Socket socket;
TextView k,l,m ;
public Client(TextView k, TextView l, TextView m)
{
this.k = k;
this.l = l;
this.m = m;
}
public void clientRun(String input)
{
Scanner in = new Scanner(System.in);
try
{
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
m.setText("enter number");
String number = input;
String sendMessage = number + "\n";
bw.write(sendMessage);
bw.flush();
k.setText("Message sent to server : "+sendMessage);
// message from server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
k.setText("Message recieved from the server : "+message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
为什么我收到此错误? //这些是随机字符串,因为堆栈交换要求我向代码添加更多细节。我相信一个好的程序员只会从名字中知道这个问题,因为它不是什么新东西。所以在这里你先进行堆栈交换。有一些字符串。
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.example.nick.grab.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send to server"
android:id="@+id/button"
android:layout_marginTop="45dp"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:onClick="send" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textView2"
android:layout_alignTop="@+id/textView3"
android:text="IN_port" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/button"
android:text="OUT_port" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView4"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 0 :(得分:0)
您在findViewById
课程中致电Server
时收到错误消息。问题出在这里:Server extends MainActivity
。辅助类不应该扩展Activity
类本身。如果要修改TextView
实例,只需将它们单独传递给助手类即可。与您的Client
类相同。
答案 1 :(得分:0)
实际上我在活动的上下文准备好之前试图找到ViewById。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView log = (TextView) findViewById(R.id.textView4);
TextView out = (TextView) findViewById(R.id.textView2);
TextView inp = (TextView) findViewById(R.id.textView3);
Server serverone = new Server(inp, out, log);
serverone.serverRun();
}
public void send (View view)
{
EditText nput = (EditText) findViewById(R.id.editText);
TextView log = (TextView) findViewById(R.id.textView4);
TextView out = (TextView) findViewById(R.id.textView2);
TextView inp = (TextView) findViewById(R.id.textView3);
Client clientone = new Client(inp, out, log);
clientone.clientRun(nput.getText().toString());
}
}
现在代码正在运行。