title: Unresolved class MainActivity
body: <p>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 <code>tools:context=".MainActivity"</code>, MainActivity is highlighted red and it says "Unresolved class MainActivity". It happens even if I create a brand new "empty activity".</p>
  
  <p>So far I've tried: </p>
  
  <ul>
  <li>refreshing IDE cache</li>
  <li>checked package names in Android manifest and MainActivity</li>
  <li>selecting a default activity in-app configuration</li>
  <li>made sure that src is the source directory </li>
  </ul>
  
  <p>I've also noticed that in my "most advanced" app the <code>build.gradle</code> looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/sCGnb.png" alt="Screenshot"></p>
  
  <p>Android manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.justjava"&gt;
  
      &lt;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"&gt;
          &lt;activity android:name=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Project directory + code: </p>
  
  <p><img src="https://i.stack.imgur.com/vwXLi.png" alt="dir"></p>
  
  <p>activity main xml: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"&gt;
  
      &lt;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" /&gt;
  
      &lt;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" /&gt;
  
      &lt;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" /&gt;
  
      &lt;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"&gt;
  
          &lt;Button
              android:id="@+id/plus"
              android:layout_width="48dp"
              android:layout_height="48dp"
              android:layout_marginLeft="8dp"
              android:onClick="increment"
              android:text="+" /&gt;
  
          &lt;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" /&gt;
  
          &lt;Button
              android:id="@+id/miuns"
              android:layout_width="48dp"
              android:layout_height="48dp"
              android:layout_marginLeft="8dp"
              android:onClick="decrement"
              android:text="-" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;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" /&gt;
  
      &lt;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" /&gt;
  
      &lt;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" /&gt;
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>Main Activity file:</p>
  
  <pre><code>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);
      }
  
  }
  </code></pre>