[英]Getting error “Cannot make a static reference to the non-static method putExtra(String, String) from the type Intent”
I am trying to get the data from an EditText in my first screen and then be able to use this in my second screen. 我试图从我的第一个屏幕中的EditText获取数据,然后能够在我的第二个屏幕中使用它。 I am using putExtra method but it keeps giving me the error message 我正在使用putExtra方法但它不断给我错误消息
Cannot make a static reference to the non-static method putExtra(String, String) from the type Intent 无法从Intent类型对非静态方法putExtra(String,String)进行静态引用
Does anyone know how to solve this? 有谁知道如何解决这个问题? Thanks!!! 谢谢!!!
final EditText et = (EditText) findViewById(R.id.word);
Button btn1 = (Button) findViewById(R.id.go_btn2);
btn1.setOnClickListener(new OnClickListener(){
MediaPlayer buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_click);
@Override
public void onClick(View v){
buttonSound.start();
startActivity(new Intent(MainActivity.this, Second.class));
Intent.putExtra("name", et.getText().toString());
}});
You need to create an object of Intent
first: 您需要先创建一个Intent
对象:
final EditText et = (EditText) findViewById(R.id.word);
Button btn1 = (Button) findViewById(R.id.go_btn2);
btn1.setOnClickListener(new OnClickListener(){
MediaPlayer buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_click);
@Override
public void onClick(View v){
buttonSound.start();
Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra("name", et.getText().toString());
startActivity(intent);
}});
You have to create instance of Intent class then to use 您必须创建Intent类的实例然后才能使用
final EditText et = (EditText) findViewById(R.id.word);
Button btn1 = (Button) findViewById(R.id.go_btn2);
btn1.setOnClickListener(new OnClickListener(){
MediaPlayer buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_click);
@Override
public void onClick(View v){
buttonSound.start();
Intent intent = new Intent(MainActivity.this, Second.class) ;
intent.putExtra("name", et.getText().toString());
startActivity(intent);
}});
final EditText et = (EditText) findViewById(R.id.word);
Button btn1 = (Button) findViewById(R.id.go_btn2);
btn1.setOnClickListener(new OnClickListener(){
MediaPlayer buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_click);
@Override
public void onClick(View v){
buttonSound.start();
Intent intent = new Intent(MainActivity.this, Second.class)
startActivity(intent);
intent..putExtra("name", et.getText().toString());
//Intent.putExtra("name", et.getText().toString());//**This cause problem in your code**
}});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.