我尝试在我的应用程序中将连接 firebase 链接到 android studio。但是,即使没有编码错误,我似乎也无法运行该应用程序。我将提供下面的代码。
下面是java代码。
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());
}
}
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;
}
}
让我知道我是否需要提供更多上下文/代码
我修改了gradle代码以满足我的项目需求。我还在manifest.xml文件中添加了一行用于连接互联网,下面是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()
}
}
(应用程序级别)
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")
}
您能否详细说明一下您的问题,因为您的代码看起来没问题,也许IDE有问题。 以下是一些可能对您有帮助的解决方案: