android上的客户端 - 服务器连接

时间:2013-12-05 13:55:20

标签: android

 public class MainActivity extends Activity {

    private Socket client;
    private PrintWriter p;
    TextView text;
    Button button;
    String SocketAddress = "192.168.1.5";
    int port = 7777;
    private boolean connected = true;
    private View connect;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        text = (TextView) findViewById(R.id.text);

        connect.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {


                try {

                    ConnectivityManager cm = (ConnectivityManager)
                            getSystemService(Context.CONNECTIVITY_SERVICE);
                    Log.d("ClientActivity", "C: Connecting...");
                    Socket socket = new Socket("192.168.1.5", 7777); // connect to server
                    setConnected(true);

                } 

                  catch (IOException e) 
                  {
                    e.printStackTrace();
                  setConnected(false);
                }
            } 
        });
    }

    public boolean isConnected() {
        return connected;
    }

    public void setConnected(boolean connected) {
        this.connected = connected;
    }
 }

这是我的logcat

12-05 03:44:14.174: D/AndroidRuntime(1214): Shutting down VM
12-05 03:44:14.174: W/dalvikvm(1214): threadid=1: thread exiting with uncaught exception (group=0x41465700)
12-05 03:44:14.290: E/AndroidRuntime(1214): FATAL EXCEPTION: main
12-05 03:44:14.290: E/AndroidRuntime(1214): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.serverconnection/com.example.serverconnection.SruthiActivity}: java.lang.NullPointerException
12-05 03:44:14.290: E/AndroidRuntime(1214):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at android.os.Looper.loop(Looper.java:137)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at java.lang.reflect.Method.invoke(Method.java:525)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at dalvik.system.NativeStart.main(Native Method)
12-05 03:44:14.290: E/AndroidRuntime(1214): Caused by: java.lang.NullPointerException
12-05 03:44:14.290: E/AndroidRuntime(1214):     at com.example.serverconnection.SruthiActivity.onCreate(SruthiActivity.java:38)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at android.app.Activity.performCreate(Activity.java:5133)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-05 03:44:14.290: E/AndroidRuntime(1214):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-05 03:44:14.290: E/AndroidRuntime(1214):     ... 11 more
12-05 03:44:28.790: I/Process(1214): Sending signal. PID: 1214 SIG: 9

帮助我连接到服务器,提供正确的代码

1 个答案:

答案 0 :(得分:1)

您在第38行的NullPointerException中遇到SruthiActivity.onCreate 查看代码之后,很容易看到你声明一个名为connect的View但从未将其初始化为任何东西。
因此,当尝试为它分配一个onClickListener时,你失败了{{1 }}

你错过了一行如下:

NullPointerException