[英]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 ...
}
then I'm using these variables anywhere across my multiple activites with ... 然后我在我的多个活动中的任何地方使用这些变量...
if (Globals.hint2_stockview_valuation == false) {
....
}
pretty basic and it was working fine untill ... 非常基本的,它工作得很好,直到......
I introduced some webview stuff like this: 我介绍了一些像这样的webview:
//-----------------------------------------------------------
// 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);
}
});
//-----------------------------------------------------------
and here's the News_Webview.class: 这是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 ??? 现在的问题是,当它从这个Activity“回来”时,看起来我的Globals变量已经消失了???
if (Globals.hint2_stockview_valuation == false) {
fires an error : 触发错误:
06-23 12:14:03.443: ERROR/AndroidRuntime(2611): Caused by: java.lang.NullPointerException
06-23 12:14:03.443:ERROR / AndroidRuntime(2611):引起:java.lang.NullPointerException
2 questions then : 然后2个问题:
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 ...
我知道我可以使用首选项,但我认为这样做更快(每次开始新活动时都不需要“读取”首选项...
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 ???
当我的活动从News_Webview.class返回时,我应该以某种方式“取回”我的savedInstanceState吗?
As always, thank you for your help. 一如既往,谢谢你的帮助。
H. H。
U could use intent.putExtra()
for inter-activity interaction... 你可以使用
intent.putExtra()
进行交互活动...
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... 有一点(仅用于诊断)我建议将静态变量初始化为某个值,然后运行App ...我想当意图启动活动返回时,您的全局类正在重新初始化...
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...
所有其他活动都在它之后,它完美地运作了......
NullPointerException从android运行时冒出来,与你的Globals类型无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.