分享"全球及QUOT;使用网页视图时,整个活动中,问题变量...? [英] sharing "Global" variables across activities, problem when using a Webview...?
问题描述
我分享一些变量进行的跨活动使用的一类是这样的:
I'm sharing some variables accross activities by using a class like this :
public class Globals {
static Boolean hint1_graph_type_switcher;
static Boolean hint2_stockview_valuation;
other variables ...
}
然后我用这些变量在我的多个activites任何地方...
then I'm using these variables anywhere across my multiple activites with ...
if (Globals.hint2_stockview_valuation == false) {
....
}
pretty基础,这是工作的罚款,直到...
pretty basic and it was working fine untill ...
我介绍了一些网页流量的东西是这样的:
I introduced some webview stuff like this:
//-----------------------------------------------------------
// open a webview with the NEWS when the more_arrow is clicked :
mNews.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String news_URL = "http://us.m.yahoo.com/w/yfinance/symbolheadlines/"+ ticker + "/?.intl=us&.lang=en";
Intent news_Webview_intent = new Intent(Chart_View.this, News_Webview.class);
news_Webview_intent.putExtra("NEWS_URL", news_URL);
startActivity(news_Webview_intent);
}
});
//-----------------------------------------------------------
和这里的News_Webview.class:
and here's the News_Webview.class:
public class News_Webview extends Activity {
//
// http://www.chrisdanielson.com/tag/progressdialog/
//
String news_URL;
private WebView webview;
private ProgressDialog progressBar;
private static final String TAG = "Hub";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webview_news);
this.webview = (WebView)findViewById(R.id.webView);
Bundle extras = getIntent().getExtras();
if (this.getIntent().getExtras()!=null){
news_URL = extras.getString("NEWS_URL");
}
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(News_Webview.this, "", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//Log.i(TAG, "Processing webview url click...");
Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(url));
startActivity(viewIntent);
return true;
}
public void onPageFinished(WebView view, String url) {
//Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(News_Webview.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl(news_URL);
}
}
现在的问题是,当它从这个活动回来,它看起来像我的全局变量都消失了?
the problem now is that, when it "comes back" from this Activity, it looks like my Globals variables have disappeared ???
if (Globals.hint2_stockview_valuation == false) {
触发一个错误:
12月6日至23日:14:03.443:
ERROR / AndroidRuntime(2611):产生的原因:
显示java.lang.NullPointerException
06-23 12:14:03.443: ERROR/AndroidRuntime(2611): Caused by: java.lang.NullPointerException
2的问题,然后:
-
我应该使用比这个全局类别的跨活动共享变量?难道仅仅是不好的做法,以做到这一点?我知道我可以使用preferences,但我认为这是更快地做这种方式(不需要读取preferences每次我开始一个新的活动...
Should I use something else than this "Global" class to share variables across activities ? Is it just bad practice to do this ?? I know that I can use the preferences but I thought it was quicker to do it this way (no need to "read" the preferences everytime I start a new activity ...
这是为什么发生这种情况的任何想法?在某些方面,我应该找回我savedInstanceState当我的活动从News_Webview.class ???
Any idea on WHY this is happening ? Should I "get back" my savedInstanceState in some way when my activity returns from the News_Webview.class ???
和往常一样,感谢你的帮助。
As always, thank you for your help.
小时。
推荐答案
ü可以使用 intent.putExtra()
的活动间的相互作用...
U could use intent.putExtra()
for inter-activity interaction...
有一件事情(只用于诊断),我建议正在初始化静态变量为某个值,然后运行该应用程序...我想你的全局类时的意图开始越来越活动重新初始化返回回来...
One thing(just for diagnostics) i would suggest is initializing the static variables to some value and then run the App... i suppose your global class is getting re-initialized when the intent started activity returns back...
我已经使用全局变量,但在我的情况下,我让他们在其中从来没有死的活动。之后所有其他活动来了,它的工作完美的罚款......
I have used global variables, but in my case i kept them in an activity which never died. All other activities came after it and it worked perfectly fine...
这篇关于分享"全球及QUOT;使用网页视图时,整个活动中,问题变量...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!