简体   繁体   English

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

[英]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.

相关问题 错误:“无法从类型Activity中对非静态方法startActivity(Intent)进行静态引用” - Error: “Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity” 无法从类型XHTMLImporter静态引用非静态方法setHyperlinkStyle(String) - Cannot make a static reference to the non-static method setHyperlinkStyle(String) from the type XHTMLImporter 无法从类型teaminfo对非静态方法acceptadd(String)进行静态引用 - Cannot make a static reference to the non-static method acceptadd(String) from the type teaminfo 无法从DialogFragment类型静态引用非静态方法show(FragmentManager,String) - Cannot make a static reference to the non-static method show(FragmentManager, String) from the type DialogFragment 收到“无法对非静态方法进行静态引用”错误,但我尚未将要从中调用的方法声明为静态方法 - getting “cannot make a static reference to the non-static method” error, but I haven't declared the method I'm calling from as static Java错误:无法对非静态方法进行静态引用 - Java Error: Cannot make a static reference to the non-static method 无法解析webView且无法从Activity类型静态引用非静态方法findViewById(int) - webView cannot be resolved & cannot make a static reference to the non-static method findViewById(int) from the type Activity 类型无法从类型View静态引用非静态方法setVisibility(int) - Type Cannot make a static reference to the non-static method setVisibility(int) from the type View 无法从类型Cmd对非静态方法getVideoURL()进行静态引用 - Cannot make a static reference to the non-static method getVideoURL() from the type Cmd 无法从View MainActivity.java类型静态引用非静态方法invalidate() - Cannot make a static reference to the non-static method invalidate() from the type View MainActivity.java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM