setText 显示为红色,我不明白为什么

setText is shown in red and i don't undrestand why

setText 显示为红色,我不明白为什么

public class MainActivity extends AppCompatActivity {

    TextView v = (TextView)findViewById(R.id.textInput);
    Button b = (Button)findViewById(R.id.btn12);
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b.setOnClickListener((new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                v.setText("aaa");// why setText not found
            }
        }));


    }

}

感谢

移动这些方法:

TextView v = (TextView)findViewById(R.id.textInput);
Button b = (Button)findViewById(R.id.btn12);

您可以使用:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView v = (TextView)findViewById(R.id.textInput);
    Button b = (Button)findViewById(R.id.btn12);
    b.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            v.setText("aaa");// why setText not found
        }
    });

或者您可以使用:

public class MainActivity extends AppCompatActivity {

    private TextView v;
    private Button b;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        v = (TextView)findViewById(R.id.textInput);
        b = (Button)findViewById(R.id.btn12);
        b.setOnClickListener(new View.OnClickListener(){
           @Override
           public void onClick(View view) {
            v.setText("aaa");// why setText not found
           }
        });

    }

}

您收到错误消息是因为 setText() 方法在 View

中不可用

并且您试图访问 onClick () 方法中的 View.setText("aaa");

在您的代码中进行以下更改

  1. onCreate()方法中执行findViewById()
  2. 重命名您的 TextView 变量

试试这个

public class MainActivity extends AppCompatActivity {

    TextView textView;
    Button b ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        
        textView = (TextView)findViewById(R.id.textInput);
        b = (Button)findViewById(R.id.btn12)
        b.setOnClickListener((new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                textView.setText("aaa");// why setText not found
            }
        }));


    }

}

setText 不是一种视图方法,您需要将其设为 EditText,只要视图始终是 EditText:

public class MainActivity 扩展 AppCompatActivity {

TextView v = (TextView)findViewById(R.id.textInput);
Button b = (Button)findViewById(R.id.btn12);
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b.setOnClickListener((new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            ((EditText)v).setText("aaa");// why setText not found
        }
    }));


}

}