Android 在片段中显示带有progressdialog的资源

Android 在片段中显示带有progressdialog的资源,android,android-fragments,Android,Android Fragments,我可以用三种不同的方式显示资源 pDialog.setMessage(getActivity().getApplicationContext().getResources().getString(R.string.please_wait)); pDialog.setMessage(getContext().getResources().getString(R.string.please_wait)); pDialog.setMessage(getResources().getString(R

我可以用三种不同的方式显示资源

pDialog.setMessage(getActivity().getApplicationContext().getResources().getString(R.string.please_wait));

pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));

pDialog.setMessage(getResources().getString(R.string.please_wait));
哪一条是正确的道路?为什么?

没有正确的道路。您只需要
上下文

  • getResources()
    当您在
    Activity
    类中时(或
    MyActivity.this.getResources()
    当您在Activity类中从内部类调用时)
  • getActivity().getResources()
    (甚至
    getContext().getResources()
    )当您在
    Fragment
    类中时
  • context.getResources()
    当您通过参数传递
    context
  • view.getContext().getResources()
    当您从
    视图中获取
    Context

以下两个具有“等效的
上下文
”,因为您处于
片段
中,就像我可以通过您的问号假设的那样:

pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));

pDialog.setMessage(getResources().getString(R.string.please_wait));
在哪里

您可以获得entrie应用程序的
上下文

这里有一些参考:

pDialog.setMessage(getActivity().getApplicationContext().getResources().getString(R.string.please_wait));