![](/img/trans.png)
[英]Error: “Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity”
[英]Getting error “Cannot make a static reference to the non-static method putExtra(String, String) from the type Intent”
我試圖從我的第一個屏幕中的EditText獲取數據,然后能夠在我的第二個屏幕中使用它。 我正在使用putExtra方法但它不斷給我錯誤消息
無法從Intent類型對非靜態方法putExtra(String,String)進行靜態引用
有誰知道如何解決這個問題? 謝謝!!!
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());
}});
您需要先創建一個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);
}});
您必須創建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.