Android:如何从服务获取变量到tabhost子活动

时间:2012-11-30 07:44:47

标签: android user-interface service android-tabhost childactivity

我有一个包含3个子活动的Tabhost

1)TabbarExample.java //标签活动

2)FirstTab.java //子活动

3)SecondTab.java //子活动

4)ThirdTab.java //子活动

因此,对于tabhost的第一个子活动,getApplicationContext.bindService()工作正常,我可以从服务中获取数据并在UI上显示。

实际上我正在访问从远程服务到第一个子活动的值(比如speed,temp)和在UI上显示

对于tabhost的第二个子活动,我需要重复我在第一个子活动中使用的相同代码。

我不想为tabhost的第2和第3个子活动重复相同的代码。

对于第二个子活动(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

1 个答案:

答案 0 :(得分:0)

看起来onCreate中的remoteService没有初始化,这就是你得到NullPointerException的原因。您在onServiceConnected方法中初始化remoteService但不在那里使用它。除非它是一个成员变量,并且在onCreate方法之前没有调用onServiceConnected。

这可能解释了你得到的错误,不知道你的要求是什么 - 你能否澄清一下?