我正在创建一个使用 AllJoyn 连接到 Lifx 灯泡并提供控制此灯泡的功能的 Android 应用程序。我使用了来自 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: 致命异常: 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.Thread。空对象引用上的中断()'
在 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)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:731)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void java.lang.Thread.interrupt()”
在 org.allseen.lsf.sdk.manager.DefaultLightingSystemQueue.stop(DefaultLightingSystemQueue.java:116)
在 org.allseen.lsf.sdk.manager.LightingSystemManager.setQueue(LightingSystemManager.java:231)
在 org.allseen.lsf.sdk.manager.LightingSystemManager.init(LightingSystemManager.java:156)
在 org.allseen.lsf.sdk.LightingDirector.start(LightingDirector.java:444)
在 org.allseen.lsf.sdk.LightingDirector.start(LightingDirector.java:400)
在 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)