what could be the problem ? i dont see nothing wrong in my code but this application is giving me black ad white flashes like its restarting every second
log cat says : ~A resource failed to call release.
Main.activity.java
package com.example.xyzenterprises;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import java.util.Locale;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadLocale();
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setTitle(R.string.app_name);
findViewById(R.id.welcomeLabel);
Button nextButton = findViewById(R.id.nextButton);
nextButton.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.language_english) {
setLocale("en");
return true;
} else if (item.getItemId() == R.id.language_french) {
setLocale("fr");
return true;
} else if (item.getItemId() == R.id.language_spanish) {
setLocale("es");
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
private void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
editor.putString("My_Lang", lang);
editor.apply();
recreate();
}
public void loadLocale() {
SharedPreferences prefs = getSharedPreferences("Settings", MODE_PRIVATE);
String language = prefs.getString("My_Lang", "en");
setLocale(language);
}
}
strings.xml
<resources>
<string name="app_name">XYZ Enterprises</string>
<string name="welcome_label">Welcome to XYZ Enterprises</string>
<string name="next_button">Next</string>
<string name="step_closer_label">You're one step closer!</string>
<string name="language">Language</string>
<string name="english">English</string>
<string name="french">French</string>
<string name="spanish">Spanish</string>
</resources>
android.manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.XYZEnterprises"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
see image
enter image description here
i tried checking for any syntax error code seems correct…
i expect the code to work and execute on the awd emulator
New contributor
Joshua Jooste is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.