从一个活动传递数据到另一个使用捆绑 - 在第二次活动不显示 [英] Passing data from one activity to another using bundle - not displaying in second activity

查看:138
本文介绍了从一个活动传递数据到另一个使用捆绑 - 在第二次活动不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图采取通过REST API调用获取的数据,分析它,因为我需要的信息,然后将该信息传递到一个新的活动。我使用的是异步HTTP客户端从loopj.com的REST客户端,然后使用下面的code我的的onClick 的onCreate 为当前和今后的活动,分别。

I'm currently trying to take data acquired through a REST API call, parse it for the information I need, and then pass that information to a new activity. I'm using the Asynchronous HTTP Client from loopj.com for the REST client, and then using the below code for my onClick and onCreate for the current and future activities, respectively.

Eclipse不传给我任何错误,任何我的code,但是当我试图在模拟器中运行,我什么也没有(即空白屏幕)的新活动/视图打开时。我试着code在我的REST客户端不同的URL,但我还是什么也看不见。我甚至把API调用的方程被注释掉的try / catch在的onClick 和不断变化的 venueName bundle.putString(VENUE_NAME,venueName); 搜索关键词。尽管如此,新的观点出现,但不显示任何内容。什么是不被通过,或者是什么我忘记了做第二个活动显示 venueName

Eclipse does not pass me any errors for any of my code, but when I try to run in the emulator, I get nothing (i.e. blank white screen) when the new activity/view opens. I've tried to code with a different URL in my REST CLIENT, but I still see nothing. I even took the API call out of the equation by commenting out the try/catch in onClick and changing venueName in the bundle.putString("VENUE_NAME", venueName); to searchTerm. Still, the new view comes up but nothing is displayed. What is not being passed, or what am I forgetting to make the second activity show the venueName?

public void onClick(View view) {
    Intent i = new Intent(this, ResultsView.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String searchTerm = editText.getText().toString();


    //call the getFactualResults method
    try {
        getFactualResults(searchTerm);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //Create the bundle
    Bundle bundle = new Bundle();
    //Add your data from getFactualResults method to bundle
    bundle.putString("VENUE_NAME", venueName);  
    //Add the bundle to the intent
    i.putExtras(bundle);

    //Fire the second activity
    startActivity(i);
}

在第二个活动的方法应该接收的意图和捆绑,并显示它:

Method in second activity that should receive the intent and bundle and display it:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Get the message from the intent
    //Intent intent = getIntent();
    //String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    //Get the bundle
    Bundle bundle = getIntent().getExtras();

    //Extract the data…
    String venName = bundle.getString(MainActivity.VENUE_NAME);        

    //Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(venName);

    //set the text view as the activity layout
    setContentView(textView);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

感谢您的帮助。非常AP preciated。

Thanks for your help. Very much appreciated.

推荐答案

两种方法可以发送数据。这是你如何在此刻发送。并没有什么不妥的地方。

Two ways you can send the data. This is how you are sending it at the moment. And there is nothing wrong with it.

//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("VENUE_NAME", venueName);
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);

在你code(次活动),但是,你指的是被捆绑的 MainActivity.VENUE_NAME ,但没有在code表明你有返回值作为实际键类名派同捆。在第二个活动更改code到这一点:

In you code (second Activity) however, you are referring to the key in the Bundle as MainActivity.VENUE_NAME but nothing in the code suggests that you have a class that returns the value as the actual key name send with the Bundle. Change your code in the second Activity to this:

Bundle bundle = getIntent().getExtras();

//Extract the data…
String venName = bundle.getString("VENUE_NAME");        

//Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(venName);

您可以检查你的第二个活动,如果包中包含使用该密钥,你会知道不是present的捆绑。校正上面,但是,将得到它为你工作。

You can check in your second Activity if the Bundle contains the key using this and you will know that the key is not present in the Bundle. The correction above, however, will get it working for you.

if (bundle.containsKey(MainActivity.VENUE_NAME))    {
    ....
}

这篇关于从一个活动传递数据到另一个使用捆绑 - 在第二次活动不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆