java - XML 文件无法引用类文件
问题描述
大约一周前,由于某些关键问题,android studio 几乎破坏了我编写的所有代码,而这些关键问题在网上几乎找不到。我试图找到解决方案的一个是 xml 文件,无论是 AndroidManifest.xml 文件还是活动文件都在类上出错。
我尝试使用 alt+enter 并通过在已经存在的文件的同名下创建一个新文件来自动修复它,并且在一秒钟内错误消失了,只是再次出现错误。我已经重新安装了 android studio,试图查看我是否以某种方式损坏了文件。
我写的唯一代码是 2 个按钮。因此,除非我做错了什么,否则我不确定类中的代码是如何导致这种情况的。在“工具:上下文”字段下,文件上出现错误,指出类文件无法识别(在 xml 文件中)。
主要活动:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Options_Button1(View view) {
Intent option = new Intent(MainActivity.this,Options.class);
startActivity(option);
}
public void Add_Button1(View view){
Intent add = new Intent(MainActivity.this,placeholder.class);
startActivity(add);
}
}
活动xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:clickable="true"
android:focusable="true"
android:onClick="Options_Button1"
android:text="@string/options6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="304dp"
android:layout_marginTop="16dp"
android:clickable="true"
android:focusable="true"
android:onClick="Add_Button1"
android:text="@string/add6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>```
解决方案
正如上面帖子的评论中提到的,解决这个问题的方法是通过 file=>invalidates Caches/Restart 清除缓存的文件。
推荐阅读
- visual-studio - 如何将 Visual Studio 2019 中的调试输出重定向到文件?
- php - WordPress 检查查询是否针对 pre_get_posts 过滤器中的自定义类型
- azure - 从 Azure Active Directory 中的多个租户获取用户详细信息
- mysql - MySQL JSON_EXTRACT() 查询返回 null
- c# - 将帖子发送到 Facebook 页面
- rotation - 如何在 OpenLayers 6 上添加旋转和缩放监听器?
- python - Python 中 Newton-Raphson 中的 TypeError
- events - 在限界上下文中分离消息系统
- perl - Perl 文件读取在将其拆分为空间时出现问题
- c - 这个 C 代码对于一个简单的计算器程序有什么问题?