Android Studio中的Intent问题

时间:2016-03-13 17:14:09

标签: java android android-intent android-studio

我一直试图让一个字符串从一个java类转到另一个我的项目。我一直在试验的代码不起作用。当我按下按钮时,我知道它打开了另一个Java类,因为它创建了另一个布局,但它没有显示字符串。请帮帮我。

First Java Class:

public class MainActivity extends AppCompatActivity {

private Button button;
Context context;
private EditText editText;
String number = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = this;

    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (editText.getText().toString() != null) {
                String value = "value";
                Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
                secondscreenIntent.putExtra("Number", editText.getText().toString());
                startActivity(secondscreenIntent);


            }
        }
    });
}
}

第二个Java类:

public class SecondScreenJ extends Activity {

String number = null;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondscreen);

    textView = (TextView) findViewById(R.id.textView);

    Bundle extras = getIntent().getExtras();
    if (extras != null){
        number = extras.getString("number");
    }

    textView.setText(number);

}


}

3 个答案:

答案 0 :(得分:3)

  你输了#34; Number"作为关键但是,在你的第二个活动中,你试图检索"数字"   所以将数字更改为数字,它将起作用。   它的情况敏感。

答案 1 :(得分:2)

不要以这种方式对您的密钥进行硬编码。 只需在MainActivity中声明公共静态变量,并在SecondScreenJ中使用它

public class MainActivity extends AppCompatActivity {

private Button button;
Context context;
private EditText editText;
public static String NUMBER_KEY = "Number";
String number = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

context = this;

editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (editText.getText().toString() != null) {
            String value = "value";
            Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
            secondscreenIntent.putExtra(NUMBER_KEY , editText.getText().toString());
            startActivity(secondscreenIntent);


        }
    }
});
 }
 }

第二个Java类:

public class SecondScreenJ extends Activity {

String number = null;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.secondscreen);

 textView = (TextView) findViewById(R.id.textView);

 Bundle extras = getIntent().getExtras();
 if (extras != null){
    number = extras.getString(MainActivity.NUMBER_KEY);
 }

 textView.setText(number);

 }


}

答案 2 :(得分:1)

当使用钥匙放置和获取额外物品时要小心。他们区分大小写。在第二项活动中将"number"替换为"Number"