Android studio and R class - android

I've a little problem with android studio. Until yesterday I used Eclipse. But today I start to use Android studio. I write a simple layout without errors. Than in src/main/java I write this simple code:
public class MyActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(android.R.layout.activity_my);
}
}
But I've an arror: "cannot resolve symbol activity_my"
According to me the layout isn't registrated in R.java. How can I resolve this problem? Thanks in advance :D
p.s. sorry for my bad english >.<

Use R and not android.R. Check that the imported R is your.package.name.R.

you should always use setContentView(R.layout.activity_my);.
so replace setContentView(android.R.layout.activity_my); with setContentView(R.layout.activity_my);
USE CTR + SHIFT + o to organize your import statements in eclipse.

Related

visual studio android import cant resolved after update

I updated Visual Studio, then my Android project had errors such as import could not be resolved. It could not be resolved to a text type. The type could not be resolved. Then I created a new project with the same settings. However, it had the same errors. It does not work because of the code, but maybe it knows who knows the code here.
package com.Android2;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class Android2 extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* Create a TextView and set its text to "Hello world" */
TextView tv = new TextView(this);
tv.setText("Hello World!");
setContentView(tv);
}
}
There is no problem with your code or imports...
I know it is hard work and a serious change when it comes to shortcuts, menus, overview, etc., but i would suggest to use android studio instead of visual studio if you only write android apps in java. I used visual studio too and switched to android studio.
Android Studio will give you more comfort than every other ide in programming android apps in java!
I know this problems you wrote about very well and had the same. Sry that this isnt a really answer of your problem and does not solves your concrete problem.
Tips for solving, you probably already know:
reboot your pc and visual studio if you not already did
delete caches or other from visual studio
rebuilt/clean your project
not easy, but reinstall visual studio
I hope i could help anyway!

Red lines in Android Studio with Ampiri

I have used Ampiri integration in my app.
I copy paste all the code from their android sdk integration wizard for banner ads into my code. What bothers me is that Android Studio can not find that listener and view in the red letter.
So what is that I miss? Is there any problems on my code? If not what should I do to resolve this issue? Please help me.
Following is my code:
I’ve implemented the AdEventCallback using:
import com.ampiri.sdk.listeners.AdEventCallback;
and call it from main activity inside onCreate as describe in my code below:
public abstract class MainActivity extends AdCallbackActivity implements AdEventCallback {...}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout adView = (FrameLayout)view.findViewById(R.id.ad_view);
StandardAd standardAd = new StandardAd(this, adView,“MY_ADUNIT_ID",BannerSize.BANNER_SIZE_320x50, adListener);
standardAd.loadAd();
Please check my comments inline for each error.
view.findViewById(R.id.ad_view)
If the ad_view is in the current Activity layout set by setContentView, you don't need to add "view."
i.e.
findViewById(R.id.ad_view)
adListener
Please implement an event listener interface AdEventCallback, referencing to our integration page.
In addition, If you will have the AdEventCallback methods in your AdCallbackActivity.java file.
please try the code below.
StandardAd standardAd = new StandardAd(
this, adView, "3dfbb889-3bcd-4c34-82ae-8fcb539c3b25",
BannerSize.BANNER_SIZE_320x50, this);
If it doesn't work, please send me your MainActivity and AdCallbackActivity to suppport#ampiri.com.

PhoneGap SplashScreen - splash cannot be resolved or is not a field

i want to make a splash screen on my android application, i use phonegap.
here is my main.java code :
public class App extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.splash);
super.loadUrl("file:///android_asset/www/index.html",5000);
}
}
but i got an error : "splash cannot be resolved or is not a field"
i've put the splash.png on every drawable directories, but the error still there..
anybody could help me? thanks in advance..
I think there is an issue about the timeout parameter in your case 5000 and it was fixed in phonegap 1.6.0 release that's why you don't get it work:
these links confirms that
How to use OpenStreetMap/OpenLayers?
phonegap - splash screen for Android app
try to use the last phone gap release (1.8.1) and you will get it work
just change the word splash by screen:
super.setIntegerProperty("splashscreen", R.drawable.screen);
I was having a similar problem. I renamed the package which extended the core DroidGap class. My solution was fixed by this solution: http://www.youtube.com/watch?v=3CHRujojc2o. In short, make sure to update your references in the AndroidManifest.xml file to point to the new package name.

Error in PhoneGap program "Web page not available"

package com.phonegap;
import org.apache.cordova.*;
import android.os.Bundle;
public class PhoneGapSampleActivity extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("assets/www/index.html");
}
}
I use this code for PhoneGap application..
But it shows an error.
I added index.html file in assets/www folder.
But it shows an error ...
Anyone can help me??
Thanks in advance.
You forgot the file:///. Keep in mind it's important to have the three slashes!
Also I think it should be android_asset and not asset.
In my app it is: "file:///android_asset/www/index.html"
Simple Google search came to this :
phonegap doesn't work
The solution was to rename phonegap.0.9.4.js and phonegap.0.9.4.jar to just phonegap.jar and phonegap.js.

Android/Eclipse Question - "id cannot be resolved or is not a field" error

Hello I started trying to develop Android applications today, but I have a problem:
I decided to try out making a Web View using this tutorial - http://developer.android.com/resources/tutorials/views/hello-webview.html
But when I put the code in for the OnCreate() method I get an "id connot be resolved or is not a field" error. Here is my code:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
HelloWebView.java:
package com.example.hellowebview;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class HelloWebView extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
}
I have tried cleaning, CTRL+SHIFT+O, and just completely restarting the project. There error is in the statement:
mWebView = (WebView) findViewById(R.id.webview);
and Eclipse just says "id cannot be resolved or is not a field"
Also, I installed everything today using the guide on developer.android.com and followed all steps correctly. I have made Java programs on this computer before so I don't think there is a problem related to the JDK/JRE.
I managed to fix the problem by simply restarting my computer. I guess in order for all the development tools to work properly a computer restart is necessary? I can't believe I didn't think about doing this before though. Thanks for those who tried to help.
There are a couple of similar questions here. Take a look at some answers:
import of android.R : this response: R cannot be resolved - Android error
java tools not building R.java: R cannot be resolved - Android error
problem with default.properties file: R cannot be resolved - Android error
and finally, something reported by several: make sure the file is main.xml and not Main.xml
Found the solution that finally worked for me after researching hours. Turns out the problem is not your R.java, All you gotta do is 'Clean' the project one last time. Save your project. And open Eclipse again. The problem is GONE!
Look into the R File Generated and see whether you see a Public Static Class called Id if so then see whether it contains a public static final int webview ( that you have created )
I also get that from time to time.
This usually (or always) does it for me:
Project - Clean - "your project"

Categories

Resources