0

I started a new project. Following this question, I set app/src as "Sources Root". Then I made a new class CustomClass under app/src. I want to construct an instance of this class for the app to access and run, so I tried to do this in MainActivity (in /app/src/main/java/com.example.testapp):

import CustomClass;

public class MainActivity extends AppCompatActivity {
    CustomClass newObject = new CustomClass();
    ...
}

but the first line gives a "Cannot resolve symbol 'CustomClass' " error.

If I make another class in /app/src, it can import CustomClass without an issue. It seems like MainActivity doesn't know where to look for the new class. How can I tell it to import the new class?

Sam Jaques
  • 291
  • 3
  • 13
  • You should import the class by it's full package name. Try- import com.example.testapp.CustomClass. Also, Importing is not required at all if your MainActivity is in the same package as the CustomClass. – Quanta Jun 19 '20 at 15:01
  • Is there a way to find the package that a class belongs to? `import com.example.testapp.CustomClass` gives the same error. And calling the class directly without importing gives the same error. – Sam Jaques Jun 19 '20 at 17:33
  • Yes, every file in java declares what package it belongs to. The first line in the file reads: `package your.package.name;` – Quanta Jun 20 '20 at 12:51

0 Answers0