Android线程,后台进程

时间:2014-12-28 12:22:08

标签: android multithreading sockets background nullpointerexception

我想在我的Arraylist上添加ipaddress来更新我的listview,它有自己的适配器但我收到一个错误,当它检测到服务器套接字时它会停止我的应用程序。此代码在后台运行,后台扫描子网的ipaddress并将地址保存到PostList类。

public class MainActivity extends Activity {

ListView lv;
ArrayList<PostList> list;
ArrayAdapter<PostList> adapter;
int port = 50000;
String ipadd = "192.168.254.";


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

    lv = (ListView)findViewById(R.id.iplist);



        scanIP(ipadd,port);


}

private void scanIP(final String ipadd, final int port){
   final Handler mHandler = new Handler();


   new Thread(new Runnable() {


    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (int i = 95; i < 105; i++){
            final String ipaddress = ipadd + i;   
        try {
            Socket socket = new Socket(ipaddress,port);
            if (socket.isConnected()){
                  try {
                        Log.d("ClientActivity", "C: Sending command.");
                        PrintWriter out = new PrintWriter(new BufferedWriter(new   OutputStreamWriter(socket
                                    .getOutputStream())), true);
                            out.println("Hey Server im " + ipaddress);
                            Log.d("ClientActivity", "C: Sent.");
                    } catch (Exception e) {
                        Log.e("ClientActivity", "S: Error", e);
                    }

                  runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        insertValue(ipaddress);
                    }


                });


            }
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 }
}).start();    


}
private void insertValue(String ipaddress) {
    // TODO Auto-generated method stub
    list.add(new PostList(ipaddress));
    adapter = new PostAdapter(MainActivity.this, list);
    lv.setAdapter(adapter);
}

}

Logcat错误,当它获取我的服务器IP地址时显示此错误

12-28 20:15:17.579: I/System.out(15675): [CDS]connect[/192.168.254.100:50000] tm:90
12-28 20:15:17.580: D/Posix(15675): [Posix_connect Debug]Process com.example.practice_socket :50000 
12-28 20:15:17.586: D/ClientActivity(15675): C: Sending command.
12-28 20:15:17.587: D/ClientActivity(15675): C: Sent.
12-28 20:15:17.588: D/AndroidRuntime(15675): Shutting down VM
12-28 20:15:17.588: W/dalvikvm(15675): threadid=1: thread exiting with uncaught exception (group=0x411da9a8)
12-28 20:15:17.589: E/test(15675): Exception
12-28 20:15:17.590: E/AndroidRuntime(15675): FATAL EXCEPTION: main
12-28 20:15:17.590: E/AndroidRuntime(15675): java.lang.NullPointerException
12-28 20:15:17.590: E/AndroidRuntime(15675):    at com.example.practice_socket.MainActivity.insertValue(MainActivity.java:95)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at com.example.practice_socket.MainActivity.access$0(MainActivity.java:93)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at com.example.practice_socket.MainActivity$1$1.run(MainActivity.java:74)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at android.os.Handler.handleCallback(Handler.java:725)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at android.os.Looper.loop(Looper.java:153)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at android.app.ActivityThread.main(ActivityThread.java:5299)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at java.lang.reflect.Method.invokeNative(Native Method)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at java.lang.reflect.Method.invoke(Method.java:511)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-28 20:15:17.590: E/AndroidRuntime(15675):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

确保在调用list.add()之前已实例化列表对象:

list = new ArrayList<PostList>();