gpt4 book ai didi

安卓工作室 : error: type annotations are not supported

转载 作者:行者123 更新时间:2023-11-30 00:14:51 26 4
gpt4 key购买 nike

我刚刚尝试在 Android Studio 中为按钮添加 OnClickListener(),但出现上述错误提示我更改为 -source 8 或更高版本。

下面这段代码有什么问题?

public class MainActivity extends AppCompatActivity
{
public Button butTest;

//---- THIS BLOCK HERE CAUSES PROBLEM ---->
public final View.OnClickListener butTest_OnClickListener = new View.OnClickListener()
{
@Override
public void onClick( View view )
{
//Toast.makeText( this, "Button1 clicked.", Toast.LENGTH_SHORT ).show();
}
}
// <<------ END OF PROBLEM

@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );

butTest = (Button) findViewById( R.id.butTest );
butTest.setOnClickListener( butTest_OnClickListener );
}
}

最佳答案

您需要将它放在一个方法中(例如 onCreate)。你可以这样做:

@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );

butTest = (Button) findViewById( R.id.butTest );

butTest.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick( View view )
{
//Toast.makeText( this, "Button1 clicked.", Toast.LENGTH_SHORT ).show();
}
});
}

希望对您有所帮助。

关于安卓工作室 : error: type annotations are not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47461957/

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