1. Are you ready for the Galaxy S20? Here is everything we know so far!

App crash after trying to retrieve data from sqlite

Discussion in 'Android Development' started by iliji, Mar 24, 2021.

  1. iliji

    iliji Lurker
    Thread Starter

    Hi guys, I'm currently facing a problem, after inserting data on activity, after pressing the send button, another activity is loaded on which the most recent data stored in the database is loaded in displayed in a TextView.
    But each time I pressed the send button, when the app redirect me to the next activity it crashed and I dunno why. If you could help me, I thank you in advance.

    Below the DBHelper.java

    Code (Java):
    1. public Cursor getDataSoilData(String username){
    2.         SQLiteDatabase db = this.getReadableDatabase();
    3.         Cursor cursor = db.rawQuery("Select pH, OCarbon, nitrogen, phosphorus, potassium, zinc, temparature, date from soilData where username = ? ORDER BY id DESC LIMIT 1", new String[] {username}, null);
    4.         return cursor;
    5.     };
    Below the activity that is fetching the data
    Code (Java):
    1. public class StatusActivity extends AppCompatActivity {
    2.  
    3.  
    4.     @Override
    5.     protected void onCreate(Bundle savedInstanceState) {
    6.         super.onCreate(savedInstanceState);
    7.         setContentView(R.layout.activity_status);
    8.  
    9.         Button viewData = (Button)findViewById(R.id.submitBtn);
    10.         TextView pHLevel = (TextView)findViewById(R.id.phLevel);
    11.         TextView organicCLevel = (TextView)findViewById(R.id.organicCLevel);
    12.         TextView nitrogenLevel = (TextView)findViewById(R.id.nitrogenlevel);
    13.         TextView phosphorusLevel = (TextView)findViewById(R.id.phosphorusLevel);
    14.         TextView potassiumLevel = (TextView)findViewById(R.id.potassiumLevel);
    15.         TextView  zincLevel = (TextView)findViewById(R.id.zincLevel);
    16.         TextView temperatureLevel = (TextView)findViewById(R.id.temparatureLevel);
    17.         TextView date1 = (TextView)findViewById(R.id.date);
    18.         String tempMessage;
    19.         String pHMessage;
    20.         String organicCMessage;
    21.         String nitrogenMessage;
    22.         String phosphorusMessage;
    23.         String potassiumMessage;
    24.         String zincMessage;
    25.         DBHelper db = new DBHelper(this);
    26.         String username = getIntent().getStringExtra("username");
    27.         Cursor cursor = db.getDataSoilData(username);
    28.         StringBuilder pH = new StringBuilder();
    29.         StringBuilder organicC1 = new StringBuilder();
    30.         StringBuilder nitrogen1 = new StringBuilder();
    31.         StringBuilder phosphorus1 = new StringBuilder();
    32.         StringBuilder potassium1 = new StringBuilder();
    33.         StringBuilder zinc1 = new StringBuilder();
    34.         StringBuilder temp = new StringBuilder();
    35.         StringBuilder date = new StringBuilder();;
    36.         while(cursor.moveToNext()){
    37.             pH.append(cursor.getDouble(2));
    38.             organicC1.append(cursor.getDouble(3));
    39.             nitrogen1.append(cursor.getDouble(4));
    40.             phosphorus1.append(cursor.getDouble(5));
    41.             potassium1.append(cursor.getDouble(6));
    42.             zinc1.append(cursor.getDouble(7));
    43.             temp.append(cursor.getDouble(8));
    44.             date.append(cursor.getString(9));
    45.         }
     



    1. Download the Forums for Android™ app!


      Download

       
  2. iliji

    iliji Lurker
    Thread Starter

    Solved by myself, one whole day wasted due to a typo in DPHelper. I mistakenly write temparature in the request while the column name is temperature
     
    mikedt likes this.
Loading...

Share This Page

Loading...