上下文初始化为Service而不是Activity

时间:2014-08-16 02:09:27

标签: java nullpointerexception android-context

我有一个从在线数据库中读取表的活动。我正在尝试将此信息读入ListView。我已在Activity中声明了一个静态上下文并将其绑定到Activity,但当我检查ContextService类型的实例而不是Activity时因此,当我运行NullPointerException时,它会导致Activity

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)

2 个答案:

答案 0 :(得分:0)

请注意,Activity扩展了Context。这里不需要使用静态Context,因为您可以随时使用this(或者在内部类中时使用BuildingListActivity.this。)

答案 1 :(得分:0)

进行以下更改:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_buildings);
        BuildingListActivity.mContext = this;

        ......
    }

这将解决制作活动的上下文实例