Android按钮ID声明

huangapple go评论56阅读模式
英文:

android button id declaration

问题

XML文件中我已经设置了按钮的ID,但当我尝试查找按钮的ID时,它显示了一个错误。
XML文件:

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/btnrotation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/rorationbtn"
    android:layout_below="@+id/imgbox"
    android:layout_centerInParent="true"
    android:layout_marginTop="50dp">
</androidx.appcompat.widget.AppCompatButton>

Java文件:

public class rotation extends AppCompatActivity {
    AppCompatButton rotate;
    ImageView imgbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rotation);
        getSupportActionBar().setTitle("Rotation");
        imgbox = findViewById(R.id.imgbox);
        rotate = findViewById(R.id.btnrotation);
    }
}

错误:
不包含ID为'btnrotation'的声明。

英文:

I have set the button id in xml file but while I tried to find the id of the button it showing an error
xml file

&lt;androidx.appcompat.widget.AppCompatButton
        android:id=&quot;@+id/btnrotation&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;@string/rorationbtn&quot;
        android:layout_below=&quot;@+id/imgbox&quot;
        android:layout_centerInParent=&quot;true&quot;
        android:layout_marginTop=&quot;50dp&quot;&gt;
    &lt;/androidx.appcompat.widget.AppCompatButton&gt;

java file

public class rotation extends AppCompatActivity {
    AppCompatButton rotate;
    ImageView imgbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rotation);
        getSupportActionBar().setTitle(&quot;Rotation&quot;);
        imgbox=findViewById(R.id.imgbox);
        rotate=findViewById(R.id.btnrotation);

    }

error
does not contain a declaration with id 'btnrotation'

答案1

得分: 0

只需重新构建您的项目并再次运行。

英文:

Just rebuild your project and run again

答案2

得分: 0

有时候这是在Android Studio中的一个错误,您可以尝试关闭并重新打开Android Studio。如果不起作用,您可以使缓存无效或清理项目并重新构建它。

英文:

Sometimes it is a bug in android studio, you can try close and reopen Android Studio. If it doesn't work, you can invalidate caches or clean project and rebuild it.

答案3

得分: 0

只需点击顶部工具栏(文件)中的“使缓存无效”,然后点击“使缓存无效并重新启动”。谢谢。

英文:

Just click on Invalidate Caches from top Toolbar (File) , and then click on Invalidate & Restart. Thank You

huangapple
  • 本文由 发表于 2023年1月9日 02:00:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定