如何在android中打开活动并传递上下文对象

如何在android中打开活动并传递上下文对象,android,Android,我面临着某种问题,我不知道为什么。当我通过单击按钮调用函数“mapFound”时,发生了一些不好的事情。但我觉得看起来不错 package com.fva_001.flashvsarrow.com.fva_001; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MapFound extends AppCompat

我面临着某种问题,我不知道为什么。当我通过单击按钮调用函数“mapFound”时,发生了一些不好的事情。但我觉得看起来不错

package com.fva_001.flashvsarrow.com.fva_001;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MapFound extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.find_map);
}
// when I click this button, error happens
public void mapFound(View view){
    MapFoundDialog dialog = new MapFoundDialog(getApplicationContext());
    dialog.show();
}
}
这是我的MapFoundDialog课程

package com.fva_001.flashvsarrow.com.fva_001;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.*;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class MapFoundDialog extends Dialog {

public Context c;
public Button yes, no;

public MapFoundDialog(Context a) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    setContentView(R.layout.dialog_map_found);

// Here I want to open a new Activity, I think it has some problem too
    no = (Button) findViewById(R.id.btn_map_open);
    no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new ButtonClick(getContext(), v);
            Intent entireMapIntent = new Intent(c, EntireMap.class);
            c.startActivity(entireMapIntent);
        }
    });
}
}

请通过提供活动上下文而不是应用程序上下文进行检查。参见下面的代码

package com.fva_001.flashvsarrow.com.fva_001;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MapFound extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.find_map);
    }
    // when I click this button, error happens
    public void mapFound(View view){
      MapFoundDialog dialog = new MapFoundDialog(MapFound.this);
      dialog.show();
    }
}

上下文似乎有问题

public MapFoundDialog(Context a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
你可以试试这个

 public void mapFound(View view){
  MapFoundDialog dialog = new MapFoundDialog(MapFound.this);
  dialog.show();
}

发生了什么不好的情况?您可以从下一个活动访问上下文,那么为什么需要传递上下文呢?请对堆栈进行跟踪以使其更清楚。但是传递整个活动是一个严重的问题,而且有时活动会加载,有时则不会……谢谢您的回答。但每次我打开活动时,它都不起作用,但后来,是的……没关系……在onResume()中调用该方法,而不是表示感谢的方式,也不是向上投票或接受答案(如果有帮助的话)。