简体   繁体   English

在Java中的类之间传递变量

[英]Transferring Variables Between Classes In Java

I have created and XML parser that acquires the data in the description tag. 我已经创建了XML解析器,该解析器获取描述标记中的数据。 I am attempting to pass it to a web view because it has HTML formatting. 我正在尝试将其传递到Web视图,因为它具有HTML格式。 I am new to Java and I don't really know how to do that. 我是Java的新手,我真的不知道该怎么做。

This is what I have in the first class: 这是我在头等舱中所拥有的:

 Intent intent = new Intent(this,Webscreen.class);
 Bundle bundle = new Bundle();
 bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
 intent.putExtras(bundle);
 startActivity(intent);

This is what I have so far in the second class: 这是我到目前为止在第二节课中得到的:

public static final String URL = "";
private static final String TAG = "WebscreenClass";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webscreen);

    if(URL == null && URL == ""){
        WebView webview = (WebView) findViewById(R.layout.webscreen);
        String htmlString = keyDescription;
        webview.loadData(htmlString, "text/html", "utf-8");
    }else{
        String turl = getIntent().getStringExtra(URL);
        Log.i(TAG, " URL = "+turl);

        WebView webview = new WebView(this);
        WebSettings websettings = webview.getSettings();
        websettings.setJavaScriptEnabled(true);
        setContentView(webview);


        // Simplest usage: An exception won't be thrown if there is a page-load error
        webview.loadUrl(turl);
    }
}

I am using the if else statement because I use this class to load web pages as well. 我之所以使用if else语句,是因为我也使用此类加载网页。 Thank you for your help! 谢谢您的帮助!

在第二个活动中,只需检索额外数据。

  Bundle extras = getIntent().getExtras();

first: Your if-Statment is wrong. 第一:您的if陈述有误。 It would be "false" at every run. 每次运行都为“假”。 You have to use "||" 您必须使用“ ||” instead of "&&". 代替 ”&&”。 The problem is that your URL String could only be "null" or empty. 问题是您的URL字符串只能为“ null”或为空。

second: You can get your "keyDescription" with following statment in your second activity: 第二:您可以在第二个活动中获得带有以下声明的“ keyDescription”:

String keyDescription = savedInstanceState.getString("keyDescription");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM