My Android App hangs on SharedPreferences, but only in Android 5 - android

An odd one here. I've built an app in Android Studio which is working perfectly in Android 4 (4.4.2), but it just stopping on Android 5.0.2. It's not throwing up any errors, just stopping.
Going line by line, it seems to be focussed on the SharedPreferences file, which it's not loading.
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
final SharedPreferences sharedPreferences = this.getSharedPreferences("appname", MODE_PRIVATE );
currentlyTracking = sharedPreferences.getBoolean("currentlyTracking", false);
So, on Android 4, going line by line, when I get to the currentlytracking line, I can view the contents of the SharedPreferences file in the Variables window, and the app works. On Android 5, when I get to the currentlytracking line, none of the variables show up, and it just says 'Collecting data'. The app then stops if I try to continue on.
Interestingly, if I change the name of the Sharedpreferences file, it will create a new file but then fail to save to it.
This is only happening on Android 5, any thoughts would be appreciated.
Cheers

Related

slow startup (500ms) with all apps made by My Android studio

I'm a beginner and when i ever i make an app and i test it on real phone
the first activity takes 400-900ms to start, even when it's only show a text view or a button. (just empty App)
on the same phone i have few apps (Games) from Play Store , and they work instantly! there's no 10ms delay!
so I'm wondering why is this happening to me even if i make (Empty App)
I mean seriously, why would it starts slowly with this simple code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setText("Hello World");
}
}
The problem is that the APK you are generating is with debug property on, so this APK is considered to be in development mode that's why it will take longer to launch. You need to generate a production APK (same one you can publish on Play Store) in order to have the same launch speed as the other apps. Check this out : Sign Your APK

Creating animator file in Android Studio blocks app

I'm working on Android apps in Android Studio and need to add an animation to the project using an xml file.So I created a res/animator directory and I put in it a file called pulsation.xml,which do nothing, here the code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:ordering="together">
</set>
Inside an Activity, the app access to some value stored in a SharedPreferences object.When running the apps without the pulsation.xml file in the res/animator directory the program retrieves the values needed without problems, when putting a .xml file inside res/animator the programs can't retrieve anything.I can't understand where this weird behavior come from. Here the piece of code where the app retrieve the data from the SharedPreferences object:
SharedPreferences pref = getSharedPreferences("ballan.emmanuele.hellpuzzle.PUNTEGGI",MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
do
{
next = pref.getInt(R.id.liv1+"next",R.id.liv2);
TextView punt = (TextView) findViewById(pref.getInt(next+"punt",R.id.punt1));
punt.setText(pref.getInt(next+"punteggio",0)+"");
TextView num = (TextView) findViewById(pref.getInt(next+"num",R.id.num1));
num.setText(pref.getInt(next+"numero_sequenza",0)+"");
LinearLayout lay =(LinearLayout)findViewById(pref.getInt(next+"riga",R.id.riga1));
lay.setVisibility(View.VISIBLE);
}while (next != ultimo);
I set the minimum SDK version to 11.Thanks for helping.
I discovered the problem consists with Nexus S Api 23. Using this emulator the program still don't work, changing emulator or using my Samsung or other Nexus emulators phone the app doesn't bother.

Libgdx Freetype Font Generator Outline acting weird

I'm using asset manager to generate my font using object
FreeTypeFontGeneratorLoader, specifically I'm using this current parameter
FreeTypeFontLoaderParameter parameter = new FreeTypeFontLoaderParameter();
parameter.fontFileName = "fonts/myFont.ttf";
parameter.fontParameters.size = size;
parameter.fontParameters.minFilter = Texture.TextureFilter.Linear;
parameter.fontParameters.magFilter = Texture.TextureFilter.Linear;
if(withStroke) {
parameter.fontParameters.borderStraight = true;
parameter.fontParameters.borderColor = Color.BLACK;
parameter.fontParameters.borderWidth = 1.5f;
}
parameter.fontParameters.color = Assets.fontColor;
(then I load my font in static variable in Assets class)
everything is normal if the app runs for the first time,
here is the generated text with outline screenshot:
screenshot
but after I close my app using this line
Gdx.app.exit();
and then open my app again, the outline is now starting to acting weird
here is the screenshot of weird generated font:
screenshot
I'm already trying to dispose my asset manager in event dispose of ApplicationListener in Gdx, but sometime my font still acting weird.
if I clear my processing run in all task android, then the font is normal again.
Can you tell me possible solutions of why it is happening, and do you have any suggestions?
Thx
Android sometimes keeps static (final) variables even when the app lost its context.
When using static variables: make sure you dispose when required and set them to null afterwards. Or do not use static for assets.
Since this problem happen because my app not closed completely because this line :
Gdx.app.exit();
is just doing this :
Schedule an exit from the application. On android, this will cause a call to pause() and dispose() some time in the future, it will not immediately finish your application"
that's why I change my way to close my app using this line:
System.exit(0);
then my app is loading and generating new font when user open my app again.

"...cannot be resolved to type" error message; no method suggestions in Eclipse

Ok guys I've got a question that for the life of me I cannot get figured out. I'm using Eclipse, and I've got the Android ADT pluging all installed, the program configured with the Android SDK path, and all the extra SDK parts downloaded (Google API's, SDK Platforms, System Images). Here is my problem: whenever I go to access a method from a certain object, Eclipse gives no method "suggestions" like it usually does. I'm probably not explaining this the best, so I've attached a screenshot that kind of illustrates the issue.
public class StartingPoint extends Activity {
int counter;
Button add;
Button sub;
TextView display;
//putting add.whatever here doesn't work
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
counter = 0;
add = (Button) findViewById(R.id.buttonAdd);
sub = (Button) findViewById(R.id.buttonSub);
display = (TextView) findViewById(R.id.textView1);
add.setOnClickListener(new View.OnClickListener() {
//putting add.whatever here doesn't show suggestions either.
I've tried everything I can think of to get it working, deleting & reinstalling eclipse, deleting everything Android from with Eclipse and going through all the SDK setup again, changing my workspace, creating an entirely new project, deleting the Eclipse preference file, endless Googling, resetting & altering settings, and I can't for the life of me figure out what's causing this. Any ideas? Thanks!

Android: SharedPreference errors after force close

I'm getting some weird effects after I force close my app. When the app is closed with finish(), everything is fine. I have some variables saved in a sharedPreferences so when the app is loaded again, it can restore those variables into the UI. However, if I force close the app and THEN try to continue where it had left off, some variables start "acting funny". By that I mean (in onCreate) I check to see if a string, loaded from the sharedPreferences, equals a value (crunched down version):
String namec;
private static final String TAG = "MyActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//namec was set as "forest" in a previous activity
//which is bypassed if the user selects continue
//from the main menu
SharedPreferences pathtaken = getSharedPreferences("pathtakenpref", MODE_WORLD_READABLE);
namec = pathtaken.getString("namec", "Unknown");
ImageView v1 = (ImageView) findViewById(R.id.pathpic1);
RelativeLayout v2 = (RelativeLayout) findViewById(R.id.pathmain);
Log.i(TAG, "namec= " + namec);
if(namec == "forest"){
v1.setImageResource(R.drawable.forest);
v2.setBackgroundResource(R.drawable.forestrepeat);
}
}
What happens here is namec, does in fact, equal "forest". I send the value to the log and it shows the variable exactly as it should be ("forest"). Yet it won't run the code inside of the if{}. It's giving me nightmares. I've been stuck on this for a week!
In the same code, I load a different set of sharedPreferences (labeled as TRHprefs) and each one of those (6 integers and 3 strings) load up and display just fine. I even add an if{} to test 1 string and 1 integer from TRHprefs... they both came back true.
Q.1: Is there anything that can cause my sharedPreferences xml to become, somehow, corrupted on a force close?
Q.2: Is there a way for me to view the xml file before and after I use force close to help debug the situation. Thanks so much!
Its a String. Try this:
if("forest".equals(namec)){
If you want to compare two String you need to use this:
if(namec.equals("forest")){
v1.setImageResource(R.drawable.forest);
v2.setBackgroundResource(R.drawable.forestrepeat);
}

Categories

Resources