You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio项目连接Firebase失败求助:无代码报错却无法运行

问题:Android Studio接入Firebase后代码无报错但应用无法运行

我尝试为我的Android Studio应用接入Firebase,代码没有报错,但应用无法正常运行。已在AndroidManifest.xml中添加网络权限,以下是相关代码及Gradle配置:

1. ReportActivity 代码

package com.example.rwn;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class ReportActivity extends AppCompatActivity {

    private EditText editTextName, editTextBinLocation, editTextProblem;
    private Button buttonSubmit;
    private DatabaseReference databaseReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_report);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // Initialize Firebase Database
        databaseReference = FirebaseDatabase.getInstance().getReference("reports");

        // Initialize UI elements
        editTextName = findViewById(R.id.editTextName);
        editTextBinLocation = findViewById(R.id.editTextBinLocation);
        editTextProblem = findViewById(R.id.editTextProblem);
        buttonSubmit = findViewById(R.id.buttonSubmit);

        // Set onClick listener for the submit button
        buttonSubmit.setOnClickListener(v -> submitReport());
    }

    private void submitReport() {
        String name = editTextName.getText().toString().trim();
        String binLocation = editTextBinLocation.getText().toString().trim();
        String problem = editTextProblem.getText().toString().trim();

        if (name.isEmpty() || binLocation.isEmpty() || problem.isEmpty()) {
            Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show();
            return;
        }

        String id = databaseReference.push().getKey();
        Report report = new Report(id, name, binLocation, problem);

        assert id != null;
        databaseReference.child(id).setValue(report)
                .addOnSuccessListener(aVoid -> {
                    Toast.makeText(this, "Report submitted", Toast.LENGTH_SHORT).show();
                    // Clear fields after submission
                    editTextName.setText("");
                    editTextBinLocation.setText("");
                    editTextProblem.setText("");
                })
                .addOnFailureListener(e -> Toast.makeText(this, "Failed to submit report", Toast.LENGTH_SHORT).show());
    }
}

2. Report 实体类代码

package com.example.rwn;

public class Report {
    private String id;
    private String name;
    private String binLocation;
    private String problem;

    public Report() {
        // Default constructor required for calls to DataSnapshot.getValue(Report.class)
    }

    public Report(String id, String name, String binLocation, String problem) {
        this.id = id;
        this.name = name;
        this.binLocation = binLocation;
        this.problem = problem;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getBinLocation() {
        return binLocation;
    }

    public String getProblem() {
        return problem;
    }
}

3. 项目级 Gradle 配置

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:8.0.0") // Use the latest stable version
        classpath("com.google.gms:google-services:4.3.10")
    }
}

plugins {
    // Use the latest stable version of the Android Application plugin and Google Services plugin
    id("com.android.application") version "8.0.0" apply false
    id("com.google.gms.google-services") version "4.3.10" apply false
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

4. 应用级 Gradle 配置

plugins {
    id("com.android.application")
    id("com.google.gms.google-services")
}

android {
    namespace = "com.example.rwn"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.rwn"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled = true
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:33.0.0"))
    implementation("androidx.multidex:multidex:2.0.1")
    implementation("androidx.appcompat:appcompat:1.4.0")
    implementation("com.google.android.material:material:1.4.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.3")
    implementation("androidx.activity:activity:1.4.0")
    implementation("com.google.firebase:firebase-analytics")
    implementation("com.google.firebase:firebase-database")
    implementation("com.github.denzcoskun:ImageSlideShow:0.0.6")
    implementation("com.github.PhilJay:MPAndroidChart:v3.1.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.3")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
}

内容的提问来源于stack exchange,提问作者darwish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 17:15:57