gpt4 book ai didi

android - setOnClickListener 错误 - 无法解析符号

转载 作者:行者123 更新时间:2023-11-29 17:16:54 25 4
gpt4 key购买 nike

我想创建一个按钮,单击该按钮时会显示另一个 Intent 。

这是代码:

   <Button
android:text="Bine ati venit!"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button1"
android:background="#479fc7"
android:textSize="40dp"
android:textColor="@color/colorPrimaryDark"
android:textStyle="normal|bold|italic"
android:textAllCaps="false"
android:fontFamily="casual"
android:onClick="next_page"/>

来自 .java 类:

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}

Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener( new View.OnClickListener()
{
public void onClick (View v){
next_page(v);
}
});

public void next_page(View v) {
Intent intent = new Intent(this, MapsActivity.class);
startActivity(intent);
}

我得到错误:“无法解析符号‘setOnClickListener’”、“从未使用变量 onClick”、“;预期”……

最佳答案

//你必须将你输入的代码移动到类方法中并像下面这样工作

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the View that shows the numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);

// Set a click listener on that View
numbers.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
@Override
public void onClick(View view) {
Intent numbersIntent = new Intent(MainActivity.this, NumberActivity.class);
startActivity(numbersIntent);
}
});
}

关于android - setOnClickListener 错误 - 无法解析符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38461374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com