I get force close when I start a new thread!
I have an app in android which is something like a client server application and inside my program at a certain point I start a new thread meant to connect the client to the server.
My problem is that when I start this new thread I don't know what happens but when I try to interact with my GUI(press a button that takes me to another activity) I get force close.
It's a little bit confusing cause as I read on different web pages the new thread I start has nothing to di with my GUI so even if the client tries to connect or fails I should be able to "play" with my interface.
Here is my code:
publi开发者_Go百科c class screen1 extends Activity {
private TextView clientState;
private String serverIpAddress="10.0.2.2";
public static final int ClientPort = 8080;
String message="Hello Server!";
int longitude;
int latitude;
private PrintWriter out=null;
DBAdapter db;
private Handler handler=new Handler();
Socket socket;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
clientState = (TextView) findViewById(R.id.client_Status);
//this is the button meant to get me to another activity and when pressed it gives me FC
Button b = (Button)findViewById(R.id.mainMenu);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(screen1.this, screen2.class);
startActivity(i);
}
});
//here I start a new thread defined below
Thread cThread=new Thread(new ClientThread());
cThread.start();
db=new DBAdapter(this);
}
//here it is my client that tries to connect
public class ClientThread implements Runnable{
public void run()
{
try
{
InetAddress serverAddr=InetAddress.getByName(serverIpAddress);
handler.post(new Runnable(){
public void run(){
clientState.setText(" try to connect!");
}
});
socket=new Socket(serverAddr,ClientPort);
//connected=true;
handler.post(new Runnable(){
public void run(){
clientState.setText("Connected!");
}
});
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
db.createDatabase();
db.openDataBase();
Cursor c=db.getAllData();
if(c.moveToFirst())
{
do{
longitude=Integer.parseInt(c.getString(1));
out.println(longitude);
latitude=Integer.parseInt(c.getString(2));
out.println(latitude);
}while(c.moveToNext());
}
}
catch(Exception e){
handler.post(new Runnable(){
public void run(){
clientState.setText("Error");
}
});
e.printStackTrace();
}
}
}
And onStop() method:
protected void onStop() {
super.onStop();
try {
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Well,this is it,for further details I'm here to explain.Thank u in advance:)
0
4-16 03:51:25.045: ERROR/AndroidRuntime(242): Uncaught handler: thread main exiting due to uncaught exception
04-16 03:51:25.074: ERROR/AndroidRuntime(242): java.lang.RuntimeException: Unable to stop activity {test.android/test.android.screen1}: java.lang.NullPointerException
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3227)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3272)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at android.app.ActivityThread.access$2500(ActivityThread.java:119)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1880)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at android.os.Looper.loop(Looper.java:123)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at java.lang.reflect.Method.invoke(Method.java:521)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at dalvik.system.NativeStart.main(Native Method)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): Caused by: java.lang.NullPointerException
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at
test.android.screen1.onStop(screen1.java:106)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at
android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1169)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at android.app.Activity.performStop(Activity.java:3797)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3224)
04-16 03:51:25.074: ERROR/AndroidRuntime(242): ... 11 more
04-16 03:51:25.144: INFO/Process(51): Sending signal. PID: 242 SIG: 3
04-16 03:51:25.154: INFO/dalvikvm(242): threadid=7: reacting to signal 3
04-16 03:51:25.204: INFO/dalvikvm(242): Wrote stack trace to '/data/anr/traces.txt'
04-16 03:51:41.604: INFO/Process(242): Sending signal. PID: 242 SIG: 9
04-16 03:51:41.714: INFO/ActivityManager(51): Process test.android (pid 242) has died.
04-16 03:51:41.753: INFO/WindowManager(51): WIN DEATH: Window{43e0eb10 test.android/test.android.screen2 paused=false}
04-16 03:51:41.765: INFO/WindowManager(51): WIN DEATH: Window{43e068b0 test.android/test.android.screen1 paused=false}
04-16 03:51:41.974: INFO/ActivityManager(51): Start proc test.android for activity test.android/.screen1: pid=249 uid=10030 gids={3003}
04-16 03:51:42.433: DEBUG/ddm-heap(249): Got feature list request
04-16 03:51:42.704: INFO/UsageStats(51): Unexpected resume of test.android while already resumed in test.android
04-16 03:51:43.294: WARN/InputManagerService(51): Got RemoteException sending setActive(false) notification to pid 242 uid 10030
04-16 03:51:43.513: INFO/ActivityManager(51): Displayed activity test.android/.screen1: 1736 ms (total 1736 ms)
Well,dear friends I've removed from my code the onStop() function and everything seems to work!
protected void onStop() {
super.onStop();
try {
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
What is wrong with it?:-S
I assume that the variable out
in your onStop
method of the thread is null
. You declare the variable, but you initialize it with null
and never initialize another value to it.
You do
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
in the run
method of your thread, but this variable has a different scope than the variable used in your onStop
method. That's why you get a NullPointerException
.
精彩评论