0

I'm building a simple Android application from scratch where I'm trying to show a grid view on my landing page. For that I imported a package in my MainActivity.java file as shown below:

import android.widget; 

When I perform Build -> Make project action I get following error:

Error:(15, 15) error: cannot find symbol class widget

Not sure why compiler is treating android.widget as a class name when it is effectively a namespace/package. I'm from a C# background completely new to java and android. I searched a bit and I get that I should be adding a reference to a jar file which is equivalent to adding a reference to a .Net assembly in C#. I'm not sure about which jar file I'm missing and how to add its reference in Android Studio. Kindly help.

7
  • because you write it like that. shouldn't there be a class name at the end or at least import android.widget.*;? Commented Jan 17, 2017 at 3:08
  • As you said c# , are you trying Xamarin or Android Studio? Commented Jan 17, 2017 at 3:10
  • I'm using android studio. Commented Jan 17, 2017 at 3:14
  • what do you expect import android.widget; to do? Commented Jan 17, 2017 at 3:57
  • @njzk2 I expect that import android.widget; statement should import all the classes present inside import android.widget package. I wanted to use GridView class (which is found inside android.widget package) in my MainActivity.java file. If I don't use the import statement then possibly I'll have to use fully qualified type names which will look verbose. Commented Jan 17, 2017 at 4:39

2 Answers 2

2

Welcome to Android, RBT. I think you should change that import to

import android.widget.*;

Or you can import it later when you use it.

Hope this help!

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your support brother. your answer was equally correct. It is just that Dbzmania had answered the question 6 minutes ahead of you so I accepted his. Thanks for the help again.
ohh my bad. answered x minutes ago essentially means - higher the number x means more older the answer is. I misinterpreted it otherwise in a rush :P. Still I upvoted your post as a courtesy. The accepted answer is more descriptive. Thanks again.
@RBT you was wrong man. Dbzmania 14mins ago while me 20mins ago. LOL
That's what I said - I misinterpreted it in a rush.
1

Welcome to Java !! I am actually going now the other way.

Anyways, importing packages in Java (or namespaces in c#) works more or less the same way. The way you imported the package , makes Java think that there is a class (Type) at the end.

So, if you want to import the whole widget package, just change it to

import android.widget.*; 

Or if you interested to import a more specific Class (which is mostly the case), then import some class like this -

import android.widget.AbsListView; 

Please go through - Java Type import

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.