-1

Can anyone help me fix this problem? Everything seems fine. My xml and java looks normal but I can't seem to find what is wrong with the error. I already tried invalidating my cache and cleaning the project but the error is still there. Is there anyway I can fix this problem?

My Problem: Cannot resolve symbol 'activity_main'

enter image description here

MainActivity

package com.example.machineproblem2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;

      public class MainActivity extends Activity {

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lbl_msg"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <EditText
        android:id="@+id/etMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/lbl_msg">
        <requestFocus/>
    </EditText>
    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_send"
        android:onClick="doNextActivity"/>

    <TextView
        android:id="@+id/tvContext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/msg"
        android:textAppearance="?android:attr/textAppearanceLarge"
        />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35

4 Answers4

4

The suggested fixes were not really working so what I did was.

  1. Copied the contents of activity_main

  2. Delete the activity_main

  3. Create a new layout of the same name

  4. Pasted the copied contents of the previous xml file to the new layout.

It is a mystery why it did not detect the previous activity_main but this fix seemed to work

3

1 - try to Clean then Rebuild project (Build > Clean Project / Rebuild Project)

2 - if (1) not work with you click : File > Invalidate Caches / Restart', and then click the 'Invalidate and Restart' button.

M. EL MOUSSAOUI
  • 451
  • 4
  • 4
1

add

import com.example.machineproblem2.R 

I don't see it in your imports, so Activity cannot resolve activity_main.xml because it doesn't understand what is R mean.

Another workaround:

try to rename your package from com.example.machineproblem2 to com.example.machineproblem. It is possible what you break the rule of individual package naming (see: Package names in Android studio when dev name starts with a number?)

Peter Staranchuk
  • 1,343
  • 3
  • 14
  • 29
0

Does R.layout have access to the other xml layouts? (activity_second & layout_toast)? That was at least you'll know if its an issue with that layout specifically or with the project in general. If the others work then consider copy-pasting the layout and see if you can reference the new one then.

EverCode
  • 66
  • 4
  • Nope. NO access. It only shows support_simple_spinner_dropdown_item and class – MiNameisJäger Aug 08 '19 at 10:11
  • 1
    I would create a new project and check if it works there, just to rule out it been a problem with the installation. Also, you could make a new java class and see if you can reference the layouts from there. Additionally you could try deleting all the imports at the top, I dont think most of them are used and maybe one of them is screwing your references for some strange reason. – EverCode Aug 08 '19 at 10:28
  • Well I am going to need to use them on my future codes. I usually lay down all my imports first when I start project – MiNameisJäger Aug 08 '19 at 10:34
  • I understand, I'm just trying eliminate code temporally to discard possibilities – EverCode Aug 08 '19 at 10:57