"R cannot be resolved to a variable " even after new Eclipse install - java

I have the famous error message “R cannot be resolved to a variable” since the last 3 days and I’m totally stuck.
I did follow the instructions in the following links but I can’t remove this error:
Developing for Android in Eclipse: R.java not regenerating
I have crated plenty of new projects with just a new title but the same.
I even today uninstall completely Eclipse , jdk, jre and reinstall all but the same…
**Config: Windows 7 64 bits
eclipse-standard-kepler-SR1-win32-x86_64
jdk-7u51-windows-x64
jre-7u51-windows-x64
Android sdk downloaded**
I have noticed there is no file in the folder “gen” is always empty..
Thank you for your help
![enter image description here][1]
![enter image description here][2]
![enter image description here][3]
![enter image description here][4]
Mainactivity.java:
package com.example.test1;
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;
}
}
activity_maim xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test1.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

This looks like a 64bit issue, as android is looking for a 32bit ADT, there are a few options to solve this. Have a look at this question: Android SDK Setup under Windows 7 Pro 64 bit
I solved this under linux by doing sudo apt-get install ia32-libs

Have you checked your manifest.xml for mistakes?
I once had a small error in my manifest that didn't show up as an error and it wouldn't generate a new R file.
I noticed another small thing that might be the issue, in your main_activity.xml you use dimension strings for padding, when creating a new project these strings are located in res/values/dimens.xml but in your layout file you are referring to #dimen instead of #dimens (notice the missing 's').
Try changing it to "#dimens/..."

Related

MediaRecord failed to start Android 7.1.1 emulator

Briefly, I cannot get MediaRecorder#record to work on Android 7.1.1 emulator and don't understand why. Here are the further details:
Environment:
Operating System: Mac OS X El Capitan version 10.11.6
IDE: AndroidStudio version 2.2.3
Problem:
I have written a sample app to demonstrate the issue. This app consists of a button with an OnClickListener which will initialize and configure MediaRecorder instance, prepare and start recording. After enabling the RECORD_AUDIO permission in settings, the code below runs correctly on the Android 7.0 API 24 Emulator (x86_64 Graphics: Hardware) downloaded by and used by the AVD manager,
but yields following error on Android 7.1.1 API 25 (with Google APIs) Emulator (x86_64 Graphics: Hardware):
Error only on Android 7.1.1 API 25
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
Question
Does anyone know what the issue is? Is there problem with my setup that does not cater for Android 7.1.1? This has been replicated on 2 Macs so far.
Code:
MainActivity.java:
package com.abstractx1.testrecord;
import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button recordButton = (Button) findViewById(R.id.recordButton);
recordButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String path = getFilesDir().getAbsolutePath() + "/" + "AudioRecording.3gp";
MediaRecorder mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(path);
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (Exception e) {
Log.e("DEBUG-MainActivity", "Error", e);
Log.e("DEBUG-MainActivity", Log.getStackTraceString(e));
}
Toast.makeText(MainActivity.this, "Recording started", Toast.LENGTH_LONG).show();
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.abstractx1.testrecord.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:text="Record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/recordButton" />
</RelativeLayout>
AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abstractx1.testrecord">
<uses-feature
android:name="android.hardware.microphone"
android:required="false" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you, and I apologize in advance if this has been answered elsewhere. I have searched for few days without success. Also, if there is ambiguity/lack of information then I will expand and answer questions.
This is such a basic thing that I believe it must be an issue with the emulator.
Can someone please help me with this?
I have encounter similar issue due to not using Environment so media player can not find the path to use to store the audio hence it works on some devices and not in others: https://github.com/alkathirikhalid/android-uni-foundation
I have also reviewed your code I have detected possible errors or at least recommendations
File location / path, you should use
Environment.getExternalStorageDirectory().getAbsolutePath()...
Let the OS give you the location as different devices have different naming conventions
Uses feature required is false, then you should have checking to see if devices have mic or not before calling Media Player
Uses Permission Write External Storage You are intending to store the recorded audio to store and perhaps use it / play later
Finally The emulator is not supported as described in Android documentation "Note: Currently, Media Recorder does not work on the emulator."
https://developer.android.com/reference/android/media/MediaRecorder.html
It is highly recommended to use actual devices when dealing with multimedia, locations and other device peripherals such as sensors.
Cheers.
MediaRecorder is not currently supported on the emulator. This is explicitly stated at the end of the introductory paragraph of the documentation. See the Android MediaRecorder documentation.

Simple map based app not working

I'm attempting to get a simple map based app running but when I install on device and try to "open" I receive the error "unfortunately application has stopped" On emulator I receive the message :
[2014-03-18 21:37:16 - FirstMap] Installation error: Unknown failure
[2014-03-18 21:37:16 - FirstMap] Please check logcat output for more details.
[2014-03-18 21:37:16 - FirstMap] Launch canceled!
Why do I receive this message ? I think my code looks good as I have followed these steps : https://developers.google.com/maps/documentation/android/v1/hello-mapview ?
Here is my code :
MainActivity.java :
package com.example.firstmap;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class MainActivity extends MapActivity {
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
}
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="mykey"
/>
AndroidManifect.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstmap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.firstmap.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Refer to my post here:
Want to use GoogleMaps - OnMyLocationChangeListener but can't implement it? Any other options
I have described what exactly to use and how.
Hope you find it helpful .. :)
There are many things missing from your manifest file. Try following the official instructions on how to use google maps google map instructions
The instructions you followed refer to version 1 google maps for android which is deprecated.
Use Google Map Api V2, The important changes that you will have to make is
1) Make use of
<fragment
android:id="#+id/map"
android:name="android.app.DialogFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
i.e You have to use fragment tag in activity_main.xml instead of the mapview tag.
2) Refer Google play services.....
example: Google Maps Can't add reference to Play Services
Best Link to refer for newbies implementing Google Maps Api V2: http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

Blank screen when attempting load url within app

I am new to eclipse/Java. I searched for answer but nothing solved my problem. I hope someone can guide me. I am attempting to launch a webpage withing an android application however I am getting a blank page in the Android Virtual Emulation.
Here is my code
package com.example.t4;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.com");
}
#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;
}
}
Here is the AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.t4"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.t4.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and here is the activity_main.xml
<?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">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
I am not sure what I am doing wrong. The code runs without an error. I just get a blank screen. Any help is much appreciated. Thanks in advance.
try to put myWebView.getSettings().setJavaScriptEnabled(true); // enable javascript
myWebView.loadUrl("http://www.google.com");
Check out this working basic webview. I tested with "www.google.com" like yours and came up with a blank view. I used "http://www.google.com" and it ran fine. Try that out on yours? But feel free to steal the sample code too.
Restart your app (I mean click Run again) and make sure you have enabled javaScript as mentioned above.

Displaying Images in Android using Java

I am splitting this off from my other thread here: Display image in popout window after button is clicked -- Android/Java
The old thread got very convoluted and confusing, and a bit off-topic, so I wanted to create another one that is clearer with more information.
I am trying to display an image in Android using an image file path that changes every time the app is run. I know there are ways to declare resources in the XML layout file, but since the picture I'd like to display is taken from the camera, I can't do it that way. I need to be able to display the image without hardcoding it.
The following is the code that I have:
photo_viewer.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pictureViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/viewImage" />
</RelativeLayout>
Java method to display image:
public void viewPhoto(String file){
ImageView imageView = new ImageView(getApplicationContext());
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Bitmap image = BitmapFactory.decodeFile(file);
imageView.setImageBitmap(image);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.pictureViewer);
rl.addView(imageView, lp);
}
However, when I run the above code the application crashes, saying it has stopped working. "file" is a string denoting the directory where the image from the camera is stored, and I know this part is correct. I can use it (using a different method) to change the wallpaper. It's just displaying it that is causing me trouble. I have tried various methods to simply display the image (dialogs, "drawable"s, bitmap factory, etc) and had the same problem with all of them. I feel like it's something simple that I have missed, since I'm new to Android app development. I'm hoping one of you folks might be able to shed some light on this.
EDIT:
I have decided to try another route, but I am still getting a null pointer exception. Here is all my code:
main class, viewPhoto method:
/* View photo */
public void viewPhoto(String file){
Intent imageView = new Intent(this, PhotoViewer.class);
imageView.putExtra("directory", file);
startActivity(imageView);
}
PhotoViewer class:
package com.androidproject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class PhotoViewer extends Activity {
public String file;
ImageView image;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.photo_viewer);
//get file path of image
Bundle extras = getIntent().getExtras();
file = extras.getString("directory");
Drawable drawable = Drawable.createFromPath(file);
image.setImageDrawable(drawable);
}
}
photo_viewer.xml (created using the Eclipse tool, so it's not just any xml file)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/photo_viewer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".PhotoViewer" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/viewImage"/>
</RelativeLayout>
Project manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PhotoViewer"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.androidproject.PhotoViewer" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
The app continues to crash and tell me "unfortunately it stopped working". Here's the logcat of the error:
As always, help is appreciated!
The null pointer exception is because
Drawable drawable = Drawable.createFromPath(file);
image.setImageDrawable(drawable);
is referencing an uninitiated variable (image) which is null
My best guess would be that to eliminate said error you'd want to initiate the variable
ImageView image = (ImageView)findViewById(R.id.imageView);

Google Map in Android 2.3.4 shows gray box on both AVD and real device

I'm trying to show a simple google map on my real Samsung device where Android version is 2.3.4.
got my SHA1 key
got my API key from google
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.neighbourhoodlocator"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBq8ATEI-DmBtso1T13S_DGfiT7cBgEav8" />
<activity
android:name="com.neighbourhoodlocator.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my main.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
Here is my Mainactivity.java file:
public class MainActivity extends FragmentActivity {
#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;
}
}
Now the issue I'm having when I launch it on the real device, it doesn't crash, nor it throws any exception (No "Invalid Authorization" either); only thing I can see on the screen is a blank (gray box) with "+" and "-" button. I've tried Activity instead of FragmentActivity, application crashed.
I checked the logcat trace:
10-12 23:14:34.989: W/dalvikvm(15055): VFY: unable to resolve instance field 28
10-12 23:14:35.119: W/dalvikvm(15055): Unable to resolve superclass of Lmaps/p/w; (743)
10-12 23:14:35.119: W/dalvikvm(15055): Link of class 'Lmaps/p/w;' failed
10-12 23:14:35.119: W/dalvikvm(15055): Unable to resolve superclass of Lmaps/ap/as; (6267)
10-12 23:14:35.119: W/dalvikvm(15055): Link of class 'Lmaps/ap/as;' failed
10-12 23:14:35.139: W/dalvikvm(15055): Unable to resolve superclass of Lmaps/af/k; (5294)
10-12 23:14:35.139: W/dalvikvm(15055): Link of class 'Lmaps/af/k;' failed
10-12 23:14:35.139: E/dalvikvm(15055): Could not find class 'maps.af.k', referenced from method maps.ag.an.a
10-12 23:14:35.139: W/dalvikvm(15055): VFY: unable to resolve new-instance 5139 (Lmaps/af/k;) in Lmaps/ag/an;
I added the google-play-services_lib appropriately. and also added the android-support-v4.jar is also in "Android private libraries".
Notice that the project build target is: Google APIs 4.2.2
and the android version in my Samsung phone is: 2.3.4
I've been trying last couple of days, I also tried in the emulator, same thing is happening.
Please help me to solve the issue.
Thanks.
I had a similar problem.. I have solved it. Android emulators aren't working. You should work your app on your phone. When exporting your project, your sha1 is changing. You must copy that sha1 and make a new API-KEY in google api console. (don't forget the change manifest file). After uploading your phone, it works..

Categories