Je crée une application qui récupère les données de Firebase, les ajoute dans un adaptateur de tableau et les affiche dans une vue de liste. Lorsque j'essaie de créer l'application, elle échoue avec:
Erreur: l'exécution a échoué pour la tâche ': app: transformClassesWithInstantRunForDebug'. Java.lang.IllegalStateException: BEGIN_ARRAY attendu mais STRING au chemin de la ligne 1 de la colonne 1 $
Mon activité:
package app.nert;
import Android.content.Intent;
import Android.content.SharedPreferences;
import Android.os.Bundle;
import Android.support.v7.app.AppCompatActivity;
import Android.view.View;
import Android.widget.ArrayAdapter;
import Android.widget.ListView;
import Android.widget.TextView;
import com.google.Android.gms.ads.AdRequest;
import com.google.Android.gms.ads.AdSize;
import com.google.Android.gms.ads.AdView;
import com.google.Android.gms.ads.MobileAds;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import Java.text.SimpleDateFormat;
import Java.util.ArrayList;
import Java.util.Calendar;
import Java.util.Locale;
public class display extends AppCompatActivity {
private AdView mAdView;
private ArrayList<String> ttable = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
MobileAds.initialize(this, "ca-app-pub-...");
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-...");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//Shared Preference declaration
SharedPreferences sprefs = getSharedPreferences("nert.app", MODE_PRIVATE);
String sid = sprefs.getString("school_id", "Not defined yet");//get schoolid
String sch = sprefs.getString("school", "Not defined yet");//get schoolname
String cl = sprefs.getString("class", "Not defined yet");// get classname
String clid = sprefs.getString("class_id", "Not defined yet");//get classid
TextView tod = findViewById(R.id.today);
TextView tom = findViewById(R.id.tom);
tod.setVisibility(View.VISIBLE);
tom.setVisibility(View.VISIBLE);
}
protected void onStart() {
super.onStart();
//Begin Shared Preferences
SharedPreferences sprefs = getSharedPreferences("nert.app", MODE_PRIVATE);
String sid = sprefs.getString("school_id", "Not defined yet");//get schoolid
String sch = sprefs.getString("school", "Not defined yet");//get schoolname
String cl = sprefs.getString("class", "Not defined yet");// get classname
String clid = sprefs.getString("class_id", "Not defined yet");//get classid
//End Shared Preferences
//Begin get Day
String weekDay;
SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.US);
Calendar calendar = Calendar.getInstance();
weekDay = dayFormat.format(calendar.getTime());
String day = String.valueOf(weekDay);
//End get Day
ListView tday = findViewById(R.id.day);
tday.setDividerHeight(0);
final ArrayAdapter tdayadapter = new ArrayAdapter(this, Android.R.layout.simple_list_item_1, ttable);
tday.setAdapter(tdayadapter);
DatabaseReference gettt = FirebaseDatabase.getInstance().getReference().child("schools").child("classes").child(sid).child(clid).child("timetable").child(day);
gettt.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
tdayadapter.add(value);
tdayadapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void homework(View view) {
Intent hw = new Intent(this, homework.class);
startActivity(hw);
}
public void replacement(View view) {
Intent rp = new Intent(this, replacement.class);
startActivity(rp);
}
public void onBackPressed() {
}
public void at(View view) {
Intent an = new Intent(this, anifrom.class);
startActivity(an);
}
public void today(View view) {
ListView lstd = findViewById(R.id.day);
ListView lstt = findViewById(R.id.tmo);
TextView tod = findViewById(R.id.today);
TextView tod2 = findViewById(R.id.today2);
TextView tom = findViewById(R.id.tom);
TextView tom2 = findViewById(R.id.tom2);
lstt.setVisibility(View.GONE);
tod2.setVisibility(View.GONE);
tom2.setVisibility(View.GONE);
lstd.setVisibility(View.VISIBLE);
tod.setVisibility(View.VISIBLE);
tom.setVisibility(View.VISIBLE);
}
public void tom(View view) {
ListView lstd = findViewById(R.id.day);
ListView lstt = findViewById(R.id.tmo);
TextView tod = findViewById(R.id.today);
TextView tod2 = findViewById(R.id.today2);
TextView tom = findViewById(R.id.tom);
TextView tom2 = findViewById(R.id.tom2);
lstd.setVisibility(View.GONE);
tod.setVisibility(View.GONE);
tom.setVisibility(View.GONE);
lstt.setVisibility(View.VISIBLE);
tod2.setVisibility(View.VISIBLE);
tom2.setVisibility(View.VISIBLE);
}
}
Et mon build.gradle
apply plugin: 'com.Android.application'
Android {
compileSdkVersion 26
defaultConfig {
applicationId "app.nert"
minSdkVersion 23
targetSdkVersion 26
versionCode 1
versionName "1.0-alpha"
testInstrumentationRunner "Android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
}
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
buildscript {
repositories {
jcenter()
maven {
url 'https://maven.fabric.io/public'
}
}
dependencies {
classpath 'io.fabric.tools:gradle:1.24.4'
}
}
dependencies {
implementation 'com.Android.support:design:26.1.0'
compile 'com.google.firebase:firebase-database:11.8.0'
compile('com.crashlytics.sdk.Android:crashlytics:2.7.1@aar') {
transitive = true
}
compile 'com.google.firebase:firebase-core:11.8.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.Android.support:appcompat-v7:26.1.0'
implementation 'com.Android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.firebase:firebase-ads:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.Android.support.test:runner:1.0.1'
androidTestImplementation 'com.Android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.google.Android.gms:play-services-ads:11.8.0'
}
apply plugin: 'io.fabric'
apply plugin: 'com.google.gms.google-services'
Comment puis-je empêcher la génération d'échouer?
Oui, c'est juste un bug dans Android Studio, essayez Build -> Clean Project
, puis cliquez sur Build -> Rebuild Project
si votre émulateur s'ouvre, fermez-le et essayez Build -> Clean Project, puis cliquez sur Build -> Rebuild Project