Java 我应该将哪个上下文传递给超类?

Java 我应该将哪个上下文传递给超类?,java,android,inheritance,Java,Android,Inheritance,将上下文传递给我的班级是个大问题。我尝试了几乎所有的方法:getApplicationContext/getBaseContext/getContext->在自动完成/this中不可用。在这个时刻,这几乎是工作,我的意思是它不是空的 有关详细信息,请参见孩子: public class PersonBlueActivity extends PersonActivity { private ListView lv_Person; @Override protected void onCreat

将上下文传递给我的班级是个大问题。我尝试了几乎所有的方法:getApplicationContext/getBaseContext/getContext->在自动完成/this中不可用。在这个时刻,这几乎是工作,我的意思是它不是空的

有关详细信息,请参见孩子:

public class PersonBlueActivity extends PersonActivity {

private ListView lv_Person;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_person_chooser_blue);

    lv_Person = (ListView) findViewById(R.id.activity_person_chooser_blue_lv);
    lv_Person.setAdapter(getListAdapter());
    lv_Person.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    lv_Person.setClickable(true);
}
@Override
    public PersonAdapter getListAdapter() {
        return super.getListAdapter();
    }
这里是异步

public class PersonGetAsync extends AsyncTask<String, Void, String> {

    ProgressDialog dialog;
    Activity mActivity;
    Context mContext;
    String response;
    private OnTaskCompletedInterface listener;

    public PersonGetAsync(Activity activity, Context context, OnTaskCompletedInterface listener) {
        this.listener = listener;
        mActivity = activity;
        mContext = context;
    }

@Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(mContext);
        String str = "Connexion en cours";
        dialog.setTitle(str + "...");
        dialog.setIndeterminate(false);
        dialog.setCancelable(true);
        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        System.setProperty("http.keepAlive", "false");
        response = "";
        try {
            //Appel du webservice
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet request = new HttpGet(Globale.webURL+Globale.PERSON);

            // Envoi de la requête GET
            HttpResponse httpResponse = httpClient.execute(request);
            response = EntityUtils.toString(httpResponse.getEntity());

            Log.v("Get : ", response);

        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        listener.onTaskCompleted(result);
        dialog.dismiss();
    }
但是,因为我的上下文没有通过,所以我无法完成此步骤:


它给我带来了一个空点异常为什么

要做到这一点,上下文应该作为活动提供,因为在这里您试图提供对活动的引用,因此您的asynctask类无法识别该活动,因此您需要提供对调用asynctask的活动的引用,如下所示:

 ProgressDialog dialog;
    Activity mActivity;
    Context mContext;
    String response;
    private OnTaskCompletedInterface listener;

    public PersonGetAsync(Activity activity, Context context, OnTaskCompletedInterface listener) {
        this.listener = listener;

        mContext = activity;
    }

@Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(mContext);
        String str = "Connexion en cours";
        dialog.setTitle(str + "...");
        dialog.setIndeterminate(false);
        dialog.setCancelable(true);
        dialog.show();
    }

我还看到,现在您还没有在活动中实现onCreate方法。您可以在onCreate中调用async,而不是在活动子类上实现构造函数

在onCreate方法内部之前,通常在调用super.onCreate之前,不要尝试对其使用Activity call方法或将其作为参数传递给其他对象


导致崩溃的原因是,在尝试将活动与PersonGetAsync类一起使用时,它尚未初始化。将PersonGetAsync的创建和使用延迟到PersonBlueActivity的onCreate方法中的super.onCreate调用之后。

永远不要使用活动的实际构造函数

覆盖“活动生命周期”功能并在那里执行您的工作

当您尝试在“进度”对话框中使用上下文时,上下文为空的原因是它尚未设置。在活动的生命周期函数中设置活动的上下文,当构造函数首先被调用时,这还没有发生


将所有构造函数代码移动到onCreate,一切都会好起来。onCreate函数也会像构造函数一样被调用一次。

我看不出注释活动会如何解决问题。我测试过了,它不起作用。上下文可能有问题,而不是活动。我将活动传递给mContext,获取java.lang.NullPointerException,正如我在回答中提到的,删除构造函数并在onCreate方法中调用异步任务。您不应该在活动构造函数中初始化它。你可能会得到一个NPE,因为活动还没有建立。用onCreate立即完成。谢谢你,DeeV,它成功了!谢谢,成功了!但是Commonware是第一个回答的,对不起
 ProgressDialog dialog;
    Activity mActivity;
    Context mContext;
    String response;
    private OnTaskCompletedInterface listener;

    public PersonGetAsync(Activity activity, Context context, OnTaskCompletedInterface listener) {
        this.listener = listener;

        mContext = activity;
    }

@Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(mContext);
        String str = "Connexion en cours";
        dialog.setTitle(str + "...");
        dialog.setIndeterminate(false);
        dialog.setCancelable(true);
        dialog.show();
    }