Android Studio not recognizing Java objects - java

For reference I am using Android Studio 3.1.4. My problem is that android studio isn't recognizing any of my java objects aka all of the objects names are underlined in red. Here is the code for my main activity:
package com.example.t00587599.unitconverter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AdapterView;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context: this, R.array.temptypes, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
}

I donot know much about the issues that you are facing as there can be multiple reasons why that is happening. There are two things that you could be happening
- if your package is compiled then you should check if the files are in the project. Or the project where the package is included is added in the build.gradle. I do this by adding
compile ":unitconverter"
- if you are including this as a compiled dependency then you can add the following line in your build.grade for the project
compile "com.example.t00587599.unitconverter"
After that you should do a gradle sync. If after doing all this you still get the red lines then you should do a "File/Sync Project with Gradle Files" from the file menu.
All of these class of problems disappear after these steps for me.
If it still appears then you should give us the error that come when you highlight it.

Related

Android studio library dependency not showing up

I went to file -->app--> dependencies --> green plus sign and press on library dependency. Nothing show up. I trying to add the support v4 dependency so I can extend Fragment Activity. Now I having error cannot resolve symbol support.
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.pager);
PagerAdapter padapter = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(padapter);
}
}
Open the build.gradle file for your application and add this line compile "com.android.support:support-v4:18.0.+"
Make sure you have downloaded the Android Support Repository using the SDK Manager.
ref here

Android/Java - Can't set ad size

I am trying to use AdMob in Android and am following tutorials. I have successfully downloaded, installed and added the Google Play Services library. Nearly everything seems fine.
However Eclipse stubbornly gives a red underline under the "adView.setAdSize" bit. The error message says "The method setAdSize(AdSize) in the type AdView is not applicable for the arguments (AdSize)". I don't see what's wrong, that code is in line with all tutorials and documentation I can find, and why would setAdSize(AdSize) not take in AdSize as an argument? That doesn't make sense to me.
Unfortunately setting the ad size is necessary for the code to run so I can't just remove that bit. Relevant code is below. Thanks to anyone that can help.
...
import com.google.ads.AdSize;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends ActionBarActivity{
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
// Create the adView.
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ID REMOVED FOR PRIVACY REASONS");
...
}
...
}
Change:
import com.google.ads.AdSize;
For:
import com.google.android.gms.ads.AdSize;

build.xml:1: Premature end of file

I am new in Android development, and I wanna build some apps for myself. I have installed Android SDK last version, NetBeans IDE 7.4 and also the plugin for this. I wrote a simple code for Android to see if it works:
package com.app;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("HelloWorld");
setContentView(tv);
}
}
When I try to build and run with an emulator, I get this error: [path] \AndroidApplication1\build.xml:1: Premature end of file.
When I go to the file (build.xml) I see that is empty. I should write to it? What should I do? Thanks in advance.

android app test error

When I run my first app, I got error.
Code of the activity:
package com.pradeep.ulc;
import android.app.Activity;
import android.os.Bundle;
public class New1Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Error:
R cannot be resolved to a variable New1Activity.java /new1/src/com/pradeep/ulc line 11 Java Problem
How can I fix it?
1) Make sure the package name is correctly set everywhere
2) Try doing a clean build to regenerate R.java.
If you must use a different package, import the R.java from the package specified in the manifest.
It means that you have problem in your resources, so R file cannot be generated.
What do you have in res/layout/main.xml file?

android "main cannot be resolved or is not a field"

i m new to this platform, please help me to find what is the error...
setContentView(R.layout.main); // this line shows the error.
Code:
package com.example.helloandroid;
import android.R;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Try deleting the line import android.R then clean your project.
Just adding more details on why the error is coming.
As there is an import of android.R so setContentView() is looking for a layout file 'android.R.layout.main' and there is no main.xml in the layout files that come along with SDK. So, using the correct R.java import will work.
clean Project then try To Run because i faced same problem before a month and remember layout/main.xml must be their and it must not contains any error.
you can Also Do this
import android.R;
or
import your.application.packagename.R; Now Clear Project and Run it.
You must have to simply change the
setContentView(R.layout.main);
... to:
setContentView(R.layout.activity_main);
... because Layout contains this .xml file.
I hope your problem will be solved.
First remove import android.R;
After any change on xml fiels you must clean project.
Build > Clean Project
after that every things corrects.

Categories