获取错误“无法对类型为Intent的非静态方法putExtra(String,String)进行静态引用”

时间:2013-11-20 05:13:53

标签: java android android-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());
        }});

3 个答案:

答案 0 :(得分:1)

您需要先创建一个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);
        }});

答案 1 :(得分:1)

您必须创建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);
        }});

答案 2 :(得分:1)

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