- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
以下代码不断抛出空指针异常...
public class MainActivity extends Activity implements OnClickListener{
Button but;
Button but2;
LocalBinder binder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but = (Button) findViewById(R.id.button1);
but.setOnClickListener(this);
but = (Button) findViewById(R.id.button2);
}
ServiceConnection connection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
Log.d("D1", "connecting");
binder = (LocalBinder) arg1;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
Log.d("D1", "disconnecting");
}
};
public static class ServiceTest extends Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return new LocalBinder();
}
public class LocalBinder extends Binder{
public void toast(){
Toast.makeText(getApplicationContext(), "hope this works", Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), ServiceTest.LocalBinder.class);
bindService(i, connection, Context.BIND_AUTO_CREATE);
Toast.makeText(getApplicationContext(), "bind completed", Toast.LENGTH_LONG).show();
}
public void binderMethod(View v){
binder.toast();
}
}
我猜测原因是onServiceConnected没有执行,导致binder保持为null。 (but按钮将服务绑定(bind)到activity,同时but2执行binder.toast();——按下but2时抛出异常)
基本上问题是:为什么 onServiceConnected 不执行?
最佳答案
我认为,该 Intent 应该导致服务,而不是服务中的绑定(bind)器:Intent i = new Intent(getApplicationContext(), ServiceTest.class);
关于java - android BindService() 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26563586/
我需要一种方法来限制对特定应用程序的服务使用。也就是说,我有一个包含用户可以安装的服务的应用程序和一个配套应用程序。我只希望配套应用能够使用该服务。我使用 bindIntent 调用该服务。 我一直在
我有一个使用远程服务的 Android 应用程序,我使用异步的 bindService() 绑定(bind)到它。 在绑定(bind)服务之前应用程序是无用的,所以我想简单地等到绑定(bind)完成后
在onCreate()我调用主要 Activity 的bindService()与 BIND_AUTO_CREATE标记,然后调用 unbindService()在 onDestroy() .然后我启
这是关于基于内存的 IPC(如 LocalService 示例)但对于在同一进程中运行的两个应用程序: 我有两个应用程序(App1、App2)和一个共享项目(Shared),它为两个应用程序定义了一些
我有一个在 API 14 中工作的项目。现在我正在迁移到 API 21,所以我正在做我需要的更改。 这是一个使用位置来跟踪路线的应用程序。我有一项服务可以处理位置信息。但是当我尝试绑定(bind)到该
以下代码不断抛出空指针异常... public class MainActivity extends Activity implements OnClickListener{ Button but;
我在将服务绑定(bind)到 Activity 时遇到问题。我得到 playing_service==null。我找不到我做错了什么。为什么 playing_service 为空?? MyActivi
我想知道 Context.bindService() 什么时候返回 false? 我试图让 onBind() 返回 null,但当 bindService 时它仍然返回 true > 被调用并且 on
作为 Android 开发的新手,我正在尝试做基础知识并构建一个媒体播放器。它在一项 Activity 中运行良好,但我现在将 MediaPlayer 内容移动到远程服务,以便它可以在 Activit
我正在尝试构建并运行一个用于 react native 应用程序的服务,在该服务运行后,我需要调用一些服务方法,这意味着我需要获取正在运行的服务的实例,所以我正在尝试使用bindservice()。
我正在尝试为 Android 应用程序创建远程服务。我或多或少地从 Android Developers 网站复制了代码,并根据我的需要对其进行了重组。但是,当我进入 bindService 步骤时,
我发现的许多类似问题都没有帮助解决我的问题。 以下是我看过的一些问题: ServiceConnection.onServiceConnected() never called after bindin
我有一个服务单例类,其中包含从同一进程调用的静态方法。我在应用程序启动时调用 startService。我也调用了 bindService,但现在我想知道这是否真的有必要。 最佳答案 这取决于您是否需
我正在实现基于 Dungeons 示例的计费服务(Google 建议您采用这种方式)。以下事实使它稍微复杂一些: 主要服务类位于库项目中(因为我想多次重新设计代码)。作为this stackoverf
我正在尝试开发一个连接 android 和 RedBear BLE shield 的应用程序。 第一次一切正常。但是当我第二次尝试连接到设备时,它不起作用。 OnServiceConnected()
我在 onCreate 中有一个 bindservice 需要很长时间,我试图通过将它移动到工作线程来解决这个问题,但它仍然导致 UI 等待。我能做什么 public void onCreate(Bu
我有一个与此相关的问题 question @mnish 大约一年前问过这个问题。 请看一下他的问题和代码。他实现了一个 ServiceConnection() 并将其传递给 bindService()
从所有代码示例来看,如果我们调用了#bindService,我们只想调用#unbindService(在同一上下文中),这是通过 boolean 检查完成的。 但是在#bindService 调用中没
据我了解,如果我希望服务在不受任何限制的情况下运行,则必须首先使用 startService(Intent i) 启动它。 我的问题是,如果我想在启动后立即绑定(bind)到服务,下面的代码是否可以保
我正在尝试使用 AIDL 开发 2 个应用程序(服务应用程序 + 客户端应用程序)的设置。我目前有 3 个模块的设置: android-agent-framework(仅包含 AIDL 文件的 and
我是一名优秀的程序员,十分优秀!