【问题标题】:Get Activity Context within class which extends Application in android在类中获取活动上下文,该类扩展了android中的应用程序
【发布时间】:2014-05-31 10:00:40
【问题描述】:

亲爱的各位, 我目前正在开发一个需要显示进度对话框的 android 应用程序。为此,我需要一个上下文对象。 我在扩展应用程序的类中执行此任务。但是每次我尝试访问上下文对象时,它都会向我显示此行的WindowManager.BadTokenException

Dialog dialog=ProgressDialog.show(getApplicationContext(), "Status", "Downloading The master");

请帮助我哪里做错了

编辑: 下面是我的代码!

public class FlightStatus extends Application {
    private Context context;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();



        SharedPreferences preference=getSharedPreferences("FlightStatus", 1);
        if(preference.getBoolean("firstLaunch", true))
            {

                try {
                    Dialog dialog=ProgressDialog.show(getApplicationContext(), "Status!", "Downloading The master!!");
                    XLSReadHelper excelReader=new XLSReadHelper(getApplicationContext());
                    //excelReader.readExcelFile(Environment.getExternalStorageDirectory()+"/UHCPAudit/cases.xls");  
                    excelReader.readExcelFile();
                    Log.d("Excel Operation ", "records read!!");
                    preference.edit().putBoolean("firstLaunch", false).commit();
                    dialog.dismiss();
                } catch (Exception e) {
                    // TODO: handle exception
                    Log.d("Excel Operation ", e.getMessage());
                }

            }
    }    
}

【问题讨论】:

    标签: android android-layout android-fragments android-view android-context


    【解决方案1】:

    尝试在Application 中获取Context 是不正确的,因为在这个阶段还没有初始化Activity。而是显示来自Activity/Fragment 的对话框。

    如果您要显示来自Activity 的进度对话框,请使用:

    Dialog dialog = ProgressDialog.show(MyActivity.this, "Status", "Downloading The master");
    

    如果您从 Fragment 显示它,请先将此成员添加到您的 Fragment 类中:

    private Activity activity;
    

    然后在你的 onCreateView() 方法中调用它:

    activity = getActivity();
    

    最后

    Dialog dialog = ProgressDialog.show(activity, "Status", "Downloading The master");
    

    【讨论】:

    • 嗨 cerebro,我正在从扩展 Application 的类访问!
    • 当你从 Activity 开始这个类时,那时你也可以传递上下文,你可以轻松地使用上下文..
    • @nsm: 你是怎么调用这个方法的??
    • 我在 android manifest 文件中提到了应用程序标签,应用程序类的名称和 oncreate 中我写的这一行
    • @nsm 请在清单中添加您的代码我认为您在编码中做错了什么
    【解决方案2】:

    您可以使用将 Activity 作为上下文的构造函数

    public class YourClass extends Application{
        private Activity context;
    
        public YourClass(Activity context){
           this.context = context;
        }
    }
    

    然后来自您的电话Activity

    YourClass yourClass = new YourClass(this);
    

    您也可以尝试在您的Activityitself 中使用ProgressDialog,在您可以从您的自定义类调用的方法中:

    public void showProgress(){
      //Your progress code
    }
    

    在您的自定义类中:

    context.showProgress();
    

    【讨论】:

    • @nsm 我已经编辑了另一个建议尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 2012-10-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多