7

I encountered a problem while using Android Studio. When I'm trying to run any app, I get the same error "Default activity not found", and in my code in line tools:context=".MainActivity", MainActivity is highlighted red and it says "Unresolved class MainActivity". It happens even if I create a brand new "empty activity".

So far I've tried:

  • refreshing IDE cache
  • checked package names in Android manifest and MainActivity
  • selecting a default activity in-app configuration
  • made sure that src is the source directory

I've also noticed that in my "most advanced" app the build.gradle looks like this:

Screenshot

Android manifest:

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

    <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>

</manifest>

Project directory + code:

dir

activity main 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">

    <ImageView
        android:id="@+id/coffee_grains"
        android:layout_width="match_parent"
        android:layout_height="300sp"
        android:contentDescription="Coffee Grains"
        android:scaleType="centerCrop"
        android:src="@drawable/coffee_grains"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/cup"
        android:layout_width="120sp"
        android:layout_height="170sp"
        android:layout_marginLeft="8dp"
        android:src="@drawable/cup"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/coffee_grains" />

    <TextView
        android:id="@+id/quantity"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16sp"
        android:layout_marginTop="16sp"
        android:gravity="center"
        android:text="quantity"
        android:textAllCaps="true"
        android:textSize="16sp"
        app:layout_constraintLeft_toRightOf="@id/cup"
        app:layout_constraintTop_toBottomOf="@id/coffee_grains" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintLeft_toRightOf="@id/cup"
        app:layout_constraintTop_toBottomOf="@id/quantity">

        <Button
            android:id="@+id/plus"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginLeft="8dp"
            android:onClick="increment"
            android:text="+" />

        <TextView
            android:id="@+id/quantity_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8sp"
            android:layout_marginRight="8sp"
            android:gravity="center"
            android:text="1"
            android:textColor="#000000"
            android:textSize="14sp" />

        <Button
            android:id="@+id/miuns"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginLeft="8dp"
            android:onClick="decrement"
            android:text="-" />
    </LinearLayout>

    <TextView
        android:id="@+id/price"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="order summary"
        android:textAllCaps="true"
        android:textSize="16sp"
        app:layout_constraintLeft_toRightOf="@id/cup"
        app:layout_constraintTop_toBottomOf="@id/linearLayout" />

    <TextView
        android:id="@+id/order_summary_text_view"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="0$"
        android:textSize="16dp"
        app:layout_constraintLeft_toRightOf="@id/cup"
        app:layout_constraintTop_toBottomOf="@id/price" />

    <Button
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16sp"
        android:layout_marginTop="8sp"
        android:gravity="center"
        android:onClick="submitOrder"
        android:text="order"
        android:textSize="16sp"
        app:layout_constraintLeft_toRightOf="@id/cup"
        app:layout_constraintTop_toBottomOf="@id/order_summary_text_view" />
</android.support.constraint.ConstraintLayout>

Main Activity file:

package com.example.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {

    int quantity = 1;
    double pricePerCup = 2.90;
    String name = "Pawel";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void increment(View view) {
        quantity = quantity +1;
        displayQuantity(quantity);
    }
    public void decrement(View view) {
        quantity = quantity - 1;
        displayQuantity(quantity);
    }
    private String createOrderSummary(double price){
        String orderSummary = "Name: " + name + "\nQuantity: " + quantity + 
    "\nTotal price: $" + price + "\nThanks!";
        return orderSummary;
    }

    private double calculatePrice(int count, double price){
        return count*price;
    }

    /**
     * This method displays the given text on the screen.
     */
    private void displayMessage(String message) {
        TextView orderSummaryTextView = 
    findViewById(R.id.order_summary_text_view);
        orderSummaryTextView.setText(message);
    }
    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        double totalPrice = calculatePrice(quantity, pricePerCup);
        String priceMessage = createOrderSummary(totalPrice);
        displayMessage(priceMessage);
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void displayQuantity(int number) {
        TextView quantityTextView = findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }

}
Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
Pawel
  • 171
  • 1
  • 1
  • 7

8 Answers8

8

use this line MainActivity XML fle

tools:context="com.example.justjava.MainActivity"

You are not referring in XML whole src directory.

OR

Just remove this line from XML

tools:context=".folderName.MainActivity"
Tanveer Munir
  • 1,852
  • 1
  • 11
  • 26
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/187821/discussion-on-answer-by-tanveer-munir-unresolved-class-mainactivity). – Samuel Liew Feb 03 '19 at 23:28
4

1.Rebuild project, if not solved

2.Go to the File menu, then click on the Invalidate Caches/Restart option then Invalidate and Restart.

Your ide will be automatically restarted and the problem will be solved.

Etienne Kaiser
  • 2,634
  • 8
  • 19
  • 29
1

Replace this line of code:

<activity android:name="MainActivity"/>

with this:

<activity android:name="com.example.justjava.MainActivity"/>

1

In my experience this happens because mistakes in the code, often in naming classes.

I solved this issue just fixing the namespace in the MainActivity class, indeed I made a mistake just there because I copied and pasted the content from another project.

papesky
  • 637
  • 1
  • 8
  • 19
0

This problem has happened to me as well, I used a program-incompatible library ,Removing that library solved the problem

Hadi Abedi
  • 131
  • 1
  • 4
0

I found a solution for this problem in FLUTTER. For native user, you can try to do something similar.

Step 1 : Open this folder of your flutter project (It contains some file like app-debug, etc. As seen in image.). This is the folder where debug app stored with some of its additional files.

{Project Location}\build\app\outputs\flutter-apk

Example - Folder location of my test App.

C:\Users\DeLL\fp\fd\test\build\app\outputs\flutter-apk

Step 2 : Delete everything.

Step 3 : Re-Run App.

Note : Sometimes you may need to repeat this step 2 times. Be patience. Hope it may help you.

enter image description here

Pankaj Kumar
  • 113
  • 7
0

I've been trying to fix this problem for a while and I didn't find any resolves.

I still don't know why this problem occurs but doing this solved my problem.

1- Create a new package
2- Drag your activity into this package and run your application

oguzata
  • 23
  • 5
-1

Make your SDK version about higher like 16 or above you can get to

If you're SDK version is 14 in the gradle file(app module)