I am new with JavaScript and Developement on Android. I installed Eclipse but I can't even run the "Hello World" program, included when you created a new project !
Please help me, I spent the two last days installing this IDE (even tried Android Studio),
but there is always the same error :
Could not find Chwet.apk!
And there seems to be an error with the "R", like in
setContentView(R.layout.activity_main)
Here is my code :
package com.example.chwet;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Look at following tutorial to get start with Android application development.
Hello world example.
Instead of importing android R class , import R class with your package name.
Press Ctrl+shift+o to add missing packages. It will automatically remove your errors.
Related
I created a blank template and chose API 15 as minimum.
1- The activity_main.xml preview asked me to choose another theme. Why?
2- The MainActivity.java class is extending AppCompatActivity instead of Activity. I tried to delete it and typed Activity and imported the corresponding jar but then the IDE doesn't reconize R in R.layout.activity_main. The IDE says it can't resolve AppCompatActivityin import section of the code. Where is the problem?
package com.company.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thanks
P.S.
It seems the gradle was somehow unsuccessful in building the project. I'd used -Xmx256m in its settings.(What was the problem with this curb?) I removed the curb and it built the project. The weird thing is Android Studio recognized AppCompat stuff after building the project! (Shouldn't the IDE know all the classes used in code before compile?). Anyway, my problem is solved now but I don't know the reason
If you are still having that error then you can try going step by step via this blog : Hello World App on android studio
It would be easier if you uploaded your code
If the error is Cannot resolve R you just need to press ctrl+shift+o to organize imports and restart android studio
I created a Hello, World project in Android Studio to test using System.out.println(). I believe the log message should be printed in the console, but it wasn't. Im using Android Studio AI-130.737825 with JRE:1.7.0_25. The test code follows:
package com.example.consoletest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("please print me in the console");
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The console is not "connected" to the running app because it is run on a different system (be it an emulator, or physical device). The only "connected" part in Android Studio is the LogCat, which can be accessed using the Android tab at the bottom of the IDE.
You should rather print output to LogCat using the Log.* methods, which provides a lot more control and information, in almost the same simplistic way. Additionally, the logcat can be filtered to find exactly what you want.
As #free3om hinted at, Log.* can be used to print out many different outputs to the Logcat. If you want to see just errors, you can use Log.e(String s1, String s2) to see what and where something went wrong. Here's a link to the doc's for Log. http://developer.android.com/reference/android/util/Log.html
I am getting the error "R cannot be resolved to a variable" that many people seems to be getting. However, none of the answers that I have seen has worked for me. I've gone to the extreme basic and created a helloworld project, with all default settings. The "R cannot be resolved to a variable" error is created instantly, and I cannot see what is wrong... no xml errors, all lowercase in /res/ folder, no difference in AndroidManifest.xml.
For reference, the code on MainActivity.java is:
package com.test.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Is there something wrong with eclipse setting?
I'm using Windows 7 64-bit
Whenever I had a problem with R not been generated, or even disappeared, first clean and rebuild the project, if this is not worked then it was due to some problem in the XML layout file that prevented the application from being built. Check for the /res directory and there must be some file that have some error in it and that is preventing the application from being built. For example, it may be a layout file or it may be due to some missing resource is, but you already defined it in the XML file.
Ok, I know there are several questions like this, but I've been through all of them and nothing seems to work! I have been using eclipse for almost a year now. I installed new softwares from the Help menu, and since then nothing is working! I re-installed eclipse and now I'm getting this "R cannot be resolved to a variable", even when I create a brand new project. Here's the code:
package test.pro;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I am getting an error in the setContentView and getMenuInflater lines. I looked in the 'gen' folder, there's nothing there! I also checked the Java Build Path, it's fine. The ADT is updated. Can someone help me out here!!? I really need it!
Usually this error is because of some error in layout, strings, dimensions or any other xml file. Look for such xml error, resolve it and you should be good to go.
Also do perform a clean of your project. This error is very common with me too.
I am a beginner Android app developer. I created an XML layout called "splash" and I have changed setContentView(R.layout.activity_main); to setContentView(R.layout.splash); but got an error. I tried to do everything all over again but I got the same error in the R. When I did a "quick fix" a new error appears. It was caused by activity_main and splash.
When I started all over again, I just changed the background in activity_main but but I got the same error although I haven't modified anything in MainActivity.java.
Here is my code so far:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.**activity_main**);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.**menu**.main, menu);
return true;
}
You need to be careful when importing R because the Android OS provides one with system defaults etc.
Have a look in your imports to make sure you're getting the R from your package.
You might also see issues if your layout doesn't compile.