Android Studio MainActivity.java can't find resources - java

I'm basically writing my first hello world in android studio and the java file says that the xml layout file and other resources in the res file don't exist. I took the references to these things from books/tutorials so they should work.
The book said to use
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main.xml);
}
But when that didn't work I changed it to res.layout.activity_main.xml
my project directory looks like this
How do I make it work?
http://i.stack.imgur.com/N71sl.png
//EDIT
http://i.stack.imgur.com/HUgsm.png

You are referencing res not R.
do something like this: setContentView( R.layout.activity_main);
Leave off the ".xml"

update your android studio in stable channel and restart android studio and build the project again .

You need to understand the concept of the R class in android.
The R class is auto generated every time you build your project and cannot be modified.
This class contains a map of all the projects assets. Every time you created or import a resource, set a dimension, a string, create a layout, add or change id etc. the R file is changed in the background.
So if you want to access a resource you simply call it using the R class.
For example:
layout: R.layout.activity_main
string: R.string.hello_world
id: R.id.et_main_helloWorld
And so on.
More info can be found here.
Make sure you also check Providing Resources and Accessing Resources for a better understanding.
Good luck and happy coding.

In the end I started a new project and added in everything piece by piece, I had put a mp3 file in the values directory which was messing up the R file.

Related

Errors when trying to create Xamarin Android binding Java Library for mapsforge/vtm

I have tried to use the Java map library mapsforge-vtm to show an offline map using a mapsforge file with Xamarin Android.
Within Visual Studio 2019, I created one aar binding project as "LibraryProjectZip" for the file "vtm-android-0.13.0.aar" and one jar binding project as "EmbeddedJar" for the file "vtm-android-0.13.0.jar".
There is also a dependency to "vtm-0.13.0.jar" which you can see in the file "vtm-android-0.13.0.pom".
That "vtm-0.13.0.jar" file seems to cause the problems (e.g. the java interface Gesture which I include further down below).
If you keep following the dependencies (by looking in the pom files), there are also dependencies to "slf4j-api-1.7.28.jar" and "androidsvg-1.4.jar" but those two jars do not seem to cause problems, as far as I can tell.
Regarding the problem file "vtm-0.13.0.jar" I have tried to use it as an "EmbeddedReferenceJar" within both of the above mentioned projects (i.e. the aar binding and the jar binding projects for vtm-android).
Both of those two binding projects can build without errors (when also using "androidsvg-1.4.jar" and "slf4j-api-1.7.28.jar" as "EmbeddedReferenceJar"'s)
However, when trying to reference those projects from a Xamarin Android app, it does not work.
When trying to do that, I can not reference both the aar binding and the jar binding projects because then there is an error message about duplicated versions of for example the class "Org.Oscim.Android.MapView".
But when I use only one of them (i.e. either aar or jar project), then only the types from the main libraries are available (i.e. the "LibraryProjectZip" file for "vtm-android-0.13.0.aar" and "EmbeddedJar" file for "vtm-android-0.13.0.jar").
But I seem to NOT be able to reference any types from the "EmbeddedReferenceJar".
For example, there is a java type "org.oscim.tiling.source.mapfile.MapFileTileSource" in the "vtm-0.13.0.jar" but it does not seem to become available i.e. I can NOT use:
using Org.Oscim.Tiling.Source.Mapfile.MapFileTileSource;
In fact, the only namespace I can use, starting with "Org.Oscim" is "Org.Oscim.Android" e.g. this works:
using Org.Oscim.Android;
Then I tried to create a separate "EmbeddedJar" from the file "vtm-0.13.0.jar", since I was hoping that it then could be referenced from Visual Studio.
(and in that project I added "slf4j-api-1.7.28.jar" as "EmbeddedReferenceJar")
However, the compilation failed with 85 errors.
And I have tried all four combinations of settings for "Android Class Parser" and "Android Codegen Target"
(i.e. the four combinations of "class-parse + XAJavaInterop1" and "jar2xml + XAJavaInterop1" and "class-parse + XamarinAndroid" and "jar2xml + XamarinAndroid")
I have also tried with a couple of different target frameworks (Android 4.4 and Android 9 and Android 10).
50 of those 85 errors are this same error:
Error CS0234 The type or namespace name 'IGesture' does not exist in the namespace 'Org.Oscim.Event' (are you missing an assembly reference?)
vtm-jar ...\obj\Debug\generated\src\Org.Oscim.Event.IGesture.cs
Indeed, when looking in that file "IGesture.cs" there is no interface defined there.
I had expected to find something like "public partial interface IGesture ..." in that file, in a similar way as there is a "public partial interface IGestureListener ..." in the file "IGestureListener.cs" in the same directory.
I suspect that it has something to do with the fact that the java interface "Gesture" seems to be a bit unusal and thus not being nicely supported by Xamarin/Visual Studio.
This is what I am talking about:
Here is a "normal" kind of Java interface (GestureListener) which seems to work without errors in Visual Studio:
https://github.com/mapsforge/vtm/blob/master/vtm/src/org/oscim/event/GestureListener.java
package org.oscim.event;
public interface GestureListener {
boolean onGesture(Gesture g, MotionEvent e);
}
Here is the source code for the problematic interface (Gesture) in "vtm-0.13.0.jar":
https://github.com/mapsforge/vtm/blob/master/vtm/src/org/oscim/event/Gesture.java
package org.oscim.event;
public interface Gesture {
final class Press implements Gesture {
}
final class LongPress implements Gesture {
}
final class Tap implements Gesture {
}
final class DoubleTap implements Gesture {
}
final class TripleTap implements Gesture {
}
final class TwoFingerTap implements Gesture {
}
Gesture PRESS = new Press();
Gesture LONG_PRESS = new LongPress();
Gesture TAP = new Tap();
Gesture DOUBLE_TAP = new DoubleTap();
Gesture TRIPLE_TAP = new TripleTap();
Gesture TWO_FINGER_TAP = new TwoFingerTap();
}
As far as I can remember, I do not think I have ever seen that kind of Java code with classes being defined and instantiated within an interface.
And I suppose that Xamarin/Visual Studio also have a hard time to understand that kind of Java code.
So, is there anything you can do about this (and all other build errors, totally 85, with this vtm library)?
Has anyone been able to implement an Android App with Xamarin Android that uses mapsforge-vtm bindings, including also having been able to put all the pieces together and succeeded to display a map from a mapsforge map file?

Eclipse RCP 4.x - Defining workspace location

I know that this question was asked many times, but I didn't find an exact answer which would fulfill my desires :)
Long story short:
I've got simple E4 application, product project, feature and main plugin with simple trim window.
Works, after exporting works too.
Now. I add lifeCycleURI property, create bundleclass for it and create simple dialog with Text area and a Button. Run it\export it and it works, before running main Trim Window dialog is shown. Fine.. Cool etc.
But I want to enter location eg. C:\TEST and after clicking button I want it to be my workspace area for the application (with .metedata and so on). HOW ???
Of course I've tried with :
Location instanceLocation = Platform.getInstanceLocation();
instanceLocation.set(new URL("file", null, "C:\TEST"), false);
But... It says that I can't change location cause it is already set... Tried to use above in Activator. The same. Tried to add
-data #noDefault in products Launching Arguments ... The same...
I always try to accomplish my tasks by myself but this.... this... ehh... Help ?
You should be able to do this in the #PostContextCreate method of the life cycle class. Don't specify the '-data' argument
#PostContextCreate
public void postContextCreate()
{
Location instanceLoc = Platform.getInstanceLocation();
// Stop if location is set
if (instanceLoc.isSet())
return;
File file = new File("C:\\TEST");
instanceLocation.set(file.toURL(), false);
}
Note: You need '\\' in your file path.
This is adapted from code which I use in my e4 RCP.
If you are currently testing the application from within Eclipse you will need to clear the workspace location in the 'Run Configuration' for the application. Open 'Run > Run Configurations', find your application and clear the 'Location' field on the 'Main' tab.

Cannot replace android library project resources with android application resources

In my android library project, I have:
/res/drawable/redButton.png
And I reference this image file in my library like this:
public class MyLibExample{
.....
RadioButton r = new RadioButton(context);
r.setButtonDrawable(com.mylib.R.drawable.redButton);
}
Now, in my android application project, I have the same res:
/res/drawable/redButton.png
However, this redButton.png is a different graphics file than the one defined in the library.
I do not reference redButton.png in my application. Instead, I just create an object of type MyLibExample which was defined in the library.
I expected to see the redButton.png from the application, not from the library. However, the redButton.png from the library is always used.
How do I force the MyLibExample to use the redButton.png from the application?
The problem is that the id com.mylib.R.drawable.redButton is the id defned in mylib. You need the id defined in my app. So in your app code:
RadioButton r = (RadioButton)findViewById(com.mylib.R.id.radiobuttonId);
r.setButtonDrawable(com.myapp.R.drawable.redButton);

Localizing strings in strings.xml gives NullPointerException

My work computer that Eclipse is installed on does not have internet connectivity due to work related issues so all code and LogCat text has been hand typed instead of copy and pasted since I am on a separate laptop that Eclipse is installed right now. So bear with me for any typos.
Now to the issue. In the new version of my app, I am making it Spanish supported. I localized all my strings in strings.xml. Below is my Java code that I am not usuing to implement.
public class SplashScreen extends SwarmActivity {
Context c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
loading = (TextView)findViewById(R.id.loading);
//loading.setText(c.getResources().setString(R.string.loading)); //This way gives NPE
//loading.setText(R.string.loading); //This way works
//loading.setText("Test"); //This way works
}
}
If I understand localization correctly, I have to getResources() first so the app knows what language of the string to display. But the getResources() is what is messing me up.
What do I need to do to get the string displaying correctly?
To answer your problem, your forgot to initialize your Context object. So c is null. Replace
loading.setText(c.getResources().setString(R.string.loading));
by
loading.setText(getResources().setString(R.string.loading));
But actually there is no need to do that.
Android loads the appropriate resources according to the locale settings of the device at run time.
You just have to respect this hierarchy in your project :
res/
values/
strings.xml
values-es / (here for spanish values)
strings.xml
values-fr /
strings.xml (here for french values)
You have this code
Context c;
public void onCreate(Bundle savedInstanceState) {
...
loading.setText(c.getResources().setString(R.string.loading)); //This way gives NPE
The member c is never set before it is used. This is the reason for the NullPointerException. You must first initialize c with View.getContext() for example.
Localization is handled automatically according to the device's capabilities and settings.
In your layout definition, you can define the text string with a reference to a string id and Android will automatically load the appropriate resource
In res/layout/splashscreen.xml:
...
<TextView android:id="#+id/loading"
android:text="#string/loading"
.../>
...
So there is no need to explicitly set the text string in your code, because Android will do so already. The only thing you have to do, is to define the appropriate text strings in the res/values*/strings.xml files.

"setImageDrawable(Drawable.createFromPath)" get image from web server Android

I am using the the gridView example from the developer.android.com site. But they are using images saved in the res/drawable folder. I want to try and set the images to come from my web server.
private Integer[] mThumbIds={
for(int i=0;i<myJSONArray.lenght();i++){
Object[] myJSONArray;
setImageDrawable(Drawable.createFromPath(String "www.mywebsite.com: + myJSONArray[i]).thumb);
};
};
Am I on the correct path to accomplish this? I am looking for advice or documentations/tutorials on this process. Thanks in advance.
I modified the GreenDroid library to do it for me - it has a nice set of imageloading classes. It takes some slight modification if you don't want to use their actionbar (I didn't see an easy way to do it without modification). Everything is also async : D !
this way you cant create Drawable from internet, for that you have first download content & make it Drawable refer this

Categories

Resources