sharing “Global” variables across activities, problem when using a Webview…?

空扰寡人 提交于 2019-12-12 09:47:20

问题


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:

//-----------------------------------------------------------
        // 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:

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) {

fires an error :

06-23 12:14:03.443: ERROR/AndroidRuntime(2611): Caused by: java.lang.NullPointerException

2 questions then :

  • 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 ???

As always, thank you for your help.

H.


回答1:


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...




回答2:


The NullPointerException is bubbling up from the android runtime and has nothing to do with your Globals type.



来源:https://stackoverflow.com/questions/3100726/sharing-global-variables-across-activities-problem-when-using-a-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!