也许我找不到答案的原因是我做错了问题,但我仍然希望在这里有人可以回答这个问题。
我有一个带有 ListView 的 MainActivity,它在处理后从数据库中呈现一些值:
public class MainActivity extends Activity {
ListView mainViewList;
CTTObjectsArrayAdapter arrayAdapter;
ArrayList<CTTObject> cttObjects = null;
public UpdateObjectResultReceiver updateObjectReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Update all the Objects in the DB
CTTUtilities.updateAllObjects(context, updateObjectReceiver);
// DB stuff
CTTObjectsDataSource dataSource = new CTTObjectsDataSource(context);
dataSource.open();
// Initialize ListView
mainViewList = (ListView) findViewById(R.id.listMain);
// Initialize our ArrayList
cttObjects = new ArrayList<CTTObject>();
cttObjects = dataSource.getAllObjects();
dataSource.close();
// Initialize our array adapter notice how it references the
// listMain.xml layout
arrayAdapter = new CTTObjectsArrayAdapter(context,
R.layout.main_list_row_layout, cttObjects);
// Set the above adapter as the adapter of choice for our list
mainViewList.setAdapter(arrayAdapter);
}
现在,我需要的是调用以下代码片段,当 updateObjectReceiver 变量从我启动并更新数据库的 Intent 服务接收到答案时更新 ListView:
cttObjects.clear();
CTTObjectsDataSource dataSource = new CTTObjectsDataSource(context);
dataSource.open();
cttObjects.addAll(dataSource.getAllObjects());
dataSource.close();
arrayAdapter.notifyDataSetChanged();
那我该怎么做呢?
编辑:
在实现了 receiver.send 和下面提出的方法之后,应用程序失败了:
07-23 20:30:40.435: W/dalvikvm(3467): threadid=15: thread exiting with uncaught exception (group=0x40c88930)
07-23 20:30:40.435: E/AndroidRuntime(3467): FATAL EXCEPTION: IntentService[UpdateObjectIntentService]
07-23 20:30:40.435: E/AndroidRuntime(3467): java.lang.NullPointerException
07-23 20:30:40.435: E/AndroidRuntime(3467): at info.hecatosoft.ctttrack2.UpdateObjectIntentService.onHandleIntent(UpdateObjectIntentService.java:89)
07-23 20:30:40.435: E/AndroidRuntime(3467): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
07-23 20:30:40.435: E/AndroidRuntime(3467): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 20:30:40.435: E/AndroidRuntime(3467): at android.os.Looper.loop(Looper.java:137)
07-23 20:30:40.435: E/AndroidRuntime(3467): at android.os.HandlerThread.run(HandlerThread.java:60)
编辑2:
它失败了:receiver.send(STATUS_UPDATED_CHANGED, Bundle.EMPTY);
有问题的代码是:
@Override
protected void onHandleIntent(Intent arg0) {
// ---process the received extras to the service call and get the URL
// from it---
this.receiver = arg0.getParcelableExtra(RECEIVER_KEY);
String recTrackNo = arg0.getStringExtra("trackNo");
Log.i("jbssmDeveloper", "received trackNo=" + recTrackNo);
String jsonDataString = null;
jsonDataString = getHTTPJSONData(recTrackNo);
if (jsonDataString != null) {
dataSource = new CTTObjectsDataSource(getBaseContext());
dataSource.open();
CTTObject cttObject = dataSource
.getObjectWithTrackNo(recTrackNo);
dataSource.close();
updateDB(cttObject, jsonDataString);
receiver.send(STATUS_UPDATED_CHANGED, Bundle.EMPTY);
}
receiver.send(STATUS_ERROR, Bundle.EMPTY);
this.stopSelf();
}