我已经为我的网络连接创建了一个服务类,以便我的应用程序可以创建连接,然后在整个程序中访问输入和输出流。我已经在第一个活动中实例化了服务类,但每当我尝试从服务类访问get方法时,我都会得到一个空指针异常。奇怪的是,当我调试程序时,它不会出现此错误。
以下是获取代码的类:
public class WaitingActivity extends Activity implements Runnable{
WaitingNetwork roleGetter;
String role;
int gotrole = 0;
Intent intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waitingroom);
Thread thread = new Thread(this);
thread.start();
ProgressDialog dialog = ProgressDialog.show(WaitingActivity.this, "",
"Loading. Please wait...", true);
}
Networking mService;
boolean mBound = false;
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, Networking.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
/** Called when a button is clicked (the button in the layout file attaches to
* this method with the android:onClick attribute) */
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
ObjectInputStream stream;
public void getStream(){
stream = mService.getStateOis();
}
@Override
public void run() {
getStream();
roleGetter = new WaitingNetwork(stream);
Thread fred = new Thread(roleGetter);
fred.start();
这是堆栈跟踪:
03-26 15:41:16.374: E/AndroidRuntime(1301): FATAL EXCEPTION: Thread-11
03-26 15:41:16.374: E/AndroidRuntime(1301): java.lang.NullPointerException
03-26 15:41:16.374: E/AndroidRuntime(1301): at com.DrawTastic.WaitingActivity.getStream(WaitingActivity.java:82)
03-26 15:41:16.374: E/AndroidRuntime(1301): at com.DrawTastic.WaitingActivity.run(WaitingActivity.java:88)
03-26 15:41:16.374: E/AndroidRuntime(1301): at java.lang.Thread.run(Thread.java:1019)
03-26 15:41:18.454: E/WindowManager(1301): Activity com.DrawTastic.WaitingActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405320a0 that was originally added here
03-26 15:41:18.454: E/WindowManager(1301): android.view.WindowLeaked: Activity com.DrawTastic.WaitingActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405320a0 that was originally added here
03-26 15:41:18.454: E/WindowManager(1301): at android.view.ViewRoot.<init>(ViewRoot.java:258)
03-26 15:41:18.454: E/WindowManager(1301): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
03-26 15:41:18.454: E/WindowManager(1301): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-26 15:41:18.454: E/WindowManager(1301): at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.Dialog.show(Dialog.java:241)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ProgressDialog.show(ProgressDialog.java:107)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ProgressDialog.show(ProgressDialog.java:90)
03-26 15:41:18.454: E/WindowManager(1301): at com.DrawTastic.WaitingActivity.onCreate(WaitingActivity.java:33)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-26 15:41:18.454: E/WindowManager(1301): at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 15:41:18.454: E/WindowManager(1301): at android.os.Looper.loop(Looper.java:123)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-26 15:41:18.454: E/WindowManager(1301): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 15:41:18.454: E/WindowManager(1301): at java.lang.reflect.Method.invoke(Method.java:507)
03-26 15:41:18.454: E/WindowManager(1301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-26 15:41:18.454: E/WindowManager(1301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-26 15:41:18.454: E/WindowManager(1301): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你的线程在onCreate()中启动,你的servise在onStart()中绑定(在onCreate之后),所以mService为null
答案 1 :(得分:0)
这是一种竞争条件,正如尼古拉斯指出的那样。
通过调用bindService(),你没有做任何事情 - 你刚刚要求服务启动。只有当调用onServiceConnected()时,服务才准备就绪。
为您的服务添加侦听器模式。在onServiceConnected()期间触发现有侦听器,一旦发生这种情况,立即触发任何新侦听器。然后不要进行任何服务调用,直到您的使用类收到该回调以表明该服务已准备就绪。
答案 2 :(得分:0)
这几乎肯定是竞争条件。你在onCreate(Bundle)
中启动一个线程,并且该线程期望mService
引用某种类型的实例,但是在你的Service
绑定之前不会填充该实例(发生在onStart()
)。
我不确定,但是您的服务可以绑定在onCreate(Bundle)
中(具体来说,在您开始Thread
之前),或者您的主题可以在onStart()
中启动(具体来说,之后)你的Service
被绑定了吗?做其中任何一个都可以提供帮助,但你最好的选择是使用某种监听器模式,它利用回调(就像android应用程序框架那样)。