0

我基于这个demo构建了一个功能齐全的FragmentActivity

最初(就像我的应用程序中的其他活动一样)数据正在从 UI 线程中的 SQLite加载到FragmentActivity.ListFragment.ListView中。

我已经成功地将其他两个活动(不是片段或列表活动)转换为使用IntentService从 SQLite 获取数据的ResultReceiver范例。此实例的明显区别在于,自定义ResultReceiver是在ListFragment中声明的,而不是在FragmentActivity中声明的:

public EWTRReceiver ArrayListFragmentReceiver;

问题: IntentService获取请求,提取数据但无法将数据返回到ListFragment:IntentService.rspPlayers() 中此行上的 NullPointerException:

r.send(mSignal, resultData);

我确信我错过了一些简单的东西,这会让我觉得自己像个白痴……我只需要被推向正确的方向。

以下是 (1) 从ListFragmentIntenService的调用,(2) IntentService 的onHandleIntent和 (3) IntentService中回调ListFragment的方法(rspPlayers())。

谢谢!

FragmentActivity.ListFragment.callCtlr():

    private void callCtlr(int mSignal){
        if (BuildConfig.DEBUG) {
            Log.i(Constants.TAG_FRGACTPLAYERS, "ArrayListFragment.callCtlr() called with signal: "+mSignal);
        }
        /** Intent to transmit between processes */
        Intent intent = new Intent(getActivity(), CtlPlayers.class);
        // declare Source of Signal
        intent.putExtra("Source", ArrayListFragmentReceiver);
        /** process signal to send to CtlPlayers */
        switch (mSignal){
           case Constants.S_DB_READPLYRLDR:
               if (BuildConfig.DEBUG) {
                   Log.i(Constants.TAG_FRGACTPLAYERS, "ArrayListFragment.callCtlr(S_DB_READPLYRLDR) to CtlPlayers.");
               }
               // Send CtlAlliances the request for the list of Alliances in current Sector
               intent.putExtra("CurrentSector", this.getArguments().getString("FSECTR"));
               intent.putExtra("CurrentAlliance", this.getArguments().getString("FANAME"));
               intent.putExtra("signal", Constants.S_DB_READPLYRLDR);
               break;
          case Constants.S_DB_READPLYRREC:
               if (BuildConfig.DEBUG) {
                   Log.i(Constants.TAG_FRGACTPLAYERS, "ArrayListFragment.callCtlr(S_DB_READPLYRREC) to CtlPlayers.");
               }
               // Send CtlAlliances the request for the list of Alliances in current Sector
               intent.putExtra("CurrentSector", this.getArguments().getString("FSECTR"));
               intent.putExtra("CurrentAlliance", this.getArguments().getString("FANAME"));
               intent.putExtra("signal", Constants.S_DB_READPLYRREC);
               break;
            default:
                //do nothing
                break;
        }
        getActivity().startService(intent);
    }

IntentService.onHandleIntent():

    protected void onHandleIntent(Intent intentREQ) {
    if (BuildConfig.DEBUG) {
        Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(Intent intent) called.");
    }
    /** get the receiver and bundle from the incoming Intent */
    ResultReceiver rInComing = intentREQ.getParcelableExtra("Source");

    /** get the bundle from the incoming Intent */
    extras = intentREQ.getExtras();
    /** variable to convey results to ActDlgAddPlyr */
    boolean mAnswer = false;
    /** tracks ActSector's context */
    Context mCtx    = getApplicationContext();
    int     mSignal = intentREQ.getIntExtra("signal", Constants.R_NO_SIGNAL);
    if (BuildConfig.DEBUG) {
        Log.i(Constants.TAG_CTLPLAYERS, "Signal Received = " + mSignal);
    }

    switch (mSignal) {
    case Constants.R_NO_SIGNAL :
        if (BuildConfig.DEBUG) {
            Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): NO READABLE SIGNAL WAS RECEIVED!");
        }
        break;
    case Constants.S_DB_ADDPLYR :
        if (BuildConfig.DEBUG) {
            Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_ADDPLYR received.");
        }
        synchronized (this) {
            // Get the New Sector Name from the bundle
            if (BuildConfig.DEBUG) {
                Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_ADDPLYR received with New Player.");
            }
            mAnswer = addNewPlayer(mCtx, extras);
            rspPlayers(Constants.R_DB_ADDPLYR, mAnswer, rInComing);
        }
        break;
    case Constants.S_DB_READPLYRLDR :
        if (BuildConfig.DEBUG) {
            Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_READPLYRLDR received.");
        }
        synchronized (this) {
            // Get the New Sector Name from the bundle
            if (BuildConfig.DEBUG) {
                Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_READPLYRLDR received.");
            }
            mAnswer = readPlyrLdr(mCtx, extras);
            rspPlayers(Constants.R_DB_READPLYRLDR, mAnswer, rInComing);
        }
        break;
    case Constants.S_DB_READPLYRREC :
        if (BuildConfig.DEBUG) {
            Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_READPLYRREC received.");
        }
        synchronized (this) {
            // Get the New Sector Name from the bundle
            if (BuildConfig.DEBUG) {
                Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_READPLYRREC received.");
            }
            mAnswer = readPlyrRec(mCtx, extras);
            rspPlayers(Constants.R_DB_READPLYRREC, mAnswer, rInComing);
        }
        break;
    } // end switch
}

IntentService.rspPlayers() [将结果发送回 FragmentActivity.ListFragment]:

private void rspPlayers(int mSignal, boolean mCode, ResultReceiver r) {
    if (BuildConfig.DEBUG) {
        Log.i(Constants.TAG_CTLPLAYERS, "respPlayers() called with signal, code: "+mSignal+", "+mCode);
    }

    Bundle resultData = new Bundle();
    switch (mSignal) {
    case Constants.R_DB_ADDPLYR :
        if (mCode == true) {
            resultData.putInt("reqStatus", Constants.R_TRUE_THAT);
        } else {
            resultData.putInt("reqStatus", Constants.R_NO_WAY);
        }
        break;
    case Constants.R_DB_READPLYRLDR :
        if (mCode == true) {
            resultData.putInt("reqStatus", Constants.R_TRUE_THAT);
            resultData.putInt("NumLdrRows", NumLdrRows);
            resultData.putSerializable("LPlyrList", LPlyrArray);
        } else {
            resultData.putInt("reqStatus",  Constants.R_NO_WAY);
        }
        break;
    case Constants.R_DB_READPLYRREC :
        if (mCode == true) {
            resultData.putInt("reqStatus", Constants.R_TRUE_THAT);
            resultData.putInt("NumRecRows", NumRecRows);
            resultData.putSerializable("RPlyrList", RPlyrArray);
        } else {
            resultData.putInt("reqStatus",  Constants.R_NO_WAY);
        }
        break;
    default :
        // do nothing
        break;
    }

    try {
        r.send(mSignal, resultData);
    } catch (java.lang.Exception e) {
        Log.e(Constants.TAG_CTLPLAYERS, "rspPlayers(): Exception message: "+e.getMessage());
        Log.e(Constants.TAG_CTLPLAYERS, "rspPlayers(): Exception cause: "+e.getCause());
        e.printStackTrace();
    }
}
4

1 回答 1

0

我认为ArrayListFragmentReceiver是空的。

Handler handler = new Handler();
public EWTRReceiver ArrayListFragmentReceiver = new ResultReceiver(handler)

需要处理程序来确保响应在原始线程中返回。否则这将是下一次崩溃。

查看本教程以获取示例实现:

http://ramdroid.github.io/androiddev/ResultReceiverDemo.html

于 2013-05-22T07:41:36.337 回答