java - Firebase データベースが Firebase データベース リファレンスからデータをフェッチしない

okwaves2024-01-25  10

すべてが正常に動作していますが、DatabaseReference がデータをフェッチしていません。コードの実行を無視しているようなもので、インターネットが機能していないのと同じです。助けてください。このコミュニティの初心者です。以下に私のコードと画像を示します。

以前は機能していましたが、一部のコードを変更して currentVersion >= vCode のみを作成し、データベースの値が < であってもユーザーが続行できるようにしました。 currentVersion ですが、ビルド後、アプリが Firebase からデータをフェッチせず、アンインストールしようとしましたが、Play ストアから古いバージョンを再インストールしましたが、データもフェッチしていませんでした。助けてください。私が何を間違ったのか分かりません。したがって、私は ParrotOS をインストールする予定なので、おそらくうまくいくでしょうが、最初に専門家に尋ねたほうがよいでしょう。

Firebase データベース イメージ

まいnActivity.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 = "MyAppPref";
        SharedPreferences sharedPreferences = getSharedPreferences(sharedPerfId,0);
        boolean isAdvertiserLoggedIn = sharedPreferences.getBoolean("isAdvertiserLoggedIn",false);
        boolean isCreatorLoggedIn = sharedPreferences.getBoolean("isCreatorLoggedIn",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("No Internet Connection");
            alertDialog.setMessage("Connect to Network and Then try again");
            alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Exit", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    MainActivity.super.onBackPressed();
                }
            });
            alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Retry", 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("Version");
            databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    String vName = (String) snapshot.child("latestVersionName").getValue();
                    long vCode = (long) snapshot.child("latestVersionCode").getValue();
                    if (currentVersionName.equals(vName) && currentVersionCode >= vCode){
                        applyScreenChange();

                        // TODO: 16-08-2020 add condition to open app if version is higher
                    }else {
                        Toast.makeText(MainActivity.this, "Old Version", Toast.LENGTH_SHORT).show();
                        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                        alertDialog.setTitle("You're on Older Version");
                        alertDialog.setMessage("This Version is no more supported, Kindly update your App to Continue");
                        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Exit", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                MainActivity.super.onBackPressed();
                            }
                        });
                        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Update", 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, "Kindly Search Sponso on PlayStore and Update", 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();
        }
    }
}

マニフェスト.xml

<?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 android:name=".Advertiser.AdvertiserCampaignUpdate"/>
        <activity android:name=".Advertiser.ui.main.AdvertiserSettings" />
        <activity android:name=".Creator.CreatorRecentlyChat" />
        <activity android:name=".Advertiser.AdvertiserRecentlyChat" />
        <activity android:name=".Advertiser.AdvertiserChatActivity" />
        <activity android:name=".Creator.CreatorChatActivity" />

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

        <activity android:name=".Creator.CreatorUserDetails" />
        <activity android:name=".Creator.CreatorList" />
        <activity android:name=".Common.AboutUs" />
        <activity
            android:name=".AdvertiserHome"
            android:label="@string/title_activity_advertiser_home"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Advertiser.AdvertiserLoginRegister"
            android:label="@string/title_activity_advertiser_login_register"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".Common.PrivacyPolicy" />
        <activity android:name=".Common.ChangePassword" />
        <activity android:name=".Creator.CreatorSettings" />
        <activity android:name=".Common.ChangeEmail" />
        <activity android:name=".Advertiser.AdvertiserUserDetails" />
        <activity android:name=".Advertiser.AdvertiserList" />
        <activity
            android:name=".CreatorHome"
            android:label="@string/title_activity_creator_home"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Creator.CreatorLoginRegister"
            android:label="@string/title_activity_creator_login_register"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".AdvertiserCreatorChooser" />
        <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.google.firebase:firebase-database:19.4.0'
    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'
}

このコードのどの部分が期待どおりに動作しないのでしょうか?

– アレックス・マモ

2020 年 9 月 3 日 10:44

DatabaseReference はデータを呼び出して文字列の値を取得する必要がありますが、Firebase からデータを呼び出さない限り、無視するようなもの。 public void onDataChange(@NonNull DataSnapshot スナップショット) { String vName = (String) snapshot.child("latestVersionName").getValue(); long vCode = (long) snapshot.child("latestVersionCode").getValue();

– アヴィナシュ・ジャー

2020 年 9 月 3 日 11:07

まず、エラーを無視するのをやめてください。 Log.d(TAG,databaseError.getMessage()); を使用します。 logcat に何か出力されていますか?

– アレックス・マモ

2020 年 9 月 3 日 11:27

今そのログを試してみましたが、エラーは発生しませんでしたが、同じ Firebase アカウントに関連する他のアプリもデータを読み込んでいません。 Firebase がクラッシュしましたか?それとも Firebase サーバーで何かが起こったのでしょうか?

– アヴィナシュ・ジャー

2020 年 9 月 3 日 13:01



------------------------

変更 DatabaseReference DatabaseReference =FirebaseDatabase.getInstance().getReference("バージョン"); に DatabaseReferencedatabaseReference = FirebaseDatabase.getInstance().getReference().child("バージョン");

2

すでに試しました...もっと提案をお願いします?アプリを最初から作り直す気はありません

– アヴィナシュ・ジャー

2020 年 9 月 3 日 11:09

1

前のコメントを拝見しました: String val = snapshot.child("childName").getVa を試してくださいlue.toString();それを印刷して、データが得られるかどうかを確認してください。

– ジャガー

2020 年 9 月 3 日 15:09



------------------------

実際の問題は Firebase データベースにありました

下の画像を Firebase データベースで確認してください

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。