Unresolved class MainActivity
Solution 1
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"
Solution 2
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.
Solution 3
Replace this line of code:
<activity android:name="MainActivity"/>
with this:
<activity android:name="com.example.justjava.MainActivity"/>
Solution 4
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.
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
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: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:
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); } }
-
Samuel Liew about 5 yearsComments are not for extended discussion; this conversation has been moved to chat.