Firebase数据库无法从Firebase数据库引用中获取数据。

huangapple go评论80阅读模式
英文:

Firebase Database not fetching data from Firebase Database Reference

问题

public class MainActivity extends AppCompatActivity {
    TextView appName;
    FirebaseUser firebaseUser;
    String currentVersionName;
    long currentVersionCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appName = findViewById(R.id.appName);
        currentVersionName = BuildConfig.VERSION_NAME;
        currentVersionCode = BuildConfig.VERSION_CODE;
        animateAppName();
        netConn();
        FirebaseAuth.getInstance();
    }

    public void applyScreenChange(){
        // ... (skipped for brevity)
    }

    public void animateAppName(){
        // ... (skipped for brevity)
    }

    public void netConn(){
        // ... (skipped for brevity)
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Version");
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                // ... (skipped for brevity)
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                // ... (skipped for brevity)
            }
        });
    }

    public void newActivity(){
        // ... (skipped for brevity)
    }

    public void skipSetupToAdvertiser(){
        // ... (skipped for brevity)
    }

    public void skipSetupToCreator(){
        // ... (skipped for brevity)
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.webroose.sponso">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:allowBackup="true"
        android:theme="@style/AppTheme">

        <!-- Activity declarations ... (skipped for brevity) -->

        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="@string/app_id"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts"/>
    </application>
</manifest>
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    implementation 'com.google.android.material:material:1.2.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'com.google.firebase:firebase-database:19.4.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.navigation:navigation-fragment:2.3.0'
    implementation 'androidx.navigation:navigation-ui:2.3.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'com.google.firebase:firebase-auth:19.3.2'
    implementation 'com.firebaseui:firebase-ui-database:6.2.0'
    implementation 'com.google.android.gms:play-services-ads:19.3.0'
    implementation 'com.android.support:multidex:2.0.0'
    implementation 'com.google.firebase:firebase-analytics:17.5.0'
    implementation 'com.google.firebase:firebase-messaging:20.2.4'
    implementation 'com.facebook.android:audience-network-sdk:5.10.1'
}
英文:

Everything is working fine but DatabaseReference is not fetching data, it's like just ignoring my code to run and like my internet is not working please help i'm new here in this community below are my codes and images.

Previously it was working, but as i just changed some code to make only currentVersion >= vCode so that user can continue even if the value in database is < currentVersion but, after building it my app was not fetching data from firebase and the i tried to uninstall it, installed again from play store the older version it also was not fetching data please help. I don't know what wrong i did. Thus i am planning to install ParrotOS so maybe then it'll work but it's better to ask first from the experts

firebase database image

MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView appName;
FirebaseUser firebaseUser;
String currentVersionName;
long currentVersionCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appName = findViewById(R.id.appName);
currentVersionName = BuildConfig.VERSION_NAME;
currentVersionCode = BuildConfig.VERSION_CODE;
animateAppName();
netConn();
FirebaseAuth.getInstance();
}
public void applyScreenChange(){
String sharedPerfId = &quot;MyAppPref&quot;;
SharedPreferences sharedPreferences = getSharedPreferences(sharedPerfId,0);
boolean isAdvertiserLoggedIn = sharedPreferences.getBoolean(&quot;isAdvertiserLoggedIn&quot;,false);
boolean isCreatorLoggedIn = sharedPreferences.getBoolean(&quot;isCreatorLoggedIn&quot;,false);
if (isAdvertiserLoggedIn){
skipSetupToAdvertiser();
}
else if (isCreatorLoggedIn){
skipSetupToCreator();
}
else {
newActivity();
}
}
public void animateAppName(){
appName.setTranslationY(-1000f);
appName.animate().translationYBy(1000f).setDuration(500);
}
public void netConn(){
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
assert connectivityManager != null;
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo == null || !networkInfo.isConnected() || !networkInfo.isAvailable()){
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle(&quot;No Internet Connection&quot;);
alertDialog.setMessage(&quot;Connect to Network and Then try again&quot;);
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, &quot;Exit&quot;, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.super.onBackPressed();
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, &quot;Retry&quot;, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
netConn();
}
});
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}else {
//            Check older version
//            applyScreenChange();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference(&quot;Version&quot;);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String vName = (String) snapshot.child(&quot;latestVersionName&quot;).getValue();
long vCode = (long) snapshot.child(&quot;latestVersionCode&quot;).getValue();
if (currentVersionName.equals(vName) &amp;&amp; currentVersionCode &gt;= vCode){
applyScreenChange();
// TODO: 16-08-2020 add condition to open app if version is higher
}else {
Toast.makeText(MainActivity.this, &quot;Old Version&quot;, Toast.LENGTH_SHORT).show();
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle(&quot;You&#39;re on Older Version&quot;);
alertDialog.setMessage(&quot;This Version is no more supported, Kindly update your App to Continue&quot;);
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, &quot;Exit&quot;, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.super.onBackPressed();
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, &quot;Update&quot;, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.app_link))));
Toast.makeText(MainActivity.this, &quot;Kindly Search Sponso on PlayStore and Update&quot;, Toast.LENGTH_SHORT).show();
finish();
}
});
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
public void newActivity(){
Intent intent = new Intent(MainActivity.this, AdvertiserCreatorChooser.class);
startActivity(intent);
finish();
}
public void skipSetupToAdvertiser(){
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser!=null) {
Intent intent = new Intent(MainActivity.this, AdvertiserHome.class);
startActivity(intent);
finish();
}else{
Intent intent = new Intent(MainActivity.this, AdvertiserLoginRegister.class);
startActivity(intent);
finish();
}
}
public void skipSetupToCreator(){
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser!=null) {
Intent intent = new Intent(MainActivity.this, CreatorHome.class);
startActivity(intent);
finish();
}else {
Intent intent = new Intent(MainActivity.this, CreatorLoginRegister.class);
startActivity(intent);
finish();
}
}
}

manifest.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
package=&quot;com.webroose.sponso&quot;&gt;
&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot;/&gt;
&lt;application
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:supportsRtl=&quot;true&quot;
android:allowBackup=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;&gt;
&lt;activity android:name=&quot;.Advertiser.AdvertiserCampaignUpdate&quot;/&gt;
&lt;activity android:name=&quot;.Advertiser.ui.main.AdvertiserSettings&quot; /&gt;
&lt;activity android:name=&quot;.Creator.CreatorRecentlyChat&quot; /&gt;
&lt;activity android:name=&quot;.Advertiser.AdvertiserRecentlyChat&quot; /&gt;
&lt;activity android:name=&quot;.Advertiser.AdvertiserChatActivity&quot; /&gt;
&lt;activity android:name=&quot;.Creator.CreatorChatActivity&quot; /&gt;
&lt;meta-data
android:name=&quot;com.google.android.gms.ads.APPLICATION_ID&quot;
android:value=&quot;@string/app_id&quot; /&gt;
&lt;activity android:name=&quot;.Creator.CreatorUserDetails&quot; /&gt;
&lt;activity android:name=&quot;.Creator.CreatorList&quot; /&gt;
&lt;activity android:name=&quot;.Common.AboutUs&quot; /&gt;
&lt;activity
android:name=&quot;.AdvertiserHome&quot;
android:label=&quot;@string/title_activity_advertiser_home&quot;
android:theme=&quot;@style/AppTheme.NoActionBar&quot; /&gt;
&lt;activity
android:name=&quot;.Advertiser.AdvertiserLoginRegister&quot;
android:label=&quot;@string/title_activity_advertiser_login_register&quot;
android:theme=&quot;@style/AppTheme.NoActionBar&quot; /&gt;
&lt;activity android:name=&quot;.Common.PrivacyPolicy&quot; /&gt;
&lt;activity android:name=&quot;.Common.ChangePassword&quot; /&gt;
&lt;activity android:name=&quot;.Creator.CreatorSettings&quot; /&gt;
&lt;activity android:name=&quot;.Common.ChangeEmail&quot; /&gt;
&lt;activity android:name=&quot;.Advertiser.AdvertiserUserDetails&quot; /&gt;
&lt;activity android:name=&quot;.Advertiser.AdvertiserList&quot; /&gt;
&lt;activity
android:name=&quot;.CreatorHome&quot;
android:label=&quot;@string/title_activity_creator_home&quot;
android:theme=&quot;@style/AppTheme.NoActionBar&quot; /&gt;
&lt;activity
android:name=&quot;.Creator.CreatorLoginRegister&quot;
android:label=&quot;@string/title_activity_creator_login_register&quot;
android:theme=&quot;@style/AppTheme.NoActionBar&quot; /&gt;
&lt;activity android:name=&quot;.AdvertiserCreatorChooser&quot; /&gt;
&lt;activity android:name=&quot;.MainActivity&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
&lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;meta-data
android:name=&quot;preloaded_fonts&quot;
android:resource=&quot;@array/preloaded_fonts&quot; /&gt;
&lt;/application&gt;
&lt;/manifest&gt;

dependency

dependencies {
implementation fileTree(dir: &#39;libs&#39;, include: [&#39;*.jar&#39;])
implementation &#39;androidx.appcompat:appcompat:1.2.0&#39;
implementation &#39;androidx.constraintlayout:constraintlayout:2.0.1&#39;
implementation &#39;com.google.android.material:material:1.2.0&#39;
implementation &#39;androidx.lifecycle:lifecycle-extensions:2.2.0&#39;
implementation &#39;com.google.firebase:firebase-database:19.4.0&#39;
implementation &#39;androidx.legacy:legacy-support-v4:1.0.0&#39;
implementation &#39;androidx.navigation:navigation-fragment:2.3.0&#39;
implementation &#39;androidx.navigation:navigation-ui:2.3.0&#39;
testImplementation &#39;junit:junit:4.13&#39;
androidTestImplementation &#39;androidx.test.ext:junit:1.1.2&#39;
androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.3.0&#39;
implementation &#39;com.google.firebase:firebase-auth:19.3.2&#39;
implementation &#39;com.google.firebase:firebase-database:19.4.0&#39;
implementation &#39;com.firebaseui:firebase-ui-database:6.2.0&#39;
implementation &#39;com.google.android.gms:play-services-ads:19.3.0&#39;
implementation &#39;com.android.support:multidex:2.0.0&#39;
implementation &#39;com.google.firebase:firebase-analytics:17.5.0&#39;
implementation &#39;com.google.firebase:firebase-messaging:20.2.4&#39;
implementation &#39;com.facebook.android:audience-network-sdk:5.10.1&#39;
}

答案1

得分: 0

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Version");

英文:

Change <br>
DatabaseReference databaseReference =FirebaseDatabase.getInstance().getReference(&quot;Version&quot;); <br>
To<br>
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child(&quot;Version&quot;);

答案2

得分: 0

而实际问题出现在Firebase数据库上

请查看下面关于Firebase数据库的图片:

Firebase数据库无法从Firebase数据库引用中获取数据。

英文:

And the actual problem was with Firebase Database

Check below image with firebase database

Firebase数据库无法从Firebase数据库引用中获取数据。

huangapple
  • 本文由 发表于 2020年9月3日 18:34:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63721851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定