amk
amk

Reputation: 11

Main Activity doesn't extend AppCompatActivity

once I open android studio and I found an error in main activty in some functions like "startActivity(intent) it gives an error: (cannot resolve method 'startActivity' in 'mainActivity')" and many others (my activity extend AppCompatActivity) and I run the app on my physical phone and also works fine without any runtime error. so I asked chatgpt and I tried build>clean project... build>rebuild project.... file>sync project with gradle files.... file>Invalidate caches but non of these works for me.once I make my activity extends Activity only so the project works fine but I can't use themes and action bar and what I understand from that is AppCompatActivity doesn't extend Activity also fragmentActivity (gradle and manifest and all activities works fine with no issues and all dependencies are okay)..... but when I created this project it was extend AppCompatActivity normally just like other projects.... so I think that there is a missing folder or its path is missing or someone moves this folder from its original path in the file explorer

actually I don't know how this error comes so I can't make the error again but what I did is make a new project with empty activity but and I faced this error again... i make two activities and by Intent to pass between them Intent intent = new Intend(MainActivity.this, newActivity.class); startActivity(intent); so i have error on startActivity that says "can't resovle this methode 'startActivity' in 'mainActivity'" and in the manifest a red line under the activity name that says 'mainActivity must extend android.app.activity' and also in newActiivty

here is my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.tbgroup">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TBGroup"
        tools:targetApi="31">
        <activity
            android:name=".newActivity" //here is the error
            android:exported="false"/>
        <activity
            android:name=".MainActivity" //here is the error
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

and here is the gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.tbgroup"
        minSdk 28
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.7.0'
    implementation 'com.google.android.material:material:1.12.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

and here is the mainActivity

package com.example.tbgroup;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(MainActivity.this, newActiivty.class);
        startActivity(intent);  //here is the error
    }
}

newActivity

package com.example.tbgroup;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class newActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_activity);
    }
}

so any help to fix that would be appreciated!

Upvotes: 0

Views: 79

Answers (0)

Related Questions