我正在研究如何在tabhost活动中绑定服务
我有一个名为Remoteserviceconnection的依赖项目 我在tabhost子活动
中调用inokeservice()正如我所说,我有一个带有三个子活动的tabhost活动,我想在子活动中绑定一个服务,我得到nul指针异常。
我经历了这两个链接仍然没有得到任何想法
Binding Multiple Activities(Tabs) to a Service using a Base Class Activity
http://code.google.com/p/android/issues/detail?id=2483
我的项目中有4项活动
1)TabBarExample.java 2)FirstTab.java 3)SecondTab.java 4)ThirdTab.java
我究竟做错了什么?
我们非常感谢任何帮助,谢谢
这是我的代码,我在这个java文件中遇到错误,
FirstTab.java
public class FirstTab extends Activity {
protected static final String TAG = "HvacActivity";
/** Called when the activity is first created. */
private IMyRemoteService remoteService;
private boolean started = false;
private RemoteServiceConnection conn = null;
private Handler serviceHandler;
private static int speed;
private static int hvactemp;
private static int hvacTemppass;
private Task myTask = new Task();
private ImageView fanimgview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.hvac);
if(started == false )
startService();
bindService();
System.gc();
serviceHandler = new Handler();
serviceHandler.postDelayed(myTask, 1000L);
}
class RemoteServiceConnection implements ServiceConnection {
// static final int hvactemp = 0;
public void onServiceConnected(ComponentName className,
IBinder boundService) {
remoteService = IMyRemoteService.Stub
.asInterface((IBinder) boundService);
Log.d(getClass().getSimpleName(), "onServiceConnected()");
}
public void onServiceDisconnected(ComponentName className) {
remoteService = null;
// updateServiceStatus();
Log.d(getClass().getSimpleName(), "onServiceDisconnected");
}
};
private void startService() {
if (started) {
// Toast.makeText(CarHome.this, "Service already started",
// Toast.LENGTH_SHORT).show();
} else {
Intent i = new Intent();
i.setClassName("com.msat.home.clusterservices",
"com.msat.home.clusterservices.RemoteService");
startService(i);
started = true;
updateServiceStatus();
Log.d(getClass().getSimpleName(), "startService()");
}
}
private void stopService() {
if (!started) {
// drivertmpcount.setText(Integer.toString(hvactemp)); //
// Toast.makeText(CarHome.this, "Service not yet started",
// Toast.LENGTH_SHORT).show();
} else {
Intent i = new Intent();
i.setClassName("com.msat.home.clusterservices",
"com.msat.home.clusterservices.RemoteService");
stopService(i);
started = false;
updateServiceStatus();
Log.d(getClass().getSimpleName(), "stopService()");
}
}
private void bindService() {
if (conn == null) {
conn = new RemoteServiceConnection();
Intent i = new Intent();
i.setClassName("com.msat.home.clusterservices",
"com.msat.home.clusterservices.RemoteService");
bindService(i, conn, Context.BIND_AUTO_CREATE);
updateServiceStatus();
Log.d(getClass().getSimpleName(), "bindService()");
} else {
// Toast.makeText(CarHome.this,
// "Cannot bind - service already bound",
// Toast.LENGTH_SHORT).show();
}
}
private void releaseService() {
if (conn != null) {
unbindService(conn);
conn = null;
updateServiceStatus();
Log.d(getClass().getSimpleName(), "releaseService()");
} else {
// Toast.makeText(CarHome.this, "Cannot unbind - service not bound",
// Toast.LENGTH_SHORT).show();
}
}
private void invokeService() { // getting ERROR here
if (conn == null) {
// Toast.makeText(CarHome.this, "Cannot invoke - service not bound",
// Toast.LENGTH_SHORT).show();
} else {
try {
System.out.println(remoteService);
final TextView drivertmpcount = (TextView) findViewById(R.id.curtempcount);
// final TextView tempcountpass = (TextView)
// findViewById(R.id.tempcountpass);
hvactemp = remoteService.getHvacTemp(); // getting ERROR here
hvacTemppass = remoteService.getHvacTemppass();
System.out.println("Raghav hvac" + hvactemp);
System.out.println("jaydeep speed" + speed);
// rpm_text.setText(rpm);
drivertmpcount.setText(Integer.toString(hvactemp));
// tempcountpass.setText(Integer.toString(hvacTemppass));
Log.d(getClass().getSimpleName(), "invokeService()");
} catch (RemoteException re) {
Log.e(getClass().getSimpleName(), "RemoteException");
}
}
}
private void updateServiceStatus() {
String bindStatus = conn == null ? "unbound" : "bound";
String startStatus = started ? "started" : "not started";
String statusText = "Service status: " + bindStatus + "," + startStatus;
// TextView t = (TextView)findViewById( R.id.serviceStatus);
// t.setText( statusText );
System.out.println("Jaydeep : " + statusText);
}
protected void onDestroy() {
super.onDestroy();
releaseService();
Log.d(getClass().getSimpleName(), "onDestroy()");
}
class Task implements Runnable {
public void run() {
invokeService(); // getting ERROR here
// serviceHandler.postDelayed(this, 1000L);
//Log.i(getClass().getSimpleName(),
// "Incrementing engineRPM in the run method");
}
}
}
LOGCAT消息
09-06 19:12:36.550: E/AndroidRuntime(14116): FATAL EXCEPTION: main
09-06 19:12:36.550: E/AndroidRuntime(14116): java.lang.NullPointerException
09-06 19:12:36.550: E/AndroidRuntime(14116): at com.hvaccontroller.msat.FirstTab.invokeService(FirstTab.java:145)
09-06 19:12:36.550: E/AndroidRuntime(14116): at com.hvaccontroller.msat.FirstTab.access$1(FirstTab.java:132)
09-06 19:12:36.550: E/AndroidRuntime(14116): at com.hvaccontroller.msat.FirstTab$Task.run(FirstTab.java:180)
09-06 19:12:36.550: E/AndroidRuntime(14116): at android.os.Handler.handleCallback(Handler.java:587)
09-06 19:12:36.550: E/AndroidRuntime(14116): at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 19:12:36.550: E/AndroidRuntime(14116): at android.os.Looper.loop(Looper.java:130)
09-06 19:12:36.550: E/AndroidRuntime(14116): at android.app.ActivityThread.main(ActivityThread.java:3686)
09-06 19:12:36.550: E/AndroidRuntime(14116): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 19:12:36.550: E/AndroidRuntime(14116): at java.lang.reflect.Method.invoke(Method.java:507)
09-06 19:12:36.550: E/AndroidRuntime(14116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-06 19:12:36.550: E/AndroidRuntime(14116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-06 19:12:36.550: E/AndroidRuntime(14116): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
此remoteService
代码null
对象为hvactemp = remoteService.getHvacTemp();
。
看起来您期望该服务将在1秒内连接。因为你在调用bindService
1秒后调用invokeService()。在您的任务中检查remoteService
对象是否为空。如果对象不为null,则在对象为invokeService()
时调用null
方法,然后注册另一个回调,如下所示:
class Task implements Runnable {
public void run() {
if(remoteService !=null){
invokeService(); // getting ERROR here
} else{
serviceHandler.postDelayed(this, 1000L);
}
//Log.i(getClass().getSimpleName(),
// "Incrementing engineRPM in the run method");
}
}
EDIT1:
或者您可以致电,您可以从invokeService()
方法调用onServiceConnected()
方法以避免异常