package com.test.server;import android.app.Activity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
import android.widget.EditText;public class TestServerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        receivedate();
    }
EditText et=(EditText)findViewById(R.id.editText1);
ServerSocket server;
    public void receivedate(){
     try {
      server=new ServerSocket(5556);
     }catch(IOException e) {
     e.printStackTrace();
     }
     beginlisten();
    }
    public void beginlisten(){
     while(true){
     try{
     final Socket socket=server.accept();
     new Thread(new Runnable(){
     public void run(){
     BufferedReader in;
     try{
     in=new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
     while(!socket.isClosed()){
     String str;
     str=in.readLine();
     et.setText(str);
     }
     socket.close();
     }catch(IOException e){
     e.printStackTrace();
     }
     }
     }).start();
     }catch(IOException e){
     e.printStackTrace();
     }
     }
    }
    
}
LogCat显示:02-26 11:20:24.152: D/AndroidRuntime(266): Shutting down VM
02-26 11:20:24.152: W/dalvikvm(266): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-26 11:20:24.213: E/AndroidRuntime(266): FATAL EXCEPTION: main
02-26 11:20:24.213: E/AndroidRuntime(266): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.server/com.test.server.TestServerActivity}: java.lang.NullPointerException
02-26 11:20:24.213: E/AndroidRuntime(266):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
02-26 11:20:24.213: E/AndroidRuntime(266):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-26 11:20:24.213: E/AndroidRuntime(266):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-26 11:20:24.213: E/AndroidRuntime(266):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-26 11:20:24.213: E/AndroidRuntime(266):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 11:20:24.213: E/AndroidRuntime(266):  at android.os.Looper.loop(Looper.java:123)
02-26 11:20:24.213: E/AndroidRuntime(266):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-26 11:20:24.213: E/AndroidRuntime(266):  at java.lang.reflect.Method.invokeNative(Native Method)
02-26 11:20:24.213: E/AndroidRuntime(266):  at java.lang.reflect.Method.invoke(Method.java:521)
02-26 11:20:24.213: E/AndroidRuntime(266):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-26 11:20:24.213: E/AndroidRuntime(266):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-26 11:20:24.213: E/AndroidRuntime(266):  at dalvik.system.NativeStart.main(Native Method)
02-26 11:20:24.213: E/AndroidRuntime(266): Caused by: java.lang.NullPointerException
02-26 11:20:24.213: E/AndroidRuntime(266):  at android.app.Activity.findViewById(Activity.java:1637)
02-26 11:20:24.213: E/AndroidRuntime(266):  at com.test.server.TestServerActivity.<init>(TestServerActivity.java:20)
02-26 11:20:24.213: E/AndroidRuntime(266):  at java.lang.Class.newInstanceImpl(Native Method)
02-26 11:20:24.213: E/AndroidRuntime(266):  at java.lang.Class.newInstance(Class.java:1429)
02-26 11:20:24.213: E/AndroidRuntime(266):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-26 11:20:24.213: E/AndroidRuntime(266):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
02-26 11:20:24.213: E/AndroidRuntime(266):  ... 11 more
02-26 11:20:51.462: I/Process(266): Sending signal. PID: 266 SIG: 9

解决方案 »

  1.   

    Unable to instantiate activity ComponentInfo{com.test.server/com.test.server.TestServerActivity}: java.lang.NullPointerException
    Caused by: java.lang.NullPointerException
    at com.test.server.TestServerActivity.<init>(TestServerActivity.java:20)EditText et=(EditText)findViewById(R.id.editText1);
    这个不是你这样写的,是需要放在方法中的,比如 onCreate中
    如果你要全局可以这样    private EditText et;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // some code....
            this.et = (EditText)findViewById(R.id.editText1);
        }
      

  2.   

    已经说的很清楚了、要学会自己看LogCat信息。