Android Studio - Log.i does not work - android

Below is code
String messageToken = FirebaseInstanceId.getInstance().getToken();
SharedPreferences sharedPreferences = getSharedPreferences(Constants.USER_INFO_PREFERENCE,
Context.MODE_PRIVATE);
String userEmail = sharedPreferences.getString(Constants.USER_EMAIL,"");
if (!userEmail.equals("")) {
Log.i(TAG,userEmail);
} else {
Log.i(TAG,"Nao ha info");
}
if (messageToken!=null && !userEmail.equals("")) {
DatabaseReference tokenReference = FirebaseDatabase.getInstance().getReference()
.child(Constants.FIREBASE_PATH_USER_TOKEN).child(Constants.encodeEmail(userEmail));
tokenReference.child("token").setValue(messageToken);
Log.i(InboxActivity.class.getSimpleName(),messageToken);
}
I have no idea why any of the Log.i is not printing in the log.

Maybe it's because of your build type , logs are not displayed in release mode

I got this problem solved! Matheus Brandino, from Alura.com.br, just pointed me that I had a wrongly overriden the onCreate method (its not showing here because I had my original post edited). So the solution was to delete the second argument from onCreate.
I had:
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
Now:
public void onCreate(Bundle savedInstanceState) {

Try going to developer settings on your android phone, Find "Monitoring" Option and then "Enable OpenGL Traces" and then select LogCat.

Run -> Edit Configurations -> Android app -> app -> Miscellaneous -> Tick all the check boxes -> Apply
Hope this help.

Related

Logcat is not Printing

I am trying to Print some messages using Log.i but it doesn't print anything, the problem starts after I updated the Android studio.
How can I solve the problem?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("check","**************checking");
setContentView(R.layout.activity_main);
}
}
An alternate solution, but it's not the best.
System.out.print();
Make sure you select the device you are using, you can do it on the top left side of the Logcat.
for example if you used an emulator and than a device make sure the device is selected.
Try using the tag constant
public static final string TAG = "MainActivity"
Then do
Log.d(TAG, YOURMESSAGE)
Also as an alternative you can use System.out

Android Studio Start Up App Tips And Hints

I've looked around and I'm trying to see if Android Studio has any sort of function for when first term use of the app it shows some messages to help people.
I was thinking of using simple text boxes when on first create off the app/ first launch it would show the tips until they click on each object or item.
I know how to do the click and view just not sure the function to use for when they start the app?
You detect the first launch of your app for instance by loading (and defaulting) a boolean value from your app's preferences.
In your MainActivity
private boolean firstStart = true;
#Override
public void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = mContext.getSharedPreferences("APP", Context.MODE_PRIVATE);
firstStart = prefs.getBoolean("firstStart", true);
if (firstStart) {
// do your stuff on first start here
// like set "showWizard" or any other flag to true so
// your controls can behave in "firstStart" mode
// Don't forget to save, that it is now no longer the first start...
firstStart = false;
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstStart", false);
editor.apply();
}
}
Maybe try adding them into this:
#Override
public void onCreate() {
super.onCreate();
//put your lines in here
}

FirebaseApp not initializing despite FirebaseApp.initializeApp() being called in Application class

I am creating an Android application and I'm currently trying to implement user authentication using Firebase. As far as I can tell, my app is connected to my Firebase server.
I encounter a runtime error when attempting to switch from the SignIn activity to the SignUp activity via a button press. The app crashes and I encounter a runtime error.
So far as I can tell, the runtime error is from the SignUp activity's onCreate() call when I attempt to initialize a FirebaseAuth object with FirebaseAuth.getInstance(). This call fails due to
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process seniordesign.phoneafriend. Make sure to call FirebaseApp.initializeApp(Context).
However, I make this call in my Application class' onCreate() method which I thought would be fine. I added the initalizeApp() call to the SignUp's onCreate() call but no dice. I've looked for others with this issue but have not found anything similar. Thanks for any help.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="seniordesign.phoneafriend">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="seniordesign.phoneafriend.PhoneAFriend">
<activity android:name=".SignIn">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SignUp"></activity>
</application>
</manifest>
PhoneAFriend.java (My Application class)
public class PhoneAFriend extends Application {
public void onCreate(){
super.onCreate();
Firebase.setAndroidContext(this);
FirebaseApp.initializeApp(this);
}
}
SignUp.java
public class SignUp extends AppCompatActivity {
protected Firebase ref;
protected EditText emailText;
protected EditText passText;
protected EditText confirmText;
protected Button button;
protected SignUp thisContext;
protected FirebaseAuth auth;
protected FirebaseAuth.AuthStateListener authListener;
private View.OnClickListener onClickListener;
public static Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
ref = new Firebase("https://phoneafriend-7fb6b.firebaseio.com");
emailText = (EditText) findViewById(R.id.signup_emailText);
passText = (EditText) findViewById(R.id.signup_passwordText);
confirmText = (EditText) findViewById(R.id.signup_passwordConfirm);
intent = new Intent(this, SignIn.class);
//Tried this already
//FirebaseApp.initializeApp(this);
auth = FirebaseAuth.getInstance();
button = (Button) findViewById(R.id.signup_button);
onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
createUser(view);
Log.v("SignUp Button" , "Clicked; Attempting to create user");
}
};
button.setOnClickListener(onClickListener);
authListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged( FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d("FirebaseAuth", "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d("FirebaseAuth", "onAuthStateChanged:signed_out");
}
// ...
}
};
thisContext = this;
}
#Override
public void onStart(){
super.onStart();
//auth.addAuthStateListener(authListener);
}
#Override
public void onStop(){
super.onStop();
if(authListener != null) {
//auth.removeAuthStateListener(authListener);
}
}
protected void createUser(View view){
String cString = null;
String pString = null;
String eString = emailText.getText().toString();
if(passText.getText() != null && confirmText.getText() != null) {
pString = passText.getText().toString();
cString = confirmText.getText().toString();
Log.v("SignUP: Pass Null check" , "Pass" );
if (emailText.getText() != null && pString.equals(cString) && passText.getText() != null) {
Log.v("SignUP: Sign up check " , "Pass");
auth.createUserWithEmailAndPassword(emailText.getText().toString() , passText.getText().toString())
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.v("createUser complete" , "status: " + task.isSuccessful());
if(task.isSuccessful()){
startActivity(SignUp.intent);
}
}
});
}
}
return;
}
}
I know there's already an accepted answer. However, I ran into the same error message saying: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process (name). Make sure to call FirebaseApp.initializeApp(Context).
I tried several solutions found on SO, and double checking everything, until I finally found that the package name defined in the Firebase Console didn't match the package name defined in my manifest file.
Try go to your Firebase Console -> Project settings -> check if package names matches.
Hope it may help some :)
Just as qbix stated, you much use the API calls from whichever version you are going to use. If possible, you should use the newer API because it will definitely be supported much further into the future.
See the docs here:
https://firebase.google.com/docs/database/android/start/
Remove:
Firebase.setAndroidContext(this);
FirebaseApp.initializeApp(this);
And put:
FirebaseDatabase database = FirebaseDatabase.getInstance();
If qbix puts his comment into an answer, you should accept his rather than mine seeing has how he beat me by a few minutes.
Also:
If you are using the old firebase and need help switching, this guide is spot on and will help you with the switch. It's a fairly simple switch.
https://firebase.google.com/support/guides/firebase-android
I encountered this problem after I deleted and re-cloned my app and forgot to include the google-services.json inside the app module. After I re-added it, the problem went away. Nevertheless, you should init the context inside your custom Application class:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
FirebaseApp.initializeApp(this)
}
}
<manifest
package="your.app">
<application
android:name=".MyApplication"
It is easier to use the Firebase wizard that is included in Android Studio: it adds all the dependencies in the gradle files, creates the firebase project if needed, connects the app with the project, etc.
In Android Studio open menu Tools / Firebase and follow the instructions.
Then FirebaseApp.initializeApp() will return a valid value.
First check up should be the Firebase documentation and the tools from
Android Studio -> Tools -> Firebase -> Cloud Messaging.
Even if you have done that before.
See this, that and that.
Again, even if you already done that before as firebase versions tend to require an up to date configuration JSON.
After much frustration, enabling Internet Permissions fixed this issue for me.
Upgrading classpath 'com.google.gms:google-services:4.1.0' to classpath 'com.google.gms:google-services:4.2.0' in the project Gradle worked for me.
Update 2019 : If you update your Firebase dependencies to latest then you should update Google play services as well.
During the writing of this answer I had updated all my Firebase libs to 17.0.+ but my Google play service was still pointing at 4.1.0.
Updating play service version to 4.3.0(Latest) fixed it for me.

Android preferences changed

I have some preferences in my activity and I would like to achieve that, depending on these preferences, some services will be initiated or not.
I am trying to implement and onSharedPreferenceChangedmethod, but it never gets called (even if I have assigned it to a field).
The code is the following:
public class MtprojectActivity extends Activity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
callsCheckbox = prefs.getBoolean("callsLogChk", true);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (!started) {
startCallsService();
bindCallsService();
} else {
releaseCallsService();
stopCallsService();
}
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
Thanks a lot in advance!
Are you sure it isn't being called or is it just a case of it not doing what you expect? I may be wrong but shouldn't the code look like this...
if (key.equals("callsLogChk")) {
if (!started) { // Check NOT started ???
startCallsService();
bindCallsService();
}
else { // The 'else' should compliment the check for !started ???
releaseCallsService();
stopCallsService();
}
}
Sorry, as I don't know your code I may be totally wrong but as you've posted it, it doesn't look quite right to me.
EDIT:
Also, are you making sure you call the SharedPreferences.Editor.commit() method when you make the change to the preference?

Android sharedpreferences

I've been following this tutorial and i am stuck.
public class Main extends Activity {
SharedPreferences mPrefs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
firstRunPreferences();
if(getFirstRun())
{
Toast.makeText(Main.this, "firstrun", Toast.LENGTH_SHORT).show();
setRunned();
}
else
{
Toast.makeText(Main.this, "not firstrun", Toast.LENGTH_SHORT).show();
}
}
public boolean getFirstRun() {
return mPrefs.getBoolean("firstRun", true);
}
public void setRunned() {
SharedPreferences.Editor edit = mPrefs.edit();
edit.putBoolean("firstRun", false);
edit.commit();
}
public void firstRunPreferences() {
Context mContext = Main.this.getApplicationContext();
mPrefs = mContext.getSharedPreferences("myAppPrefs", 0);
}
}`
Everytime i run it in Eclipse it says "not firstrun". I guess the preferences reset every time the app is reinstalled, so what is wrong with the code? As far as i remember, i saw once "firstrun".
Thanks
I am assuming you are using the Emulator to run your app. Are you closing the emulator between runs?
Check if you have the "Wipe User Data" checkbox ticked in the Target tab of your Debug run configuration in Eclipse.
SharedPreferences are not cleaned by uninstall.
If you want something cleaned after reinstall, put a field in your database.
Stéphane
I'm not sure but I think SharedPreferences are removed when uninstalling, but not when updating an app.

Categories

Resources