Import R (android) - android

I have a problem with my code which refuses to go away. This is the first half of my code:
public class SampleGame extends AndroidGame {
public static String map;
boolean firstTimeCreate = true;
#Override
public Screen getInitScreen() {
if (firstTimeCreate) {
Assets.load(this);
firstTimeCreate = false;
}
InputStream is = getResources().openRawResource(R.raw.map1);
map = convertStreamToString(is);
return new SplashLoadingScreen(this);
}
An error message is generated on the line
InputStream is = getResources().openRawResource(R.raw.map1)
The error message says I should add the line "import android.R", but when I do this, a second error message is generated which says "map1 cannot be resolved or is not a field." Can someone please give me any suggestions on how to solve this intractable problem?

Be sure that there is map1 in Raw folder.If it exists,then try clean/build your project or close and reopen eclipse.It may be solved.

Try updating your Eclipse android ADT to the latest, restart it.
Im not very good at android but if yours does have an onCreate some where make sure u reference your Resources after it has done the onCreate stuff.

Related

Log.e not working even after android.util.Log is imported

Am a beginner I dont understand, Why its not working even i imported android.util.Log. I am using android studio.
package com.sai.myapplication;
import android.util.Log; // showing full gray
public class Fan {
protected String fanName;
protected boolean isOn;
public String getFanName() {
return this.fanName;
}
public void BreakFan(){
log.e(getFanName(),"BANg, CRASH!......OOPPs");// "log" is showing in red
}
}
Log 'L' show be Caps. Please check it. You are using wrong one.
Log.e(getFanName(),"BANg, CRASH!......OOPPs");// "log" is showing in red
Its Log not log(with capital L). If I am not wrong log was giving you compile time error. So please use Log as below.
Log.e(getFanName(),"BANg, CRASH!......OOPPs");

Android Studio Imports not working

So today I was working on my App, everything was working okay until all the sudden Universal Image Loader and MultiDex stopped working, all my other imports were ok but just these two, I try removing the imports and re-imported them they could not work, here is my Application class that is suffering from the import issue:
public class App extends Application {
private static App instance;
public static Context applicationContext=null;
public static volatile Handler applicationHandler = null;
public static Point displaySize = new Point();
public static float density = 1;
public Bitmap cropped = null;
public Uri imgLocation = null;
public String imgePath = null;
#Override public void onCreate() {
super.onCreate();
initImageLoader();
instance = this;
mInstance = this;
applicationContext = getApplicationContext();
applicationHandler = new Handler(applicationContext.getMainLooper());
checkDisplaySize();
density = App.applicationContext.getResources().getDisplayMetrics().density;
DisplayImageOptions defaultDisplayImageOptions = new DisplayImageOptions.Builder() //
.considerExifParams(true)
.resetViewBeforeLoading(true)
.showImageOnLoading(R.drawable.nophotos)
.showImageOnFail(R.drawable.nophotos)
.delayBeforeLoading(0)
.build(); //
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.defaultDisplayImageOptions(defaultDisplayImageOptions)
.memoryCacheExtraOptions(480, 800).threadPoolSize(5).build();
ImageLoader.getInstance().init(config);
}
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(base);
}
And my Gradle File:
dependencies {
compile project(':ImageViewTouch')
compile project(':Gpu-Image')
compile project(':fastjson-1.2.5')
compile project(':android-support-multidex')
compile project(':universal-image-loader-1.9.4')
}
This answer is based off of the fact that you said:
Everything was working okay until all the sudden
Okay, so if everything was working before, it is not your fault, but android-studios fault.
Here are some things that you can try to do to fix it:
Clean project
Rebuild Project
Restart IDE
Update SDK tools
This is probably not an issue with your code, so you don't have to worry about it much. If cleaning and rebuilding doesn't work, just wait for some time.
Give android studio a while to set up, and this could take a few minutes. Often times, it fixes itself without even having to do any of the steps I listed above.
IMPORTANT:
ONLY update your SDK or mess with files AS THE LAST STEP. If you do that, I HIGHLY suggest you take a backup first.
Many times, this error appears when there is an error on the file to be imported. So open the file to be imported and correct any errors in there first.
If the error still persists, right click on the project and select the option 'Analyse' > 'Code Cleanup'
also,
make sure your package directory structure does not have any uppercase letters

logcat doesn't display logs from library project in my special case

I have three Android projects: BaseLibProject, MainLibProject, AppProject.
The relationship of these three Android projects is that:
The BaseLibProject is a library project used by MainLibProject.
The MainLibProject uses BaseLibProject as library project, and generates MainLib.jar(use maven build)
The generated MainLib.jar is added to AppProject's libs/ folder & also added to the build path of AppProject.
NEXT:
A simple class in BaseLibProject :
public class BaseLibClass {
public static String doBaseTask() {
Log.i("MyLog", "doBaseTask..."); //I can't see this log
return "Result from Base!";
}
}
A simple class in MainLibProject which defined a function invokes the function in BaseLibProject:
public class MainLibClass {
public static void doMainTask() {
Log.i("MyLog", "doMainTask..."); //I can see this log in logcat
String result = BaseLibClass.doBaseTask();
Log.i("MyLog", "result = " + result); //I can see the result log
}
}
Finally, in my AppProject I simply call above function of MainLibProject (remember I have MainLib.jar):
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MainLibClass.doMainTask();
}
}
I started logcat with command adb logcat -s MyLog:* . When I run my AppProject application, logcat displays
I/MyLog(2039): doMainTask...
I/MyLog(2039): result = Result from Base!
According to above log, the code in BaseLibProject is running (because I got result from BaseLibClass), but it doesn't display any logs from BaseLibProject, why???
(I have googled on internect, someone got similar issue fixed by restarting eclipse, but in my case, it doesn't help. Besides, I am checking logs from terminal NOT from eclipse.)
Ok, finally, I fixed the problem.
The reason is that in BaseLibProject pom.xml, I defined <packaging>apk</packaging>, while in MainLibProject pom.xml, when I define the dependency of BaseLibProject, I didn't specify the <type> of the artifact.
After I changed BaseLibProject's pom.xml to <packagin>apklib</packaging> and in MainLibProject's pom.xml specified the dependency of BaseLibProject with <type>apklib</type> . I am able to see the logs in BaseLibProject.

Android error: NoClassDefFound error while using android-json-rpc

I am using android-json-rpc library. I added the android-json-rpc-0.3.4.jar library to my build path. Just following some basic tutorial.
Here is my code:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.text_view);
tv.setText(testMethod());
}
private String testMethod() {
// Crashes here on this line
JSONRPCClient client = JSONRPCClient.create("10.1.2.3/json", JSONRPCParams.Versions.VERSION_2);
String string = "";
try {
string = client.callString("cf.test");
} catch (JSONRPCException e) {
Log.i("JSON-RPC Client", e.toString());
}
return string;
}
}
Error:
*AndroidRuntime(1528): java.lang.NoClassDefFoundError: org.alexd.jsonrpc.JSONRPCParams$Versions*
While going around this error, I found out that some guy has a blogpost about the same issue on a Mac Lion (same as mine) but worked fine on Ubuntu. http://www.1771.in/android-jsonrpc-not-working-on-mac.html
Could anyone help me with a workaround for this issue?
Thanks,
Dexter
There are 2 options I have in mind. 1. Have "libs" folder in root of the project, copy lib there, no need to add to the build path. This is a new requirement from android team. 2. Add import of used class.
Just got it compiled and working.

Android, string resource not found

I'm working on an App, and I am getting weired errors. I put in some String resource in res/values/strings and saved it. Now If I want to access it in an Activity I get an error like this
07-13 11:16:20.050: ERROR/AndroidRuntime(5883): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f060006
And I can't figure out why.
Resource looks like this:
<string name="dialog_download_text">please wait while downloading</string>
and I want to Access it like
public void onCreate(Bundle b){
super.onCreate(b);
String s = getResources().getString(R.string.dialog_download_text);
}
I also looked into R.java and found my Entry
public static final class string {
public static final int dialog_download_cancel=0x7f060005;
public static final int dialog_download_text=0x7f060007;
public static final int dialog_download_title=0x7f060006;
}
I don't know what to do, because I never had a problem like this before.
Please help me. Thanks all.
Sometimes I get this error (once every 2 days), and it's because eclipse is not compiling the new code and it's deploying the old apk.
I fix this by
doing a clean on eclipse (Project -> clean)
closing the emulator
restarting the adb server by running adb kill-server and adb start-server from the command line
Most likely there's an extra import android.R; Then your string can't be found there. Otherwise, clean the project and rebuild it, try again, then report back.
what i had was
import android.R;
instead of
import com.example.project.R;
android.R does not have your resources it only has the general android resources.
Checkout this thread, it fixed my problem.
R cannot be resolved - Android error
Clean the project in eclipse, and try running.If this doesn't resolve your issue, from a already running emulator uninstall the app, and run it again from eclispe.
I am currently writing the "net.superlinux.tcltktutorials" you can find it on play store now.
now in the application, I want to use the standard way of using locales instead of my way of doing locales. I had to add for Arabic /res/vlaues-ar and I have the default for English /res/values.
The app is about ad based YouTube Playlist TCL/Tk programming language tutorials. now the playlist can be in Arabic and in English. What I noticed is that if you have in the default /res/values 36 entries and in /res/values-ar 35 entries for the same playlist, this will make the ResourceNotFound exception. All you have to do is add the missing entry as empty at the bottom of your list just to make it equal in numbers in English and Arabic, Even if the English playlist is less in numbers.
This was my method of adding to the playlist formed inside the list activity, and also a clever way of using the resources as xml built in data:
package net.superlinux.tcltktutorials;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListOfVideos extends ListActivity {
List<String> model = new ArrayList<String>();
ArrayAdapter<String> adapter = null;
List<String> filename = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
adapter = new ArrayAdapter<String> (this, R.layout.list_item, model);
load_playlist();
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String selected_youtube_video=filename.get(position);
try {Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+selected_youtube_video));
startActivity(i);
}
catch(Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v="+selected_youtube_video+"&loop=1&autoplay=1")));
e.printStackTrace();
}
}
void load_playlist()
{
int display_id=0;
int file_id=0;
//loop forever until nothing has to be added to the ListView or stop if the list item
// to be added does not exist.
for (int i=0;;i++){
display_id=getResources().getIdentifier("display_"+i, "string", getPackageName());
if (display_id!=0 && getString(display_id).length()!=0)
adapter.add(getString(display_id));
else {
Log.e("string id not found or empty","R.string.display_"+i );
return;
}
file_id=getResources().getIdentifier("file_"+i, "string", getPackageName());
if (file_id!=0 && getString(file_id).length()!=0){
filename.add(getString(file_id));
}
else {
Log.e("string id not found or empty","R.string.file_"+i );
return;
}
}
}
}
I also met this problem and it is very wired at first.
1: I make sure I import the correct R into my class
2: I checked R.java in the gen folder and the resource id is there.
3: I cleaned my project and remove from my phone and relaunch it
the exception still there.
Finally I found the reason:
I just added my resource string into the strings.xml in language-specific folder(say values-zh) but missed it in the default strings.xml in the default values folder.
then it is solved.
There are two cases you may get this error
1.your strings.xml file is not referred correctly?
Ans:give the correct package name for R.java file as per you application?
2.if you kept correct string.xlm path you may get still error?
Ans:once clean and build the projector restart the eclipse or android studio.
Thanks
krishh
For Kotlin use
this->context.resources.getString(R.string.your_string)

Categories

Resources