我有一个从在线数据库读取表的活动。我正试图将此信息读到ListView
中。我已经在Activity
中声明了一个静态上下文,并将其绑定到Activity
,但是当我检查Context
时,它是Service
类型的实例,而不是Activity
,因此当我运行Activity
时,它会导致NullPointerException
。
public class BuildingListActivity extends Activity {
private static Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_buildings);
BuildingListActivity.mContext = getApplicationContext();
......
}
public static Context getContext() {
return BuildingListActivity.mContext;
}
class LoadAllBuildings extends AsyncTask<String, String, String>{
@Override
protected void onPostExecute(String file_url){
//Dismiss the dialog after getting all buildings
pDialog.dismiss();
// Updating UI from Background Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
/**
* Updating parsed JSON data into ListView
*/
BuildingListActivity.getContext();
L1 = (ListView)findViewById(R.id.list1);
if (BuildingListActivity.mContext instanceof Activity) {
System.out.println("Context: Activity");
} else if (BuildingListActivity.mContext instanceof Service) {
System.out.println("Context: Service");
}else{
System.out.println("Context: Service");
}
myadp = new myAdapter(BuildingListActivity.mContext,buildingNameArray);
L1.setAdapter(myadp); //This is the line that is throwing the NullPointerException
}
});
}
class myAdapter extends ArrayAdapter<String> {
TextView label;
View row;
public myAdapter(Context context, ArrayList<String> buildingNameArray) {
super(context, android.R.layout.simple_list_item_1, buildingNameArray);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ...
}
}
-15 21:02:06.306: E/AndroidRuntime(2002): FATAL EXCEPTION: main
08-15 21:02:06.306: E/AndroidRuntime(2002): java.lang.NullPointerException
08-15 21:02:06.306: E/AndroidRuntime(2002): at com.gerquinn.heritagevancouver.BuildingListActivity$LoadAllBuildings$1.run(BuildingListActivity.java:250)
08-15 21:02:06.306: E/AndroidRuntime(2002): at android.app.Activity.runOnUiThread(Activity.java:4644)
08-15 21:02:06.306: E/AndroidRuntime(2002): at com.gerquinn.heritagevancouver.BuildingListActivity$LoadAllBuildings.onPostExecute(BuildingListActivity.java:233)
08-15 21:02:06.306: E/AndroidRuntime(2002): at com.gerquinn.heritagevancouver.BuildingListActivity$LoadAllBuildings.onPostExecute(BuildingListActivity.java:1)
08-15 21:02:06.306: E/AndroidRuntime(2002): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-15 21:02:06.306: E/AndroidRuntime(2002): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-15 21:02:06.306: E/AndroidRuntime(2002): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-15 21:02:06.306: E/AndroidRuntime(2002): at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 21:02:06.306: E/AndroidRuntime(2002): at android.os.Looper.loop(Looper.java:137)
08-15 21:02:06.306: E/AndroidRuntime(2002): at android.app.ActivityThread.main(ActivityThread.java:5039)
08-15 21:02:06.306: E/AndroidRuntime(2002): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 21:02:06.306: E/AndroidRuntime(2002): at java.lang.reflect.Method.invoke(Method.java:511)
08-15 21:02:06.306: E/AndroidRuntime(2002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-15 21:02:06.306: E/AndroidRuntime(2002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-15 21:02:06.306: E/AndroidRuntime(2002): at dalvik.system.NativeStart.main(Native Method)
注意Activity
扩展了Context
。这里不需要使用静态的Context
,因为您可以随时使用this
(或者在内部类中使用BuildingListActivity.this
)。
做以下修改:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_buildings);
BuildingListActivity.mContext = this;
......
}
这将解决创建活动的上下文实例