java putExtra String Intent Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5648304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:05:04  来源:igfitidea点击:

putExtra String Intent Android

javaandroidandroid-intent

提问by M_K

public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    m = lv.getAdapter().getItem(info.position).toString();

    Toast.makeText(getBaseContext(), "You clicked !"+m, Toast.LENGTH_SHORT).show();


    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to delete this Fixture?");
    builder.setCancelable(false);

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            //listAdapter.remove(m);

            Intent i  = new Intent(Afc.this.getApplicationContext(), WebViewExample.class);
            i.putExtra("Key", m); 
            startActivity(i);   
        }

//new class activity WebView/////////////

//新类活动WebView////////////

Bundle extras = getIntent().getExtras(); 
      String addOn = extras.getStringExtra("key",m);
      Toast.makeText(getBaseContext(), "You clicked !"+addOn, Toast.LENGTH_SHORT).show();

Hi I am passing a string with an intent, its giving me a error in the new activity on the variable m, inside the getStringExtra("key",m);. can anyone help?

嗨,我正在传递一个带有意图的字符串,它在 getStringExtra("key",m); 内的变量 m 上的新活动中给了我一个错误。有人可以帮忙吗?

Am I doing this the right way?

我这样做是否正确?

回答by RoflcoptrException

The key of putExtra is case sensitive. One time you use

putExtra 的键区分大小写。一次使用

key

钥匙

and in the another

而在另一个

Key

钥匙

You should use key or Key in both cases.

在这两种情况下,您都应该使用 key 或 Key。

In any case use getStringExtra("key") instead of getStringExtra("key", m). As you can see in the documentationthere isn't a getStringExtra method that takes two parameters. To explain: your variable m can't be resolved because you just declared it in your first class but not in the second.

在任何情况下都使用 getStringExtra("key") 而不是 getStringExtra("key", m)。正如您在文档中看到的那样,没有采用两个参数的 getStringExtra 方法。解释一下:您的变量 m 无法解析,因为您只是在第一堂课中声明了它,而没有在第二堂课中声明它。

回答by Harinder

Yes and getStringExtra("key",m);??? is Wrong USE getStringExtra("key") ;

是和 getStringExtra("key",m);???使用 getStringExtra("key") 是错误的;