这让我很难过,我在Android 2.1-r8 SDK中使用它:
ProgressDialog.show(getApplicationContext(), ....);
Run Code Online (Sandbox Code Playgroud)
还有
Toast t = Toast.makeText(getApplicationContext(),....);
Run Code Online (Sandbox Code Playgroud)
使用getApplicationContext()崩溃ProgressDialog和Toast....导致我这个问题:
尽管分享了"上下文"的措辞,但活动上下文和应用程序上下文之间的实际差异是什么?
我是新来的Android和我想明白之间的差别getApplication(),getApplicationContext()getBaseContext(),getContext()以及someClass.this特别是当使用这些方法在下面的代码行:
当我发起祝酒时,这些和我使用它们之间有什么区别?
Toast.makeText(LoginActivity.this, "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)
与意图相同:
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
Intent intent = new Intent(MenuPagina., LoginActivity.class);
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
Intent intent = new Intent(getApplication(), LoginActivity.class);
Run Code Online (Sandbox Code Playgroud) 我真的很纳闷.我有一个上下文:
Context context= getActivity();
Run Code Online (Sandbox Code Playgroud)
当我context在片段中使用UI时,像webview app这样的东西给了我NullPointerException(Forceclose),但是当我使用getActivity()它时效果很好.有什么区别!?让我解释一下用法.我有一个名为"A"和"B"的活动.活动"B"从活动"B"继承NavigationDrawer和Actionbar.所以有:
public class B extends A
Run Code Online (Sandbox Code Playgroud)
我们在NavigationDrawer中知道有一个主要内容.活动"B"使用片段来提供主要内容,我在该片段中使用Context.我真的很想知道!抱歉英语不好.
编辑:这是我的代码:
public class PlaceholderFragment extends Fragment {
public Context context = getActivity();
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_text, container, false);
String text = "<html><head><link href=\"file:///android_asset/style_css.css\" rel=\"stylesheet\" type=\"text/css\"></head> <body class=\"body\"> title1 <hr> <div align=\"center\"> <img src= "+imagePath1_1+" width= \"95% \" /></div>les1</body></html>";
WebView webView= new WebView(context);
webView.loadDataWithBaseURL(null,text, …Run Code Online (Sandbox Code Playgroud)