Randroid
Randroid

Reputation: 3698

Android: How to get a variable from a service to tabhost child activity

I have a Tabhost with 3 child activities

1)TabbarExample.java //tab activity

2)FirstTab.java //child activity

3)SecondTab.java // child activity

4)ThirdTab.java // child activity

So for the first child activity of tabhost, getApplicationContext.bindService() is working fine and I could able to get the data from service and displaying on UI.

The actual thing is I am accessing the values(say speed,temp) from remote service to the First child activity and dispalying on the UI

and for the second child activity of tabhost do i need to repeat the same code which I have in my first child activity.

I dont want to repeat the same code for the 2nd and 3rd child activities of tabhost.

For the second child activity(SecondTab.java) I have done some changes but the UI is not getting the data.

In the second child activity I am not getting the data displayed rather i am getting the errors.

        @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);


}

These are the error messages

      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

Upvotes: 0

Views: 320

Answers (1)

tim
tim

Reputation: 1738

It looks like remoteService in onCreate isn't initialized, which is why your getting that NullPointerException. You initialize remoteService in the onServiceConnected method but don't use it there. Unless it's a member variable and onServiceConnected isn't called before the onCreate method.

That might explain the errors your getting, not sure what your asking otherwise - could you clarify?

Upvotes: 0

Related Questions