2017-06-17 2 views
0

Je développe une application de nutrition dont j'ai besoin pour enregistrer les noms des aliments et leurs calories dans la base de données. J'utilise Sugar ORM comme outil de base de données sqlite. Lorsque j'essaie de sauvegarder un nouvel enregistrement, l'application cesse de fonctionner, mais ne me donne aucun message d'erreur! Je suis nouveau android si s'il vous plaît ayez pitié Voici mon code Java pour la classe d'entité:L'application cesse de fonctionner lorsque j'essaie d'insérer deux champs à l'aide de Sugar ORM

import com.orm.SugarRecord; 
import com.orm.dsl.Column; 
import com.orm.dsl.Table; 

@Table(name = "foods_name") 
public class Foods extends SugarRecord{ 

@Column(name = "food_name") 
private String foodName; 

@Column(name = "calorie") 
private String calorie; 

public Foods() { 
    this.foodName = foodName; 
    this.calorie = calorie; 
} 

public Foods(String foodName, String calorie) { 

    this.foodName = foodName; 
    this.calorie = calorie; 
} 
} 

et voici quand j'invoque cette classe:

 switch(item.getItemId()) { 
     case R.id.insert: 
      Dialog dialog = new Dialog(Dashboard.this); 
      dialog.setContentView(R.layout.insert); 
      dialog.setCanceledOnTouchOutside(true); 
      dialog.setTitle("Add a Food"); 
      final EditText foodName = (EditText) dialog.findViewById(R.id.insertFood_txt); 
      final EditText calorieName = (EditText) dialog.findViewById(R.id.calorie_txt); 
      Button insert_btn = (Button) dialog.findViewById(R.id.insertFood_btn); 
      insert_btn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        String food_name = foodName.getText().toString().trim(); 
        String calorie = calorieName.getText().toString().trim(); 
        foods = new Foods(food_name,calorie); 
        foods.save(); 
        Toast.makeText(getApplicationContext(),"Food Added Successfully",Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      dialog.show(); 
      break; 

Ma mise en page XML pour l'insertion de données est comme suit:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/insert"> 

    <EditText 
     android:id="@+id/insertFood_txt" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="Food Name" 
     android:inputType="textPersonName" /> 

    <EditText 
     android:id="@+id/calorie_txt" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="Calorie" 
     android:inputType="numberDecimal" /> 

    <Button 
     android:id="@+id/insertFood_btn" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Add Food" /> 
</LinearLayout> 

à la fin voici mon fichier manifeste:

<?xml version="1.0" encoding="utf-8"?> 

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

    android:name="com.orm.SugarApp" > <!--Use this attribute verbatim--> 
    <meta-data android:name="DATABASE" android:value="nutrition.db" /> 
    <meta-data android:name="VERSION" android:value="1" /> 
    <meta-data android:name="QUERY_LOG" android:value="true" /> 
    <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example.niloufar.nutrition3.dataModel" /> 

    <activity android:name=".Dashboard"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".BmiActivity" /> 
    <activity android:name=".IdealWeight" /> 
    <activity android:name=".GoalWeight"></activity> 
</application> 

Mon Android Moniteur Log:

`06-18 02:43:26.874 1009-1009/? I/art: Not late-enabling -Xcheck:jni (already on) 
06-18 02:43:26.874 1009-1009/? W/art: Unexpected CPU variant for X86 using defaults: x86_64 
06-18 02:43:26.890 1009-1016/? I/art: Debugger is no longer active 
06-18 02:43:26.890 1009-1016/? I/art: Starting a blocking GC Instrumentation 
06-18 02:43:26.961 1009-1009/? W/System: ClassLoader referenced unknown path: /data/app/com.example.niloufar.nutrition3-2/lib/x86_64 
06-18 02:43:26.969 1009-1009/? I/InstantRun: starting instant run server: is main process 
06-18 02:43:27.015 1009-1009/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
06-18 02:43:27.163 1009-1025/? I/OpenGLRenderer: Initialized EGL, version 1.4 
06-18 02:43:27.163 1009-1025/? D/OpenGLRenderer: Swap behavior 1 
06-18 02:43:27.163 1009-1025/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 
06-18 02:43:27.164 1009-1025/? D/OpenGLRenderer: Swap behavior 0 
06-18 02:43:27.182 1009-1009/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 

` After Adding the food

Répondre

0

Je trouve la question. L'exécution instantanée doit être désactivée en premier. Ensuite, j'ai augmenté la version de la base de données dans le fichier Manifest et l'application a fonctionné.

1

avez-vous mettre à jour votre base de données avant de le désinstaller de votre appareil? Si vous mettez à jour votre base de données et ne pas augmenter la version de base de données dans votre manifeste votre application se bloque,

 <meta-data 
     android:name="DATABASE" 
     android:value="your_database_name" /> 
    <meta-data 
     android:name="VERSION" 
     android:value="2" /> 
    <meta-data 
     android:name="QUERY_LOG" 
     android:value="true" /> 
    <meta-data 
     android:name="DOMAIN_PACKAGE_NAME" 
     android:value="your_package_name" /> 
+0

L'a-t-il et le problème existe toujours –

+0

pourriez-vous s'il vous plaît me donner l'erreur dans l'onglet du moniteur Android au fond du studio android? – Meikiem

+0

Il n'y a pas d'erreur là-bas! qu'est-ce qu'il y a ceci: Connecté au processus 835 sur le périphérique Slim_Shady_API_24 [émulateur-5554] Application terminée. –