简体   繁体   English

无法解析 MainActivity 中的方法“findViewById”

[英]Cannot resolve method “findViewById” in MainActivity

I'm suddenly getting "cannot resolve" errors for findViewById(int) and setContentView().我突然收到 findViewById(int) 和 setContentView() 的“无法解决”错误。 I don't want to get into the knitty gritty of what my app does, but it was running fine until suddenly it wasn't after some time working on unrelated functions, with no changes made to the code.我不想深入了解我的应用程序所做的事情,但它运行良好,直到突然在一段时间后处理不相关的功能,没有对代码进行任何更改。 I tried reverting to a previous day when I was able to get the code to run, but the issue persists.当我能够让代码运行时,我尝试恢复到前一天,但问题仍然存在。

I've looked through a lot of help threads, and I've tried the "Invalidate Caches/Restart" option, but that didn't have any effect unfortunately我查看了很多帮助线程,并尝试了“使缓存无效/重新启动”选项,但不幸的是没有任何效果

public class MainActivity extends FragmentActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
        setContentView(R.layout.main_activity);
        myDialog = new Dialog(this);
        displayFragment = (inputsFragment) getSupportFragmentManager().findFragmentById(R.id.main_inputs_frag);
        progressBar = findViewById(R.id.progressBar);
        ShowPopup(findViewById(android.R.id.content), -1);
    }
}

this.getSystemService(VIBRATOR_SERVICE) now gives the error, "cannot resolve getSystemService in MainActivity", for presumably the same reason. this.getSystemService(VIBRATOR_SERVICE) 现在给出错误“无法解析 MainActivity 中的 getSystemService”,大概是出于相同的原因。

In addition, activity android:name=".MainActivity" in my manifest now gives the error: "MainActivity must extend Android.app.Activity."此外,我的清单中的活动 android:name=".MainActivity" 现在给出错误:“MainActivity 必须扩展 Android.app.Activity。” Which seems strange, since to my understanding FragmentActivity inherits Activity这似乎很奇怪,因为据我了解 FragmentActivity 继承了 Activity

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidprogramlol">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.VIBRATE"/>



</manifest>

i literally copy pasted all my files into a new project and now it works:)我从字面上将所有文件复制粘贴到一个新项目中,现在它可以工作了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM