I have given this to save data with sharedpreference
so I want to get value to a spinner, here I have given this
SharedPreferences spf = getSharedPreferences("myprfs", Context.MODE_PRIVATE);
String ret = spf.getString("ctyp","");
Here am getting the value to a String, I want to assign a same value to a spinner.
for(int i=0;i<2;i++)
if(ret.equals(spinner.getItemAtPosition(i).toString())){
spinner.setSelection(i);
break;
}
I am getting this error
'java.lang.Object android.widget.Spinner.getItemAtPosition(int)' on a null object reference
Can any one suggest me correct ans..
Your spinner should be null. Set a breakpoint to confirm that. Remember to run in debug mode to make the breakpoint to "break"
Most probably:
not done a findViewById
using ButterKnife and forgot to bind it
If you've done the above, but spinner is still null, in which lifecycle method are you running above code? Maybe you're trying to touch the UI before it's loaded
Related
I have 2 fragments.
Fragment 1
Loads sharedpreference to display string
Fragment 2
Saves sharedprefence for string
Is it possible to retrieve that string in my first Fragment without running the second Fragment?
Yes, this is possible. You just need to make sure you are reading with the same key you used to write with:
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
// Reading from SharedPreferences
String value = prefs.getString("myKey", "defaultValue");
Log.d(LOG_TAG, value);
Note that we've assigned a defaultValue as the return value here. If there is no value with the key "myKey" in your shared prefs, it will instead return "defaultValue". This is a nice safeguard, think of it like a null pointer check - you will always get a value from getString(), even if it's just the default.
You don't need to be in the same activity for this to work, you just need to make sure that 1) your preferences name is the same and 2) the key used to store the value is the same in both spots.
First, don't get confuse between Activity and Fragment.
And yes, you can.
On the picture you can see that the boolean variable took the default value, even though there was a key-value pair in the SharedPreferences with the right key. What can cause this? In the code this is at the end of an onCreate method. After this the onMapReady method gets called (from com.google.android.gms.maps.OnMapReadyCallback), where I check the SAME boolean value, to see If i have to place some markers on the map or not. In that method the getBoolean() behaviour is correct, the default value is ignored. This doesn't make any sense to me, anyone can help me out?
1) Did you intend the space in the key "isThereReservation " ? In your debug code, the variable doesn't have a space. Make sure you use the right key else you'll get the default value!
2) How are you saving the sharedPref? your code should be:
myPrefs = myContext.getSharedPreferences("MY_PREFERENCE_NAME", MODE_PRIVATE);
myPrefEditor = myPrefs.edit();
myPrefsEditor.clear();
myPrefsEditor.putBoolean("MY_KEY",myBool);
and then you can access by using:
myPrefs = myContext.getSharedPreferences("MY_PREFERENCE_NAME", MODE_PRIVATE);
myPrefs.getBoolean("MY_KEY",MY_DEFAULT_VALUE);
The method getBoolean return default value if the key doesn't exist. In your case, the default value is false and if the key of the SharedPreference doesn't exist is false in your case.
You are showing in debug the value of the HashMap mMap but what is the content of the eu.arrowhed.arrowheaddemo for the same key?
I have tried to print a variable value but this is what was printed:
android.support.v7.internal.widget.TintEditText#41fb6428
My code is:
Torre objTorre = new Torre();
objTorre.setLatitude(txtLat.toString());
String torre = objTorre.getLatitude();
System.out.println(torre.toString());
just do this
objTorre.setLatitude(txtLat.getText().toString());
Change the first line to:
objTorre.setLatitude(txtLat.getText().toString());
To get the text from your EditText and not the reference to it.
getLatitude() returns an instance of android.support.v7.internal.widget.TintEditText, which AFAIK is a subclass of EditText.
If you are trying to get at what the user typed into the EditText as a String, call objTorre.getLatitude().getText().toString().
I get the list data from webservices (Json) in spinner. But before websevices data in spinner i want show null value.(Need first value is null)
please any one help me..
please give me a sample code.
Thank You.
You could add a dummy object representing your null value. But spinners are not designed for this - they should contain some data (or nothing if no data is available).
So in your case I would recommend using a ListView or something similar instead which could be opened in a new activity with startActivityForResult and return an Intent.
Are you doing whole process with single thread?
you can use your listener, first you inflate empty array/list and later you can set your data change..
A null value inside a spinner will give you a really annoying exception. It's better to add as first element of the array some object like "Please, select one" or "Nothing selected". When you create the array you give to the spinner, first add this element and then load the real data.
I am working on an application, and from a list view I create a bundle that includes the item selected and the previous item. What I need to determine is if the previous item actually gets bundled through. If it exists, I want the information, but if it doesn't exist, then I need to set my text views to reflect that. But I get a null pointer exception if it doesn't exist when trying to load the receiving activity (the bundling activity does not cause the crash as I found during debugging - I get to the point where I'm testing for the data in the bundle before it crashes). So I've included the code from the receiving activity.
Bundle evmBundle = this.getIntent().getExtras();
final EVMData evm = (EVMData) evmBundle.getSerializable("evm");
final Project project = (Project) evmBundle.getSerializable("project");
if (!evmBundle.getSerializable("prvEVM").equals(null)){
final EVMData prvEvm = (EVMData) evmBundle.getSerializable("prvEVM");
edtPrvAC = (TextView) findViewById(R.id.edtPrvEVMAC);
edtPrvAC.setText(prvEvm.getAc().toString());
}
else{
edtPrvAC = (TextView) findViewById(R.id.edtPrvEVMAC);
edtPrvAC.setText("0");
}
I know something is getting passed through in the bundle for "prvEVM" because it showed up as part of the bundle in the debugger.
I also tried pulling it out of the bundle first and then trying to compare it. After pulling it out of the bundle, prvEvm is null (looking at the variables in the debugger), so I thought something like this might work:
if (!prvEvm.equals(null)){
edtPrvAC = (TextView) findViewById(R.id.edtPrvEVMAC);
edtPrvAC.setText(prvEvm.getAc().toString());
}else{
edtPrvAC = (TextView) findViewById(R.id.edtPrvEVMAC);
edtPrvAC.setText("0");
}
But I get a NullPointerException because prvEvm is Null. So I tried flipping the if test around, but got the same results. I should note that no matter how I set this up, if prvEvm is not null, all configurations here work - the requested data is put in the TextView. Basically, I need to be able to get around this null pointer exception. I know the object is null, I want to test for that so that if it is, that object isn't used. It will only be null once.
Don't use equals(null) as you call a method (equals) on a null object. Instead, compare to null:
if (prvEvm != null){
besides, equals(null) always returns false.