我试图在Android Studio上创建一个应用程序,该应用程序使用Belkin WeMo SDK来控制我家周围的设备。我在onCreate方法中初始化了mWeMoSDKContext对象,如文档中所示。但是,当我去运行应用程序时,我一直得到NullPointerException并在调试后发现它在我初始化对象的行中。有没有什么办法解决这一问题?此外,对应用程序的任何建议也将不胜感激!
public class MyActivity extends AppCompatActivity implements NotificationListener {
private WeMoSDKContext mWeMoSDKContext = null;
private EditText devicesDisp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button onButton = (Button)findViewById(R.id.button);
Button offButton = (Button)findViewById(R.id.button2);
devicesDisp = (EditText)findViewById(R.id.textView2);
TextView device = (TextView) findViewById(R.id.textView);
**mWeMoSDKContext = new WeMoSDKContext(getApplicationContext());
mWeMoSDKContext.addNotificationListener(this);**
}
public class TextTask extends AsyncTask <String, String, String>{
@Override
protected String doInBackground(String... params) {
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
mWeMoSDKContext.refreshListOfWeMoDevicesOnLAN();
}
}
public void onNotify(final String event, final String udn) {
ArrayList<String> udns = mWeMoSDKContext.getListOfWeMoDevicesOnLAN();
int length = udns.size();
if (event.equals(WeMoSDKContext.REFRESH_LIST)) {
if(udns.size() == 0){
devicesDisp.setText("No Devices Detected");
}
else {
for (int i = 0; i < length; i++) {
WeMoDevice device = mWeMoSDKContext.getWeMoDeviceByUDN(udns.get(i));
String tempName = device.getFriendlyName();
devicesDisp.setText(tempName);
}
}
}
}
else if(event.equals(WeMoSDKContext.SET_STATE)) {
int numDevices = udns.size();
if(hourlyPrice > hourlyPriceLimit) {
for (int i = 0; i < numDevices; i++) {
String checkState = "WEMO_DEVICE_OFF";
WeMoDevice checkDevice = mWeMoSDKContext.getWeMoDeviceByUDN(udns.get(i));
if (!checkDevice.getState().equalsIgnoreCase("WEMO_DEVICE_OFF")){
devicesDisp.setText("Device state not changed for" + checkDevice);
}
}
}
else{
for (int i = 0; i < numDevices; i++) {
String checkState = "WEMO_DEVICE_ON";
WeMoDevice checkDevice = mWeMoSDKContext.getWeMoDeviceByUDN(udns.get(i));
if (!checkDevice.getState().equalsIgnoreCase("WEMO_DEVICE_ON")){
devicesDisp.setText("Device state not changed for" + checkDevice);
}
}
}
}
}
public void turnON(View view){
ArrayList<String> devices = mWeMoSDKContext.getListOfWeMoDevicesOnLAN();
int length = devices.size();
for(int i = 0; i < length; i++){
String state = "WEMO_DEVICE_ON";
WeMoDevice deviceState = mWeMoSDKContext.getWeMoDeviceByUDN(devices.get(i));
String type = deviceState.getType();
if (type.equals(WeMoDevice.SWITCH) || type.equals(WeMoDevice.INSIGHT)) {
mWeMoSDKContext.setDeviceState(state, devices.get(i));
}
}
}
public void turnOFF(View view){
ArrayList<String> devices = mWeMoSDKContext.getListOfWeMoDevicesOnLAN();
int length = devices.size();
for(int i = 0; i < length; i++){
String state = "WEMO_DEVICE_OFF";
WeMoDevice deviceState = mWeMoSDKContext.getWeMoDeviceByUDN(devices.get(i));
String type = deviceState.getType();
if (type.equals(WeMoDevice.SWITCH) || type.equals(WeMoDevice.INSIGHT)) {
mWeMoSDKContext.setDeviceState(state, devices.get(i));
}
}
}
protected void onDestroy() {
super.onDestroy();
mWeMoSDKContext.stop();
}
}
更新:这里是错误的堆栈跟踪
java.lang.NullPointerException: Attempt to get length of null array
at org.cybergarage.upnp.ControlPoint.start(ControlPoint.java:976)
at org.cybergarage.upnp.ControlPoint.start(ControlPoint.java:1051)
at com.belkin.wemo.localsdk.WeMoSDKContext$1.run(WeMoSDKContext.java:125)
at java.lang.Thread.run(Thread.java:818)