我正在开发项目,使用Android应用程序打开LED,并与Wi-Fi进行通信。
问题是当我按下按钮将指令发送到远程设备时,应用程序总是崩溃(我正在使用Arduino + wifi shield)。我认为问题是我的线程构造和执行。任何人都可以解决我的问题,如何在android中正确使用线程类?
这是我的线程类的代码
package com.gfhz.appliancecontrol;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.widget.Toast;
public class ConnectionSocket extends Thread {
Socket client;
ConnectionInfoActivity conInfo;
String instructionCode;
String serverAddr;
OutputStream os;
DataOutputStream dos;
String stoast;
final int port = 2000;
ConnectionSocket(String instruction) {
// Get the DEVICE ID
String devId = conInfo.getDevId();
// Concatenate instruction message and device Id
instructionCode = devId.concat(instruction);
// Get the SERVER IP ADDRESS
serverAddr = "192.168.1.105";
}
@Override
public void run() {
//android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
try{
// Open a socket
client = new Socket(serverAddr, port);
dos = new DataOutputStream(client.getOutputStream());
dos.writeBytes(instructionCode);
} catch (UnknownHostException e) {
//TODO Auto-generated catch block
e.printStackTrace();
stoast = "Unknown Host Exception" + e.toString();
} catch (IOException e) {
// TODO Auto-generated cach block
e.printStackTrace();
// Display in TOAST
stoast = "IO Exception" + e.toString();
} finally {
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//Toast.makeText(getTContext(), stoast, Toast.LENGTH_SHORT).show();
}
}
}
这里是执行线程的代码
package com.gfhz.appliancecontrol;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class LightsMenuActivity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lights_menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_lights_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void powerLamp1 (View view) {
Thread cs = new ConnectionSocket("0401");
cs.start();
}
public void powerLamp2(View view) {
Thread cs = new ConnectionSocket("0402");
cs.start();
}
public void powerLamp3(View view){
Thread cs = new ConnectionSocket("0403");
cs.start();
}
}
logcat错误:
11-16 19:49:21.127: E/AndroidRuntime(7457): FATAL EXCEPTION: main
11-16 19:49:21.127: E/AndroidRuntime(7457): java.lang.IllegalStateException: Could not execute method of the activity
11-16 19:49:21.127: E/AndroidRuntime(7457): at android.view.View$1.onClick(View.java:2154)
11-16 19:49:21.127: E/AndroidRuntime(7457): at android.view.View.performClick(View.java:2538)
11-16 19:49:21.127: E/AndroidRuntime(7457): at android.view.View$PerformClick.run(View.java:9152)
11-16 19:49:21.127: E/AndroidRuntime(7457): at android.os.Handler.handleCallback(Handler.java:587)
11-16 19:49:21.127: E/AndroidRuntime(7457): at android.os.Handler.dispatchMessage(Handler.java:92)
11-16 19:49:21.127: E/AndroidRuntime(7457): at android.os.Looper.loop(Looper.java:130)
11-16 19:49:21.127: E/AndroidRuntime(7457): at android.app.ActivityThread.main(ActivityThread.java:3693)
11-16 19:49:21.127: E/AndroidRuntime(7457): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 19:49:21.127: E/AndroidRuntime(7457): at java.lang.reflect.Method.invoke(Method.java:507)
11-16 19:49:21.127: E/AndroidRuntime(7457): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-16 19:49:21.127: E/AndroidRuntime(7457): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-16 19:49:21.127: E/AndroidRuntime(7457): at dalvik.system.NativeStart.main(Native Method)
11-16 19:49:21.127: E/AndroidRuntime(7457): Caused by: java.lang.reflect.InvocationTargetException
11-16 19:49:21.127: E/AndroidRuntime(7457): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 19:49:21.127: E/AndroidRuntime(7457): at java.lang.reflect.Method.invoke(Method.java:507)
11-16 19:49:21.127: E/AndroidRuntime(7457): at android.view.View$1.onClick(View.java:2149)
11-16 19:49:21.127: E/AndroidRuntime(7457): ... 11 more
11-16 19:49:21.127: E/AndroidRuntime(7457): Caused by: java.lang.NullPointerException
11-16 19:49:21.127: E/AndroidRuntime(7457): at com.gfhz.appliancecontrol.ConnectionSocket.<init>(ConnectionSocket.java:25)
11-16 19:49:21.127: E/AndroidRuntime(7457): at com.gfhz.appliancecontrol.LightsMenuActivity.powerLamp1(LightsMenuActivity.java:37)
11-16 19:49:21.127: E/AndroidRuntime(7457): ... 14 more
答案 0 :(得分:0)
您的logcat显示ConnectionSocket类的第25行存在NullPointerException。
这就是应用程序崩溃的原因。
除此之外,我建议您研究Android特定的背景线程实现,例如AsyncTask。
它们比原始的Thread类更容易使用。