Cannot resolve method “findViewById” in MainActivity

I’m suddenly getting “cannot resolve” errors for findViewById(int) and 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.

In addition, activity android:name=”.MainActivity” in my manifest now gives the error: “MainActivity must extend Android.app.Activity.” This seems strange, since to my understanding FragmentActivity inherits 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>

Source: Android Questions

Author Details

admin photo

sobuj033@gmail.com

Sobuj Ali

Founder & CEO

Sobuj Ali is Founder & CEO at Apkpm.com. I have a good knowledge of Marketing. So I provide all Technology information on their website.

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Rating: