3

在游戏应用程序中,我有以下场景:

  • 从主游戏Activity开始,玩家开始了几个在后台运行且持续时间不同的游戏任务。
  • 玩家应该能够在单独的View.

为此,我创建了两个Activitys 和 a Service,定义如下:

  • Service ProgressService处理ProgressBar在并行线程上同时运行的多个 s。

  • Activity WorkScreen2创建一个游戏任务,从传入的任务参数Service开始。startService()Bundle

  • Activity ProgressScreen绑定到Service以获取并显示ProgressBar正在运行的任务的 s。

  • 这两个活动都在一个单独TabHost的 s 下运行TabActivity


我遇到的问题是该ServiceConnection.onServiceConnected()方法从未被调用。我得到 a 是Java.lang.NullPointerException因为我尝试调用Service应该在此方法中分配的对象的方法。请参阅下面的代码。

我使用getApplicationContext().bindService()绑定Activity到,Service因为TabSpec无法绑定到Services。此方法返回true. 因此,绑定成功。


这是Service

public class ProgressService extends Service implements GameConstants {
    public static final String BROADCAST_PROGRESS = "com.mycompany.android.mygame.progressbroadcast";
    private static final long UPDATE_INTERVAL = 500;
    private IBinder mBinder;
    private List<ProgressBar> mProgressBarList;
    private List<String> mStaffNameList;

    private final class ServiceHandler extends Handler {

        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            ProgressBar progressBar = new ProgressBar(ProgressService.this);
            mProgressBarList.add(progressBar);

            Bundle bundle = msg.getData();
            String staffName = bundle.getString(WorkScreen2.STAFF_NAME);
            mStaffNameList.add(staffName);

            int taskDurationMillis = bundle.getInt(WorkScreen2.TASK_DURATION) * 1000;
            progressBar.setMax(taskDurationMillis / 1000);

            long startTimeMillis = SystemClock.uptimeMillis();
            long elapsedTimeMillis = SystemClock.uptimeMillis()
                    - startTimeMillis;

            Intent intent = new Intent();
            intent.setAction(BROADCAST_PROGRESS);

            while (elapsedTimeMillis < taskDurationMillis) {
                try {
                    Thread.sleep(UPDATE_INTERVAL);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                elapsedTimeMillis = SystemClock.uptimeMillis()
                        - startTimeMillis;
                int elapsedTimeSeconds = (int) elapsedTimeMillis / 1000;
                progressBar.setProgress(elapsedTimeSeconds);
                sendBroadcast(intent);
            }

            progressBar.setVisibility(View.GONE);
            mProgressBarList.remove(progressBar);
            mStaffNameList.remove(staffName);
            sendBroadcast(intent);

            if (mProgressBarList.isEmpty()) {
                stopSelf(msg.arg1);
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mBinder = new ProgressServiceBinder();
        mProgressBarList = Collections
                .synchronizedList(new ArrayList<ProgressBar>());
        mStaffNameList = Collections.synchronizedList(new ArrayList<String>());
    }

    /*
     * Creates a thread for each game task with parameters passed in
     * <code>intent</code>
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "starting service", Toast.LENGTH_LONG).show();

        HandlerThread thread = new HandlerThread("ServiceStartArguments",
                Process.THREAD_PRIORITY_BACKGROUND);
        thread.start();

        Handler serviceHandler = new ServiceHandler(thread.getLooper());

        Message msg = serviceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.setData(intent.getExtras());
        serviceHandler.sendMessage(msg);

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public class ProgressServiceBinder extends Binder {
        ProgressService getService() {
            return ProgressService.this;
        }
    }

    public List<ProgressBar> getProgressBarList() {
        return mProgressBarList;
    }

    public List<String> getStaffNameList() {
        return mStaffNameList;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service done", Toast.LENGTH_SHORT).show();
    }
}

这是Activity绑定到它的:

public class ProgressScreen extends ListActivity {
    private final String TAG = "ProgressScreen";

    private ProgressScreenAdapter mAdapter;
    private ProgressService mProgressService;
    private List<ProgressBar> mProgressBarList;
    private List<String> mStaffNameList;

    @Override
    public void onCreate(Bundle bundle) {
        Log.i(TAG, "ProgressScreen oncreate");

        super.onCreate(bundle);
        setContentView(R.layout.progress_screen_layout);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ProgressService.BROADCAST_PROGRESS);
        registerReceiver(receiver, filter);
        doBindService();

        mAdapter = new ProgressScreenAdapter(this, mStaffNameList, mProgressBarList);
        setListAdapter(mAdapter); // Returns true

        /*
         * This is where I get the NullPointerException
         * mProgressService is null here
         */
        mProgressBarList = mProgressService.getProgressBarList();
        mStaffNameList = mProgressService.getStaffNameList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ProgressService.BROADCAST_PROGRESS);
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    boolean doBindService() {
        return getApplicationContext().bindService(new Intent(this, ProgressService.class), mConnection, Context.BIND_AUTO_CREATE);
    }

    void doUnbindService() {
        getApplicationContext().unbindService(mConnection);
    }

    ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder binder) {
            mProgressService = ((ProgressService.ProgressServiceBinder) binder).getService();
            Toast.makeText(ProgressScreen.this, "Connected to ProgressService", Toast.LENGTH_SHORT).show();
        }

        public void onServiceDisconnected(ComponentName name) {
            mProgressService = null;
        }
    };

    private BroadcastReceiver receiver = new BroadcastReceiver () {
        @Override
        public void onReceive(Context context, Intent intent) {
            mAdapter.notifyDataSetChanged();
        }
    };
}

Servicemain开始Activity如下:

Intent intent = new Intent(WorkScreen2.this, ProgressService.class);
intent.putExtra(TASK_DURATION, task.getDuration());
intent.putExtra(STAFF_NAME, staff.getName());
startService(intent);

AndroidManifest.xml包含_

<service
    android:name=".ProgressService"
    android:label="@string/progress_service">
</service>
4

3 回答 3

15

ServiceConnection 的 onServiceConnected()被调用,但没有人保证它会在onCreate 继续执行之前被调用。那么,这里发生了什么 - 你成功绑定到服务(这就是 onBind 返回 true 的原因),但你没有完全连接- onServiceConnected() 还没有被调用,所以你的本地 mProgressService 对象还没有初始化,因此你获取 NullPointerException。

解决方案:

移动这两行:

mProgressBarList = mProgressService.getProgressBarList();
mStaffNameList = mProgressService.getStaffNameList();

从 onCreate() 到 onServiceConnected() 函数(在 onServiceConnected() 中初始化后使用服务对象)。

于 2012-10-13T20:40:37.413 回答
9

检查您的 AndroidManifest.xml 并添加您尝试绑定的服务。

于 2012-02-12T08:20:01.483 回答
2

您必须从返回您的 Binder 内部类

private final IBinder mBinder = new ServiceBinder();

 public class ServiceBinder extends Binder {
   public PlayerActivity getService() {
   return PlayerActivity.this;
     }
  }

@Nullable
@Override
           public IBinder onBind(Intent intent) {
             return mBinder;
           }
于 2015-11-24T11:00:59.670 回答