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.
Related
BaseActivity.java
Language switching
Intent
OnClickListener
Error demo
No error message was prompted.
You are using old style of language switching which were deprecated and removed after releasing android 8, It completely get removed. However, if you want to use your application for android prior to 26, you have to change your code on language switching Image as follow:
Configuration config = new Configuration(resources.getConfiguration());
New instance of Configuration have to be passed to resources.updateConfiguration();. above line will solve your problem.
On the other hand if you want to publish your application for higher api level, follow link below:
#Override
protected void attachBaseContext(Context newBase){
Configuration configuration = new Configuration(newBase.getResoureces().getConfiguration());
configuration.setLocale(/* selected locale which you have to get from user or your app configuration*/);
super.attachBaseContext(newBase.createConfigurationContext(configuration));
}
Override above method for each activity which you have declared;
This might be a very naive question but I can't get this java.lang.Exception error to go away. I also just started learning java and android so... this might be an easy problem to solve.
So I have a main activity in android and I want to implement a weka ml classifier within the app. Right now I'm just trying to load some data.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ml_classifiers ml_object = new ml_classifiers();
int number = ml_object.loadData();
Trying to load data...
public class ml_classifiers {
public int loadData() {
ConverterUtils.DataSource source = new ConverterUtils.DataSource("C:/Users/Seth/Desktop/iris.arff");
Instances data = source.getDataSet();
int num = data.numInstances();
return num;
}
}
Why does java.lang.exception occur?
Why does java.lang.exception occur?
In the future, when you encounter crashes, use LogCat to examine the Java stack trace associated with the crash. If you do not understand the stack trace or otherwise cannot identify the problem, and you want help here, post the stack trace along with the code. As it stands, we have to guess exactly what is going wrong in your app.
In this case, while you may be crashing elsewhere, you will definitely crash with:
ConverterUtils.DataSource source = new ConverterUtils.DataSource("C:/Users/Seth/Desktop/iris.arff");
assuming that this is code in your Android app.
You are attempting to read data from C:/Users/Seth/Desktop/iris.arff. That is a path to a file on a Windows machine. Android is not Windows. There are ~2 billion Android devices in use, and none of them have access to files on your Windows' machine's desktop.
You need to get this data onto the Android device, such as:
by putting it in the assets/ directory in your module (to package it with your app), then using AssetManager to get an InputStream on that asset, hopefully passing that directly to ConverterUtils.DataSource
downloading the file from the Internet, perhaps into internal storage (e.g., getCacheDir())
expecting the user to copy the file onto their device by hand, such as via external storage
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.
This code is from an exercise source code from a course I took. It worked fine up until this afternoon. No change was made to it and I re imported it and a similar project and experienced the same new error.
public class TourListAdapter extends ArrayAdapter<Tour> {
Context context;
List<Tour> tours;
public TourListAdapter(Context context, List<Tour> tours) {
super(context, android.R.id.content, tours);
this.context = context;
this.tours = tours;
}
I'm using Android Studio 1.3
android.R.id.content used to work and now generates the error:
Expected resource of type layout.
I've searched stackoverflow and there appears to be a new feature that annotates possible mis-matches. Somehow this android id integer used to be ok and is not any longer. I did not download anything new today to Android Studio.
I have re-built, cleaned and sycn'd. I have exited and rebooted the computer with no resolution.
It's not an error, it's simply a warning.
As you've noted in your question, resource method parameters can now be annotated to indicate what type of resource should be passed to it. Android Studio checks these parameters based upon the name and flags it if it doesn't match the correct type.
However, you've stumbled upon a corner-case that Android Studio does not deal with. As noted by this question, android.R.id.content is a special resource ID used to obtain a root view without knowing the actual ID of the view. Sadly, the Android SDK developers put it as an id resource and not as a layout resource (it should probably have been designated android.R.layout.content, but it's too late to change it now!).
So, to sum up, Android Studio is expecting you to use a R.layout.value here, but you are using a special R.id.value. But you are correct and your App should still build correctly (mine does when I tested it) - hence it is a warning, not an error.
Hello friends I want to integrate multiple languages in my application so I made multiple values folder for different languages.
Now my question is how can I display my dynamic textview value which comes from my web api which is string value. But How can I display language as per my choice from application. Below I have mentioned my code. Please help me. Thanks!
Main.java
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
textView=(TextView)findViewById(R.id.textView1);
textView.setText("My custom text which comes from web api");
}
You can use dynamic value in TextView with localization in resource folder.
You can localize text by with country code and language code.
like:
res/values-en/string.xml
res/values-hi/string.xml
res/values-IN-gu/string.xml
Read More
You can achieve the language localisation from the android framework using strings.xml, create one strings.xml for each language inside the proper values folder. Refer this LINK to do this.
From server you can get the text key which you define in stings.xml then in your textview set this key and android will automagically show the proper localised text.