Search code examples
javaandroidsqliteandroid-7.0-nougatsugarorm

App stops working when trying to insert two fields using Sugar ORM


I'm developing a Nutrition App which I need to save Foods names and their Calories into Database. I'm using Sugar ORM as my sqlite Database tool. When I try to save a new record the App stops working though give me no error message! I'm new to android so please have mercy Here is my Java code for Entity class:

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;
}
}

and here is when I invoke that class:

        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;

My xml layout for data insertion is as follows:

<?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>

at the end here is my manifest file:

<?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>

My Android Monitor 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


Solution

  • I found the issue. Instant Run must be disabled first. Then I increased the database version in the Manifest file and app worked.