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.
Related
I'm writing a simple Android App using AIDE (Android IDE). I gave one of my layout elements an ID, but when I try to access the element using findViewById(), I get an error tht says: "Unknown member 'id' of 'com.mycompany.mailscomunes.R'. I haven't seen this error outside of AIDE.
This is the Java code:
package com.mycompany.mailscomunes;
import android.app.*;
import android.os.*;
import android.content.Intent;
import android.provider.ContactsContract;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.one);
}
}
And this is the relevant XML:
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/one"/>
Remove these lines:
import android.app.*;
import android.os.*;
You're literally importing the entire Android framework, which includes layout files, which has ID's in them.
And you're not including your own ID file (called "R.java").
So remove those two lines, and include this one:
import com.mycompany.mailscomunes.R;
The root of an activity xml should be of Layout type.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Text"/>
</LinearLayout>
Goto your project and delete the build folder and do a rebuild. I had the same problem and is fixed
This is the start of a simple app to initially display some TextViews and Buttons.
I want to avoid using layout managers by getting the dimensions of the main window/layout and using that to hard-code the components any way I want.
But my main Activity can't get non-zero data about the main window/layout.
I don't know if it's supposed to be this way or if I'm doing something dumb, and would appreciate some help.
This is a command line project using Ant under win10 and outputting to a USB'd phone.
I've updated the SDK Manager to Android 6.0 (API 23) with Android Tools 24.4.1 and installed v23.2 of the Android Support Lib, although Ant seems to think differently as you can see in my previous message which has the Ant output:
-build-setup:
[getbuildtools] Using latest Build Tools: 21.1.2.
I don't know if that makes a difference, but not in this case I think.
The next two files are complete as far as they go.
I would have included more but the way the website mangles my code doesn't encourage uploading to it.
It might have helped if I could have comprehended the 'How to Format' but (A) I didn't even see it before my first post and (B) I don't understand it now.
There are far too many instructions and too much jargon; e.g. what is a backtick?
Can't there be a simple format that will take everything you throw at it?
main.xml
========
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
package="com.Chatterton.Peter.ChessClock"
android:id="#+id/main_window"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:id="#+id/time_text_view_left"
android:text="top_left"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:layout_alignParentBottom="true"
...
There are a total of 4 components, one per corner, with the same layout_width and height.
ChessClockActivity.java
=======================
package com.Chatterton.Peter.ChessClock;
import android.app.Activity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.graphics.Color;
import android.widget.TextView;
import android.util.Log;
public class ChessClockActivity extends Activity
{
public int winOriginX=10, winOriginY=20, winWidth=44, winHeight=66;
#Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout mainLayout = (RelativeLayout)findViewById( R.id.main_window );
int mainWidth = mainLayout.getWidth();
int mainHeight = mainLayout.getHeight();
Log.d( "clox", "mainLayout=" + mainLayout );
Log.d( "clox", "main W/H=" + mainWidth + "/" + mainWidth );
...
Screen Results:
It shows the last component displayed -- full-size -- with its text.
logcat results:
===============
D:\Android\ChessClock>adb shell
shell#android:/ $ logcat clox:D *:S
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
D/clox (27214): mainLayout=android.widget.RelativeLayout#41755b80
D/clox (27214): main W/H=0/0
**
They have no size at this point of the initialization process.
Click here to an answer that will let you access the sizes of your components after they have been displayed.
Or perhaps you can get the window size and figure the placement of your components by that value......
Answer here
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
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
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;