Android Socket连接问题

时间:2016-02-14 20:56:45

标签: java nullpointerexception

当尝试通过Android应用程序连接到我的笔记本电脑时,它一直在这里抓住logcat

02-14 21:22:29.307      354-375/? D/STATUSBAR-NotificationService﹕ Noti Alert - mSystemReady:true, AlertEnabled:true
02-14 21:22:29.393  14395-14395/ligandfas.rasptest I/System.out﹕ Starting startruuning
02-14 21:22:29.393  14395-14395/ligandfas.rasptest I/System.out﹕ starting Estabco
02-14 21:22:29.393  14395-14395/ligandfas.rasptest I/System.out﹕ trying to connect...
02-14 21:22:29.393  14395-14395/ligandfas.rasptest I/System.out﹕ keep going192.168.43.22
02-14 21:22:29.401  14395-14395/ligandfas.rasptest I/System.out﹕ Closing Connection...
02-14 21:22:29.401  14395-14395/ligandfas.rasptest D/AndroidRuntime﹕ Shutting down VM
02-14 21:22:29.401  14395-14395/ligandfas.rasptest W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41e2f2a0)
02-14 21:22:29.409      447-447/? D/StatusBar﹕ expanding top notification at 5entry.userCollapsed() = false
02-14 21:22:29.424  14395-14395/ligandfas.rasptest E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3698)
        at android.view.View.performClick(View.java:4222)
        at android.view.View$PerformClick.run(View.java:17337)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4895)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3693)
        at android.view.View.performClick(View.java:4222)
        at android.view.View$PerformClick.run(View.java:17337)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4895)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at ligandfas.rasptest.Network.closeConnection(Network.java:38)
        at ligandfas.rasptest.Network.startRunning(Network.java:70)
        at ligandfas.rasptest.MainActivity.controlRasp(MainActivity.java:22)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3693)
        at android.view.View.performClick(View.java:4222)
        at android.view.View$PerformClick.run(View.java:17337)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4895)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)

她是我的代码我有两个班级

package ligandfas.rasptest;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;


public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void controlRasp(View view){

    String mes = "loool";
    String ip = "192.168.43.22";

    Network net = new Network(ip,mes);
    System.out.println("Starting startruuning");
    net.startRunning();
}

}

和网络课程

package ligandfas.rasptest;

import java.io.*;
import java.net.*;



public class Network {
private Socket sock ;
private String serverIP,mes;
private ObjectOutputStream output;

public Network(String host,String lol){
    serverIP=host;
    mes=lol;
}


private void EstabCo() throws IOException {
    System.out.println("trying to connect...");
    System.out.println("keep going"+serverIP);
    sock= new Socket(InetAddress.getByName(serverIP), 6789);
    System.out.println("sock passed");
    System.out.println("Connection Established! Connected to: " + sock.getInetAddress().getHostName());
}

private void SetupStreams () throws IOException{
    System.out.println("Settingup Streams");

    output= new ObjectOutputStream(sock.getOutputStream());
    output.flush();
    System.out.println("Streams set UP");
}

private void closeConnection(){
    System.out.println("Closing Connection...");
    try{
        output.close();

        sock.close();
    }catch(IOException ioException){
        ioException.printStackTrace();
    }
    System.out.println("Connexion succesffully closed");
}

private void sendMessage(String message){
    System.out.println("Sending Message ...");
    try{
        output.writeObject(message);
        output.flush();

    }catch(IOException ioException){
        System.out.println("\n Oops! Something went wrong!");
    }
}
public void startRunning(){
    try{
        System.out.println("starting Estabco");
        EstabCo();
        System.out.println("Passed Estabco");
        SetupStreams();
        sendMessage(mes);

    }catch(EOFException eofException){
        System.out.println("\n Client terminated the connection");
    }catch(IOException ioException){
        ioException.printStackTrace();
    }finally{
        closeConnection();
    }
}
}

我认为这条线是我无法弄明白的问题

sock= new Socket(InetAddress.getByName(serverIP), 6789);

0 个答案:

没有答案