我有一个包含3个子活动的Tabhost
1) 选项卡Example.java//选项卡活动
2) FirstTab.java//子活动
3) SecondTab.java//子活动
4) ThirdTab.java//子活动
因此,对于tabhost的第一个子活动,getApplicationContext.bindService()运行良好,我可以从服务中获取数据并在UI上显示。
实际情况是,我正在访问从远程服务到First子活动的值(比如速度、温度),并在UI 上显示
对于tabhost的第二个子活动,我需要重复第一个子活动中的相同代码吗。
我不想为tabhost的第二个和第三个孩子的活动重复相同的代码。
对于第二个子活动(SecondTab.java),我做了一些更改,但UI没有得到数据。
在第二个子活动中,我没有得到显示的数据,而是得到了错误。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hvacpass);
// ((SecondTab) getApplicationContext()).doBindService();
// doBindService();
try {
passcurtempcount = (TextView) findViewById(R.id.passcurtempcount);
hvactemppass = remoteService.getHvacTemppass(); //GETTING ERRORS HERE
passcurtempcount.setText(Integer.toString(hvactemppass));
Toast.makeText(SecondTab.this, "Connected", Toast.LENGTH_SHORT)
.show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onServiceConnected(ComponentName className, IBinder boundService) {
// TODO Auto-generated method stub
doBindService();
remoteService = IMyRemoteService.Stub
.asInterface((IBinder) boundService);
Log.d(getClass().getSimpleName(), "onServiceConnected()");
Intent i = new Intent();
i.setClassName("com.msat.home.clusterservices",
"com.msat.home.clusterservices.RemoteService");
getApplicationContext().startService(i);
getApplicationContext().bindService(i, this, Context.BIND_AUTO_CREATE);
if(startService(i) != null) {
Toast.makeText(getBaseContext(), "service is already running", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getBaseContext(), "There is no service running, starting service..", Toast.LENGTH_SHORT).show();
}
}
public void onServiceDisconnected(ComponentName className) {
// TODO Auto-generated method stub
remoteService = null;
}
public void doBindService() {
getApplicationContext().bindService(
// getApplicationContext() fixed the problem here.
new Intent(getApplicationContext(), FirstTab.class), conn,
Context.BIND_AUTO_CREATE);
}
这些是错误信息
09-06 12:58:55.160: E/AndroidRuntime(2512): FATAL EXCEPTION: main
09-06 12:58:55.160: E/AndroidRuntime(2512): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hvaccontroller.msat/com.hvaccontroller.msat.SecondTab}: java.lang.NullPointerException
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1650)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.view.View.performClick(View.java:2485)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.view.View$PerformClick.run(View.java:9080)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.os.Handler.handleCallback(Handler.java:587)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.os.Looper.loop(Looper.java:130)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.app.ActivityThread.main(ActivityThread.java:3686)
09-06 12:58:55.160: E/AndroidRuntime(2512): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 12:58:55.160: E/AndroidRuntime(2512): at java.lang.reflect.Method.invoke(Method.java:507)
09-06 12:58:55.160: E/AndroidRuntime(2512): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-06 12:58:55.160: E/AndroidRuntime(2512): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-06 12:58:55.160: E/AndroidRuntime(2512): at dalvik.system.NativeStart.main(Native Method)
09-06 12:58:55.160: E/AndroidRuntime(2512): Caused by: java.lang.NullPointerException
09-06 12:58:55.160: E/AndroidRuntime(2512): at com.hvaccontroller.msat.SecondTab.onCreate(SecondTab.java:37)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-06 12:58:55.160: E/AndroidRuntime(2512): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1614)
09-06 12:58:55.160: E/AndroidRuntime(2512): ... 18 more
看起来onCreate中的remoteService没有初始化,这就是为什么您会得到NullPointerException。您在onServiceConnected方法中初始化remoteService,但不在那里使用它。除非它是一个成员变量,并且在onCreate方法之前没有调用onServiceConnected。
这可能解释了你得到的错误,不确定你的要求是什么——你能澄清一下吗?