Java 活动正在接收数据但未显示

Java 活动正在接收数据但未显示,java,android,eclipse,android-intent,Java,Android,Eclipse,Android Intent,我正在将字符串从活动A传递到B,然后我将从A中的活动B获取数据。因此,我使用了startActivityForResult()。我总是得到“不为空”。我正在接收数据,但我无法在活动B的文本视图中查看该数据。我正在发布代码。请告诉我出了什么问题 活动A(发送数据): 活动B(接收数据): 附言: 我在活动B中进行了更改,以查看是否获得了字符串。它仍然显示“is not Null”,这意味着我没有得到字符串。但我不知道为什么这不显示。 代码如下: Bundle got = getIntent().g

我正在将字符串从活动A传递到B,然后我将从A中的活动B获取数据。因此,我使用了startActivityForResult()。我总是得到“不为空”。我正在接收数据,但我无法在活动B的文本视图中查看该数据。我正在发布代码。请告诉我出了什么问题

活动A(发送数据):

活动B(接收数据):

附言: 我在活动B中进行了更改,以查看是否获得了字符串。它仍然显示“is not Null”,这意味着我没有得到字符串。但我不知道为什么这不显示。 代码如下:

Bundle got = getIntent().getExtras();
String ss= got.getString("UmerData");
if(getIntent()!=null && ss==null )
{
    //Bundle got = getIntent().getExtras();
    //rt1.setText(got.getString("UmerData"));
    rt1.setText("Is not Null");
}
这是在活动B中:

private void InitializeFoo() {
    // TODO Auto-generated method stub
    Rg = (RadioGroup) findViewById(R.id.RG);
    rt1 = (TextView) findViewById(R.id.Rt1);
    rt2 = (TextView) findViewById(R.id.Rt2);
    rec = (Button) findViewById(R.id.Return);
    Rg.setOnCheckedChangeListener(this);
    rec.setOnClickListener(this);
}

谢谢

如果您使用putExtras(Bundle)添加额外功能,则需要在值对的键中包含包名。参考文件:

“向意向添加一组扩展数据。按键必须包含程序包前缀,例如,应用程序com.android.contacts将使用类似“com.android.contacts.ShowAll”的名称。”

你可以随时使用

data.putExtra("UmerData", cheese);

所以有两个主要错误。 第一个是在SendData中,就在调用ReceiveData之前,您设置为intent的cheese的值为null,这是您在第二个活动中接收null的主要原因。 以下代码必须适用于这些情况,因此在“如果”括号外:

cheese = Dt.getText().toString();
另一件事(实际上,在您提供的代码中是可以的)是,您将bundle“check”设置为一个extra本身(通过调用putExtra(…)),而不是一个由键值对组成的bundle(通过调用putExtras(…))

它在两个地方都测试到了正确的值。 为了澄清,我附上有问题的活动

package com.umer.practice2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SendData extends Activity implements View.OnClickListener {

    EditText Dt;
    Button Dbut, result;
    TextView medt;
    String cheese;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.data);

    InitializeShit();
    }

    private void InitializeShit() {
    // TODO Auto-generated method stub

    Dt = (EditText) findViewById(R.id.Det);
    Dbut = (Button) findViewById(R.id.Sna);
    result = (Button) findViewById(R.id.Sfr);
    medt = (TextView) findViewById(R.id.Med);

    Dbut.setOnClickListener(this);
    result.setOnClickListener(this);

    }

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    cheese = Dt.getText().toString();
    switch (arg0.getId()) {
        case R.id.Sna:

        Bundle send = new Bundle();

        send.putString("UmerData", cheese);
        Intent foo = new Intent(SendData.this, RecieveData.class);

        foo.putExtras(send);

        startActivity(foo);
        break;

        case R.id.Sfr:
        Intent data = new Intent(SendData.this, RecieveData.class);
        Bundle check = new Bundle(); // NOT IN TRAVIS CODE

        check.putString("UmerData", cheese);// NOT IN TRAVIS CODE
        medt.setText(cheese);// NOT IN TRAVIS CODE
        data.putExtras(check);// NOT IN TRAVIS CODE
        startActivityForResult(data, 5);
        break;
    }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        Bundle roti = new Bundle();
        roti = data.getExtras();
        String ch = roti.getString("String");
        medt.setText(ch);
    }
    }
}

希望对您有所帮助。

您的活动B是否扩展了活动?否。它没有扩展任何自定义活动您注释掉的代码是正确的。哪一部分返回null,
getExtras()
getString()
?我不确定。让我检查一下!@Devunwired我更新了我的问题,我收到了字符串,但我不知道为什么活动B没有显示它。
cheese = Dt.getText().toString();
package com.umer.practice2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SendData extends Activity implements View.OnClickListener {

    EditText Dt;
    Button Dbut, result;
    TextView medt;
    String cheese;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.data);

    InitializeShit();
    }

    private void InitializeShit() {
    // TODO Auto-generated method stub

    Dt = (EditText) findViewById(R.id.Det);
    Dbut = (Button) findViewById(R.id.Sna);
    result = (Button) findViewById(R.id.Sfr);
    medt = (TextView) findViewById(R.id.Med);

    Dbut.setOnClickListener(this);
    result.setOnClickListener(this);

    }

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    cheese = Dt.getText().toString();
    switch (arg0.getId()) {
        case R.id.Sna:

        Bundle send = new Bundle();

        send.putString("UmerData", cheese);
        Intent foo = new Intent(SendData.this, RecieveData.class);

        foo.putExtras(send);

        startActivity(foo);
        break;

        case R.id.Sfr:
        Intent data = new Intent(SendData.this, RecieveData.class);
        Bundle check = new Bundle(); // NOT IN TRAVIS CODE

        check.putString("UmerData", cheese);// NOT IN TRAVIS CODE
        medt.setText(cheese);// NOT IN TRAVIS CODE
        data.putExtras(check);// NOT IN TRAVIS CODE
        startActivityForResult(data, 5);
        break;
    }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        Bundle roti = new Bundle();
        roti = data.getExtras();
        String ch = roti.getString("String");
        medt.setText(ch);
    }
    }
}