我在android中有一个应用程序,类似于客户端服务器应用程序,在我的程序中,在某个时刻我开始一个新的线程,意味着将客户端连接到服务器。
我的问题是,当我开始这个新线程时,我不知道会发生什么,但是当我尝试与我的GUI交互时(按下一个按钮将我带到另一个活动)我会强行关闭。
这有点令人困惑,因为我在不同的网页上看到我开始使用的新线程与我的GUI无关,所以即使客户端尝试连接或失败,我也应该能够 用我的界面“玩”。
这是我的代码:
public 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);
//这是让我去另一个活动的按钮,当按下它时,它给了我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);
}
});
//这里我开始在下面定义一个新线程
Thread cThread=new Thread(new ClientThread());
cThread.start();
db=new DBAdapter(this);
}
//这是我的客户端尝试连接
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();
}
}
}
和onStop()方法:
protected void onStop() {
super.onStop();
try {
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
嗯,就是这样,有关详细信息,我在这里解释一下。谢谢你提前:)
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)
好吧,亲爱的朋友们,我已经从我的代码中删除了onStop()函数,一切似乎都有效!
protected void onStop(){
super.onStop();
try {
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
它有什么问题?: - S
答案 0 :(得分:2)
我假设线程的out
方法中的变量onStop
为null
。您声明了变量,但是使用null
初始化它并且从不为其初始化另一个值。
你做
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
在线程的run
方法中,但此变量的范围与onStop
方法中使用的变量不同。这就是你得到NullPointerException
的原因。