For some reason, the "id" in the argument is underlined in red and is "not resolved and or not a field"
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.widget.Button;
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button toggleButton = (Button)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}
}
Check your gen[Generated Java Files] folder in package explorer.
It may be empty, try
Project --> Clean
This will Generate files
Hope This Helps
It is one of the problems everyone faces with EclipseIDE.
Clean your project and build it once again.
P.S. : Also make sure that the id of the Button in your mail.xml is android:id="#+id/toggleButton"
It is a problem faced in eclipse IDE. I had this problem
When there was an error in one of my xml files
When the package definition in my manifest file has changed
When my generated file is in another package.
Check if any of the above is your problem. May be it will help
Cleaning and building the project might also help you
Just Click on Build Button (Green Hammer).
//Done
Related
I have tried to clean up, rebuild and debug my code. I am making a simple quiz (GEO Quiz). I have a menu folder in my resources but there is nothing in it, but I still can get the error to go away. This is my first app.
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class QuizActivity extends Activity {
private Button tButton;
private Button fButton;
//private Question[] questions= new Question[5];
//private int qNum =0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
My layout and string.xml are correct thus far. Whenever i type in R. into the onCreateOptionsMenu class. There is no list or prompt that says menu on it.
You should have menu.xml in menu folder
if not, add the menu.xml file inside /res/menu/ folder, this is an example:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="75"
android:title="my menu Item!"
app:showAsAction="never"/></menu>
Your menu resource folder does not have any xml file. the menu file should be on the menu resource folder.
The only solution that worked for my mac was to install MAMP. XAMPP did not work and locating files was confusing.
When entering commands in terminal all files had protection and contraints I could not do anything.
So MAMP is the best bet to solve this question. Make sure to use the php file in CLI interpeter.
Hi I'm doing an android gps tutorial and I'm getting an error I don't understand (I'm pretty new to android dev). Im getting the error at
position = (WebView) findViewById(R.id.activity_main);
where activity_main seems to be the problem. Could somebody explain what the problem is? thanks.
import android.R;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;
public class GPSActivity extends Activity
{
WebView position;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
position = (WebView) findViewById(R.id.activity_main);
position.getSettings().setJavaScriptEnabled(true);
}
public void onLocationChanged(Location loc)
{
String Text ="My current location is: " + "Latitud="
+ loc.getLatitude() + "Longitud =" + loc.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_LONG).show();
String url ="http://maps.google.com/staticmap?center="
+ loc.getLatitude() +loc.getLatitude()
+ "&zoom=14&size=512x512&maptype=mobile/&markers="
+ loc.getLatitude() + loc.getLongitude();
position.loadUrl(url);
}
}
the error is activity main cannot be resolved or is not a field, I think the id is in activity_main but correct me if I'm wrong, here it is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Webview
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Change it to
position = (WebView) findViewById(R.id.webview);
Also make sure you'e setting the correct layout
setContentView(R.layout.activity_main);
The reason is that the id of the WebView is not R.id.activity_main. In your case it is R.id.webview. The ID values are set in your XML layout via android:id="#+id/webview
Another possible issue. Remove import android.R; as well.
This is the reason it can't find your resources:
import android.R;
It expecting to find your R related stuff in the android.R that is precompiled. You need to remove that line and import your package's R not the precompiled R.
Solution
Remove the import android.R line.
Also ensure your project compiles and can generate the R.java during the build process.
position = (WebView) findViewById(R.id.webview); after you remove the import android.R.
It is looking for webview in android.R.id, but it is located in your_package_name.R.id.
Remove the
import android.R;
You will probabl still get errors that it is not recognized. This is because you are in a default package instead of your project package. You can get around it by importing your .R
So if you package name is com.GPS
then import com.GPS.R
View v=inflater.inflate(R.layout.fragment_two, container, false);
WebView mWebView = (WebView) v.findViewById(R.id.webView2);
I did that :)
I was able
I've been having the same issue with findViewByID(R.id.activity_main);, and I spent days trying out all the solutions I saw online.
I solved the problem using the following method:
I went to the XML document and gave my layout a name (ID): activity_main.
This line of code popped up in the TextView of my XML document: android:id="#+id/activity_main".
Immediately, I stopped having the activity_main problem.
Note: I also do not have the import.android.R statement.
Im new as a developer of Android apps. I find a problem. that is R.layout.main cannot be resolved. How can I solve my problem. my code is here. please solve my problem.
package com.android;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
Button bt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button)findViewById(R.id.ButtonOk);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast.makeText(getBaseContext(), "Welcome Android World", 3000).show();
Intent intent = new Intent(HelloActivity.this, DisplayActivity.class);
startActivity(intent);
}
});
}
}
*
and my xml code is here:
*
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ok" android:id="#+id/ButtonOk" android:height="50dp" android:width="100dp"></Button>
</LinearLayout>
Step 1 : Remove import android.R;
Step 2 : Clean And Rebuild (It should work)
If not
Then close the project exits eclipse and open again. Follow the steps it should work.
If not
Change your package
From
import android.R;
To
import yourpackage.R;
It Should work
Remove the line import android.R from the header.
and do a Ctrl + Shift + O
If android.R appears again, then manually write
import <yourpackagename>.R
Looks like you used the package name as com.android.R. Ideally speaking, avoid using package names like com.android. Try to maintain it like
com.companyname.appname
So you get into that habit, and don't have the pain of changing the package name in all the folders once you are about to publish.
Import com.android.R not android.R because your package name is com.android
Try to Clean and Rebuild your application and this should solve the issue.
my friend,
check that whether name of your xml file is "main" because we always write setContentView(R.layout.your_xml_file_name);
If you find my solution worthy then please like it
I also encountered this problem on IntelliJ IDEA and this is how I was able to solve the problem, but first I noticed the following:
this problem did not hinder the execution of the application
the resource file (R.java) located in the out/production/my/package/name directory is automatically updated, but the resource file (R.java) located in the gen/my.package.name directory is not updated automatically
IntelliJ IDEA uses the non-updated R.java for intellisence
So, I copied the layout class that was generated in R.java file in out/production/my/package/name directory, as below
public static final class layout {
public static final int main = 2130903040;
public layout() {
}
}
and I pasted this into the R.java file in gen/my.package.name directory
NOTE:
Please, and please, don't modify the auto-generated resource file R.java file in out/production/my/package/name
I've got a Question. I have a ListActivity in which i can't access over R, my own layout that i wrote for it, what am I doing wrong.
So here's the Layout Code (list_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/nofilter"/>
</LinearLayout>
Now i can't bind that Layout to my ListActivity over setContentView(R.layout.list_layout);
Here's some more Code about the ListActivity:
package de.retowaelchli.filterit.stats;
import java.util.ArrayList;
import java.util.List;
import de.retowaelchli.filterit.database.ADFilterDBAdapter;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
public class CreatedADFilters extends ListActivity {
//Variablen deklaration
private ADFilterDBAdapter mDbHelper;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.list_layout);
}
}
Wham am I doing wrong?
Best Regards
safari
NOTES:
I tried to clean my Project, it didnt work.
Make sure that the import of the R class is from your application and not com.android.R.
Sometimes when you use Eclipse's auto import it will import the wrong one...
use layout file like :
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
This is a ListActivity example.
try to use LayoutInflater.
LayoutInflater mInflater;
mInflater.inflate(R.layout.custom_row_view, null);
Might be worth cleaning your project. If you're using eclipse, go to: Project > Clean...
You must have one ListView in your project Layout file. And also see for delete android.R if it is auto imported there.If you are not using listview then y r u exetnding ListActivty.Extend Activity only.
I have defined a layout in an xml file in the 'res' folder of my android project. The 'EditText' element looks like:
<EditText android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numeric="integer|decimal"></EditText>
In my class file in my android project, I have the following:
public void doCalculation(View view) {
String firstNo = ((EditText) findViewById(R.id.editText1)).getText().toString();
String secondNo = ((EditText) findViewById(R.id.editText2)).getText().toString();
String operator = ((Spinner) findViewById(R.id.spinner1)).getSelectedItem().toString();
callWebService(firstNo, secondNo, operator );
}
Unfortunately, the first 2 assignments in my method above are showing an error in eclipse stating
EditText cannot be resolved to a type
I've no idea how to fix this. I'm using android 2.3.3 API 10.
Any help would be appreciated. Thanks
You need to import the EditText class, so it's known, using the following line at the beginning of your .java file :
import android.widget.EditText;
Note that, in most cases, Eclipse can help you a lot : it has an Organize Imports feature, that will add the required import lines :
Menu > Source > Organize Imports
Or use Ctrl + Shift + O
Did you try adding this manually?
import android.widget.EditText;
Also check your console & error log for additional errors. Usually with things this obvious the reason can be something else too.
If the import doesnt work, try closing and reopening your project.
If you tried to ad import android.widget.EditText, and doesn't worked try to clean your project at Project -> Clean... and Try to click with right mouse button on your project choose Android tools then fix project properties. Hope it helps.
If none of the other answers work, you can always do this:
EditText txt1 = (EditText)findViewById(R.id.editText1);
EditText txt2 = (EditText)findViewById(R.id.editText2);
String firstNo = txt1.getText().toString();
String secondNo = txt2.getText().toString();
Check for imports.
To get the text from EditText try get the TextView value. It might work.
String firstNo = ((TextView) findViewById(R.id.editText1)).getText().toString();
String secondNo = ((TextView) findViewById(R.id.editText2)).getText().toString();
Above your class, just import:
import android.widget.EditText;