首页 > 解决方案 > 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>```


标签: javaandroid

解决方案


正如上面帖子的评论中提到的,解决这个问题的方法是通过 file=>invalidates Caches/Restart 清除缓存的文件。


推荐阅读