android - 跨 Activity 共享 "Global"变量,使用 Webview 时出现问题...?

标签 android global-variables webview

我通过使用这样的类在 Activity 之间共享一些变量:

public class Globals {

static Boolean hint1_graph_type_switcher; 
static Boolean hint2_stockview_valuation;

other variables ...
    }

然后我在我的多个 Activity 中的任何地方使用这些变量......

if (Globals.hint2_stockview_valuation == false) {

        ....

    }

非常基本,它工作正常直到......

我介绍了一些像这样的 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);
            }
        });
        //-----------------------------------------------------------

这是 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);
    }
}

现在的问题是,当它从这个 Activity 中“返回”时,我的 Globals 变量似乎已经消失了???

if (Globals.hint2_stockview_valuation == false) {

引发错误:

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

然后是 2 个问题:

  • 我是否应该使用这个“全局”类以外的其他类来跨 Activity 共享变量?这样做只是不好的做法吗?我知道我可以使用首选项,但我认为这样做更快(每次开始新 Activity 时无需“阅读”首选项...

  • 知道为什么会这样吗?当我的 Activity 从 News_Webview.class 返回时,我是否应该以某种方式“取回”我的 savedInstanceState???

一如既往,感谢您的帮助。

H.

最佳答案

您可以使用 intent.putExtra() 进行 Activity 间交互...

一件事(仅用于诊断)我建议将静态变量初始化为某个值,然后运行应用程序...我想当 Intent 开始 Activity 返回时,您的全局类正在重新初始化...

我使用了全局变量,但在我的例子中,我将它们保存在一个永不消亡的 Activity 中。所有其他 Activity 都在它之后进行,它运行得非常好......

关于android - 跨 Activity 共享 "Global"变量,使用 Webview 时出现问题...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3100726/

相关文章:

c++ - 外部变量和数组声明问题 C++

javascript - React Native getElementById

javascript - React Native WebView 中的身份验证

android - 用于插入联系人的广播接收器

java - Android 游戏开发 - 移动手机

c - 为什么这个动态加载的库不能访问加载程序的全局变量?

c - 当有两个具有相同符号但不同类型的全局变量的 .c 文件时,引用如何工作?

android - 键盘以 Xamarin 形式将 Textarea 隐藏在混合 Webview 中

android - 如何使用 Android 的应用内更新 API?

Android AutoResizeTextView - 调整大小后关闭垂直重力