我正在创建一个Android应用程序,它使用AllJoyn连接到Lifx灯泡,并提供控制此灯泡的功能。我使用教程应用程序和AllJoyn的示例应用程序演示他们的闪电sdk作为基础。
当加载应用程序时,它会向用户提供网络上当前灯泡的列表。当应用程序被发送到后台(使用后退键而不是主页键)然后恢复时,LightingDirector返回一个空数组,好像没有灯,我不知道为什么。我注意到他们的Android照明教程应用程序也不会在从后台恢复时显示我的灯泡,所以我怀疑是一个SDK错误。
要解决这个问题,我目前在我的onDestroy()方法中使用System.exit(0)来解决问题,但这不是一个好的解决方案。有谁知道解决这个问题的方法?
public class MainActivity extends AppCompatActivity implements NextControllerConnectionListener {
private static final int CONTROLLER_CONNECTION_DELAY = 5000;
public LightingDirector lightingDirector;
private ListView lv;
private ItemListView lampAdapter;
private ArrayList<Lamp> lamps = new ArrayList<Lamp>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.initViews();
// STEP 1: Initialize a lighting controller with default configuration.
LightingController lightingController = LightingController.get();
lightingController.init(new LightingControllerConfigurationBase(getApplicationContext().getFileStreamPath("").getAbsolutePath()));
lightingController.start();
// STEP 2: Instantiate the director, add the custom listener, then start.
lightingDirector = LightingDirector.get();
lightingDirector.setNetworkConnectionStatus(true);
lightingDirector.postOnNextControllerConnection(this, CONTROLLER_CONNECTION_DELAY);
lightingDirector.start("TutorialApp");
}
private void initViews() {
lv = (ListView) findViewById(R.id.list);
lampAdapter = new ItemListView(this, this.lamps);
lv.setAdapter(lampAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
goToPage2(position);
//send which one I have clicked on
}
});
}
private void goToPage2(int position) {
Intent dbIntent2 = new Intent(this, BulbSettingActivity.class);
dbIntent2.putExtra("position", position);
startActivity(dbIntent2);
}
@Override
public void onControllerConnected() {
Lamp[] lightingDirectorLamps = lightingDirector.getLamps();
if (lightingDirectorLamps.length > 0) {
// clear out whatever is in there
this.lamps.clear();
// add the lamps back
for (Lamp lamp : lightingDirectorLamps) {
this.lamps.add(lamp);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
lampAdapter.notifyDataSetChanged();
}
});
}
}
@Override
protected void onDestroy() {
lightingDirector.stop();
System.exit(0);
super.onDestroy();
}
}
在LFS教程应用程序中,我得到了例外:
02-19 10:02:44.746 16855-16885 / org.allseen.lsf.tutorial I / System.out:AllJoynManager.stop():成功
02-19 10:02:44.771 16855-16885 / org.allseen.lsf.tutorial I / System.out:AllJoynManager.destroy()
02-19 10:02:50.496 16855-16855 / org.allseen.lsf.tutorial E / AndroidRuntime:FATAL EXCEPTION:main
处理:org.allseen.lsf.tutorial,PID:16855
java.lang.RuntimeException:无法启动活动ComponentInfo {org.allseen.lsf.tutorial / org.allseen.lsf.tutorial.TutorialActivity}:java.lang.NullPointerException:尝试调用虚方法'void java.lang .tread.interrupt()'在空对象引用
上在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3231)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3327)
在android.app.ActivityThread.access $ 1100(ActivityThread.java:221)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1771)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:158)
在android.app.ActivityThread.main(ActivityThread.java:7144)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:731)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void java.lang.Thread.interrupt()'
at org.allseen.lsf.sdk.manager.DefaultLightingSystemQueue.stop(DefaultLightingSystemQueue.java:116)
at org.allseen.lsf.sdk.manager.LightingSystemManager.setQueue(LightingSystemManager.java:231)
at org.allseen.lsf.sdk.manager.LightingSystemManager.init(LightingSystemManager.java:156)
at org.allseen.lsf.sdk.LightingDirector.start(LightingDirector.java:444)
at org.allseen.lsf.sdk.LightingDirector.start(LightingDirector.java:400)
at org.allseen.lsf.tutorial.TutorialActivity.onCreate(TutorialActivity.java:58)
在android.app.Activity.performCreate(Activity.java:6840)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3327)
在android.app.ActivityThread.access $ 1100(ActivityThread.java:221)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1771)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:158)
在android.app.ActivityThread.main(ActivityThread.java:7144)