【问题标题】:Passing data from one activity to another using bundle - not displaying in second activity使用捆绑将数据从一个活动传递到另一个活动 - 不在第二个活动中显示
【发布时间】:2013-03-04 22:07:16
【问题描述】:

我目前正在尝试获取通过 REST API 调用获取的数据,将其解析为我需要的信息,然后将该信息传递给新活动。我将 loopj.com 的异步 HTTP 客户端用于 REST 客户端,然后将以下代码分别用于我的 onClickonCreate 用于当前和未来的活动。

Eclipse 没有为我的任何代码传递任何错误,但是当我尝试在模拟器中运行时,当新的活动/视图打开时,我什么也得不到(即空白屏幕)。我尝试在我的 REST CLIENT 中使用不同的 URL 进行编码,但我仍然什么也看不到。我什至通过注释掉onClick 中的try/catch 并将bundle.putString("VENUE_NAME", venueName); 中的venueName 更改为searchTerm,从而将API 调用排除在外。尽管如此,新视图仍然出现,但没有显示任何内容。什么没有通过,或者我忘记让第二个活动显示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);
}

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

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

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

【问题讨论】:

    标签: android json


    【解决方案1】:

    您可以通过两种方式发送数据。这就是您目前发送它的方式。而且也没有错。

    //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);
    

    但是,在您的代码(第二个活动)中,您将 Bundle 中的 key 称为 MainActivity.VENUE_NAME,但代码中没有任何内容表明您有一个将值作为实际 key 名称发送的类返回与捆绑。将第二个 Activity 中的代码更改为:

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

    如果 Bundle 包含使用此密钥的密钥,您可以签入您的第二个 Activity,并且您将知道 key 不存在于 Bundle 中。然而,上面的更正会让它为你工作。

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

    【讨论】:

    • 这是一个非常简洁但非常详细的答案。谢谢你写得这么清楚。它帮助我快速了解了如何使用捆绑包。
    • 谢谢。这个例子帮助了我。
    【解决方案2】:

    我想如果你替换

    String venName = bundle.getString(MainActivity.VENUE_NAME); 
    

    String venName = bundle.getString("VENUE_NAME");
    

    它应该工作。

    以下是我如何处理从一项活动到另一项活动的数据传输:

    向名为“Projectviewoptions”的活动发送数据:

    Bundle b = new Bundle();
              b.putString("id", str_projectid);
              Projectviewoptions pv = new Projectviewoptions();
    

    接收数据:

    idbundle = getArguments();
    String myid = idbundle.getString("id");
    

    两边的“key”应该相同;在本例中为“id”。

    通过 Intent 发送数据的另一种方式是:

    发送:

    Intent intent = new Intent(getActivity(),ViewProjectDetails.class);
                                intent.putExtra("id", myid);
                                startActivity(intent);
    

    收到:

    String id = getIntent().getExtras().getString("id");
    

    【讨论】:

      【解决方案3】:

      您错误地访问了您在捆绑包中添加的密钥。除了将字符串作为MainActivity.VENUE_NAME 之外,尝试直接传递您在捆绑包中添加的键名,如下所示:

      除了获取字符串如下:

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

      尝试使用如下键名获取字符串:

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

      【讨论】:

        【解决方案4】:

        发送捆绑包。

        Bundle bundle = new Bundle();
        bundle.putString("Name",Object); //This is for a String
        i.setClass(CurrentClass.this, Class.class);
        i.putExtras(bundle);
        startActivity(i);
        

        接收捆绑包

        Bundle bundle = null;
        bundle = this.getIntent().getExtras();
        String myString = bundle.getString("Name");//this is for String  
        

        【讨论】:

        • 这里是什么“我”。请解释我的含义
        • 如果你定义意图 i = new Intent();我已经在尝试这个请解释你的答案
        【解决方案5】:

        确保您用作将项目放入捆绑包中的密钥的字符串与用于提取它的密钥相同。在您的情况下,MainActivity.VENUE_NAME 可能与“VENUE_NAME”不同

        【讨论】:

          【解决方案6】:
          Intent loginIntent = new Intent(LoginActivity.this, HomeActivity.class);
          
          Bundle bundle = new Bundle(); 
          
          
          bundle.putString("user_id", userId);
          
          i.putExtras(bundle);
          
          startActivity(loginIntent);
          
          LoginActivity.this.finish();
          

          【讨论】:

            【解决方案7】:
            package com.example.mytest;
            
            import androidx.annotation.MainThread;
            import androidx.annotation.Nullable;
            import androidx.appcompat.app.AppCompatActivity;
            
            import android.content.Intent;
            import android.os.Bundle;
            import android.view.View;
            import android.widget.Button;
            import android.widget.EditText;
            import android.widget.Toast;
            
            import static com.example.mytest.MainActivity.STATIC_VARIABLE;
            
            public class MainActivity2 extends AppCompatActivity {
               EditText editText1;
                Button button1;
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main2);
                    editText1 = (EditText) findViewById(R.id.editText1);
                    button1 = (Button) findViewById(R.id.button1);
                    Bundle extras=null;
                     extras=this.getIntent().getExtras();
                    if(extras!=null) {
                      String recieved =extras.getString("TAG");
                      editText1.setText(String.valueOf(recieved));
                    }
                    button1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
              Intent backIntent = new Intent(MainActivity2.this, MainActivity.class);
                            String input= editText1.getText().toString();
                            backIntent.putExtra("TAG2",input);
                           STATIC_VARIABLE=3;
            //    setResult(RESULT_OK,intent);
            //                startActivityForResult(intent, 2);
                            startActivity(backIntent);
            
            
                        }
                    });
                }
            
            
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-07-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-12-20
              相关资源
              最近更新 更多