Doch noch nen kleines Problem
Hallo,
Also ich hab mir jetzt ne neue Anwendung zum TCP- Datenaustausch gebastelt.
Nun habe ich wieder ein Problem.
Ich müsste die Verbindung vom Client zum Server in einem seperaten Thread ausführen, leider bekomm ich das irgendwie nich hin.
Client Code:
package com.example.client;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.net.*;
import java.io.*;
public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	String ip = "0";
	ChatClient client = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_main);
    	
     	
     	
     	
	}
	
	public void onSendClick(View v)
	{
    	EditText nachricht = (EditText) this.findViewById(R.id.EditText01);
    	String msg = nachricht.getText().toString();
    	client.send(msg);
    	
	}
	
	public void onConnectClick(View v)
	{
    	EditText AdressEditText = (EditText) this.findViewById(R.id.EditText02);
    	ip = AdressEditText.getText().toString();
    	int port = 1337;
    	client =  new ChatClient(ip, port);
	}
}
class ChatClient
{  
   private Socket socket          	= null;
   private DataInputStream  console   = null;
   private DataOutputStream streamOut = null;
   public ChatClient(String serverName, int serverPort)
   {  System.out.println("Establishing connection. Please wait ...");
  	try
  	{  socket = new Socket(serverName, serverPort);
     	Log.d("DEBUG","Connected: " + socket);
     	start();
  	}
  	catch(UnknownHostException uhe)
  	{  Log.d("DEBUG","Host unknown: " + uhe.getMessage());
  	}
  	catch(IOException ioe)
  	{  Log.d("DEBUG","Unexpected exception: " + ioe.getMessage());
  	}
  	
 	
   }
   
   public void start() throws IOException
   {  
  	console   = new DataInputStream(System.in);
  	streamOut = new DataOutputStream(socket.getOutputStream());
   }
   
   public void stop()
   {  try
  	{  if (console   != null)  console.close();
     	if (streamOut != null)  streamOut.close();
     	if (socket	!= null)  socket.close();
  	}
  	catch(IOException ioe)
  	{  System.out.println("Error closing ...");
  	}
   }
   
   public void send(String msg)
   {
   	try
   	{  
      	streamOut.writeUTF(msg);
      	streamOut.flush();
   	}
   	catch(IOException ioe)
   	{  
       	Log.d("DEBUG","Sending error: " + ioe.getMessage());
   	}
   }
  
}
	
			Alles anzeigen
	Server-Code:
import java.net.*;
import java.io.*;
public class Server implements Runnable
{  private Socket   	socket = null;
   private ServerSocket server = null;
   private Thread   	thread = null;
   private DataInputStream  streamIn  =  null;
   public Server()
   {  
  	int port = 4444; 
  	try
  	{  System.out.println("Binding to port " + port + ", please wait  ...");
     	server = new ServerSocket(port);  
     	System.out.println("Server started: " + server);
     	start();
  	}
  	catch(IOException ioe)
  	{  
      	System.out.println(ioe); 
  	}
   }
   public void run()
   {  while (thread != null)
  	{   try
     	{  System.out.println("Waiting for a client ..."); 
        	socket = server.accept();
        	System.out.println("Client accepted: " + socket);
        	open();
        	boolean done = false;
        	while (!done)
        	{  try
           	{  String line = streamIn.readUTF();
              	System.out.println(line);
              	done = line.equals(".bye");
           	}
           	catch(IOException ioe)
           	{  done = true;  }
        	}
        	close();
     	}
     	catch(IOException ie)
     	{  System.out.println("Acceptance Error: " + ie);  }
  	}
   }
   public void start()
   {  if (thread == null)
  	{  thread = new Thread(this); 
     	thread.start();
  	}
   }
   @SuppressWarnings("deprecation")
   public void stop()
   {  if (thread != null)
  	{  thread.stop(); 
     	thread = null;
  	}
   }
   public void open() throws IOException
   {  streamIn = new DataInputStream(new 
                    	BufferedInputStream(socket.getInputStream()));
   }
   public void close() throws IOException
   {  if (socket != null)	socket.close();
  	if (streamIn != null)  streamIn.close();
   }
   public static void main(String args[])
   {  
   	Server server =  new Server();
   }
}
	
			Alles anzeigen
	Mainfest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.example.client"
	android:versionCode="1"
	android:versionName="1.0" >
	<uses-sdk
    	android:minSdkVersion="8"
    	android:targetSdkVersion="15" />
	<uses-permission android:name=""/>
	<application
    	android:icon="@drawable/ic_launcher"
    	android:label="@string/app_name"
    	android:theme="@style/AppTheme" >
    	<activity
        	android:name=".MainActivity"
        	android:label="@string/title_activity_main" >
        	<intent-filter>
            	<action android:name="android.intent.action.MAIN" />
            	<category android:name="android.intent.category.LAUNCHER" />
        	</intent-filter>
    	</activity>
	</application>
</manifest>
	
			Alles anzeigen
	Logcat:
08-10 15:08:44.500: D/OpenGLRenderer(26478 
 Flushing caches (mode 0)
08-10 15:08:49.700: D/AndroidRuntime(26478 
 Shutting down VM
08-10 15:08:49.700: W/dalvikvm(26478 
 threadid=1: thread exiting with uncaught exception (group=0x40a401f8 )
08-10 15:08:49.710: E/AndroidRuntime(26478 
 FATAL EXCEPTION: main
08-10 15:08:49.710: E/AndroidRuntime(26478 
 java.lang.IllegalStateException: Could not execute method of the activity
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at android.view.View$1.onClick(View.java:3044)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at android.view.View.performClick(View.java:3511)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at android.view.View$PerformClick.run(View.java:14110)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at android.os.Handler.handleCallback(Handler.java:605)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at android.os.Handler.dispatchMessage(Handler.java:92)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at android.os.Looper.loop(Looper.java:137)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at android.app.ActivityThread.main(ActivityThread.java:4424)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at java.lang.reflect.Method.invokeNative(Native Method)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at java.lang.reflect.Method.invoke(Method.java:511)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at dalvik.system.NativeStart.main(Native Method)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 Caused by: java.lang.reflect.InvocationTargetException
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at java.lang.reflect.Method.invokeNative(Native Method)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at java.lang.reflect.Method.invoke(Method.java:511)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at android.view.View$1.onClick(View.java:3039)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 ... 11 more
08-10 15:08:49.710: E/AndroidRuntime(26478 
 Caused by: java.lang.NullPointerException
08-10 15:08:49.710: E/AndroidRuntime(26478 
 at com.example.client.MainActivity.onSendClick(MainActivity.java:31)
08-10 15:08:49.710: E/AndroidRuntime(26478 
 ... 14 more
08-10 15:08:51.030: I/Process(26478 
 Sending signal. PID: 26478 SIG: 9
08-10 15:09:11.300: D/AndroidRuntime(26669): Shutting down VM
08-10 15:09:11.300: W/dalvikvm(26669): threadid=1: thread exiting with uncaught exception (group=0x40a401f8 )
08-10 15:09:11.310: E/AndroidRuntime(26669): FATAL EXCEPTION: main
08-10 15:09:11.310: E/AndroidRuntime(26669): java.lang.IllegalStateException: Could not execute method of the activity
08-10 15:09:11.310: E/AndroidRuntime(26669): at android.view.View$1.onClick(View.java:3044)
08-10 15:09:11.310: E/AndroidRuntime(26669): at android.view.View.performClick(View.java:3511)
08-10 15:09:11.310: E/AndroidRuntime(26669): at android.view.View$PerformClick.run(View.java:14110)
08-10 15:09:11.310: E/AndroidRuntime(26669): at android.os.Handler.handleCallback(Handler.java:605)
08-10 15:09:11.310: E/AndroidRuntime(26669): at android.os.Handler.dispatchMessage(Handler.java:92)
08-10 15:09:11.310: E/AndroidRuntime(26669): at android.os.Looper.loop(Looper.java:137)
08-10 15:09:11.310: E/AndroidRuntime(26669): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-10 15:09:11.310: E/AndroidRuntime(26669): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 15:09:11.310: E/AndroidRuntime(26669): at java.lang.reflect.Method.invoke(Method.java:511)
08-10 15:09:11.310: E/AndroidRuntime(26669): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-10 15:09:11.310: E/AndroidRuntime(26669): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-10 15:09:11.310: E/AndroidRuntime(26669): at dalvik.system.NativeStart.main(Native Method)
08-10 15:09:11.310: E/AndroidRuntime(26669): Caused by: java.lang.reflect.InvocationTargetException
08-10 15:09:11.310: E/AndroidRuntime(26669): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 15:09:11.310: E/AndroidRuntime(26669): at java.lang.reflect.Method.invoke(Method.java:511)
08-10 15:09:11.310: E/AndroidRuntime(26669): at android.view.View$1.onClick(View.java:3039)
08-10 15:09:11.310: E/AndroidRuntime(26669): ... 11 more
08-10 15:09:11.310: E/AndroidRuntime(26669): Caused by: java.lang.NullPointerException
08-10 15:09:11.310: E/AndroidRuntime(26669): at com.example.client.MainActivity.onSendClick(MainActivity.java:31
Ich würde mich freuen wenn mir jemand helfen könnte.
P.S. wenn das Programm laufen sollte möchte ich ein Image übertragen. 
Vllt. hat ja einer nen Beispiel bzw. ne Seite mit nem Tutorial für mich.
Danke!
Gruß
Klaus
Edit: Vielleicht könnte ja jemand einfach mal den Code in Eclipse laufen lassen. Ich bin echt am verzweifeln, krieg einfach keinen funktionierenden Code hin. Ist wahrscheinlich nur ne kleinigkeit aber ich hab da irgendwie nen völligen Blackout! Danke.