-1

So I have the following piece of code, the code trys to add data to a database and if can't it closes the app. No matter what I do the app just closes, any advice

package net.connormccarthy.walkingdeadcharacterprofiles;

import java.util.ArrayList;


import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class rickAddNotes extends Activity
{

SQLiteDatabase db;




    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // this try catch block returns better error reporting to the log
            // Android specific calls
            super.onCreate(savedInstanceState);
            setContentView(R.layout.ricknotes);
            Button  btnLeft=(Button)findViewById(R.id.BtnRickSave);


            // create the database manager object
            //db = new MainActivity();


            final Button button = (Button) findViewById(R.id.BtnRickSave);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    data();
                }
            });      


    }


    public long data()
    {
        long d=0;
      //EditText edittext1=(EditText )findViewById(R.id.editText1);
      //String notes = edittext1.toString();


try {

        final String Insert_Data="INSERT INTO Characters VALUES(2,'WOOP','5')";
        db.execSQL(Insert_Data);
    } catch (Exception e) {

        System.exit(0);

    }
    return d;

    }


    public void showdata(View view)
    {
        Cursor c=db.rawQuery("SELECT * from Character WHERE character = rick", null);
         int count= c.getCount();
        c.moveToFirst();
        TableLayout tableLayout = new TableLayout(getApplicationContext());
        tableLayout.setVerticalScrollBarEnabled(true);
       TableRow tableRow;
       TextView textView,textView1,textView2,textView3,textView4,textView5;
       tableRow = new TableRow(getApplicationContext());
       textView=new TextView(getApplicationContext());
       textView.setText("Firstname");
       textView.setTextColor(Color.RED);
        textView.setTypeface(null, Typeface.BOLD);
         textView.setPadding(20, 20, 20, 20);
        tableRow.addView(textView);
        textView4=new TextView(getApplicationContext());
        textView4.setText("LastName");
        textView4.setTextColor(Color.RED);
        textView4.setTypeface(null, Typeface.BOLD);
         textView4.setPadding(20, 20, 20, 20);
        tableRow.addView(textView4);
        textView5=new TextView(getApplicationContext());
        textView5.setText("Email");
        textView5.setTextColor(Color.RED);
        textView5.setTypeface(null, Typeface.BOLD);
        textView5.setPadding(20, 20, 20, 20);
        tableRow.addView(textView5);
       tableLayout.addView(tableRow);
         for (Integer j = 0; j < count; j++)
         {
             tableRow = new TableRow(getApplicationContext());
             textView1 = new TextView(getApplicationContext());
             textView1.setText(c.getString(c.getColumnIndex("character")));
             textView2 = new TextView(getApplicationContext());
             textView2.setText(c.getString(c.getColumnIndex("notes")));
             textView1.setPadding(20, 20, 20, 20);
             textView2.setPadding(20, 20, 20, 20);
             tableRow.addView(textView1);
             tableRow.addView(textView2);
             tableLayout.addView(tableRow);
             c.moveToNext() ;
         }
         setContentView(tableLayout);
    db.close();
    }





}

If I dont add the exception the app just crashes. I want to ultimately get it to work with a variable I pass to it (editText1) but even when I hard code data the app just crashes or closes, depending on weather or not I add the exception.

Any help is greatly appreciated.

Shiv
  • 4,569
  • 4
  • 25
  • 39
ConnorM
  • 9
  • 1
  • 7
  • 1
    Please format your code properly so we don't have to scroll around and post the stack trace from logcat. – Simon Apr 01 '13 at 11:06
  • first you are not initializing `db` instance of `SQLiteDatabase` before performing DB operations and second add logcat result with question to get more help – ρяσѕρєя K Apr 01 '13 at 11:14

1 Answers1

0

in your sql statement, we are viewing 2 types of probable errors -

1>

final String Insert_Data="INSERT INTO Characters VALUES(2,'WOOP','5')";

Here your table name is Characters

where as in

Cursor c=db.rawQuery("SELECT * from Character WHERE character = rick", null);

your table name is Character

Please check this.

2> Your select query should be -

Cursor c=db.rawQuery("SELECT * from Character WHERE character = rick", null);

should be -

Cursor c=db.rawQuery("SELECT * from Character WHERE character = 'rick'", null);

Please look at these 2 pointers. We will help you for any further comments.

Thanks

Piyas De
  • 1,786
  • 15
  • 27
  • Thanks for your response! I have changed those but the app still exits when I press the button to run the function. – ConnorM Apr 01 '13 at 11:22
  • 2
    @ConnorMcCarthy Why you ignoring requests to post the exception stack trace from logcat? – Simon Apr 01 '13 at 11:38
  • Sorry I'm not sure what that means. Bit of a newbie to Android. :) – ConnorM Apr 01 '13 at 11:40
  • @ConnorMcCarthy : where you are initializing `db` ? – ρяσѕρєя K Apr 01 '13 at 11:41
  • I'm initializing it in MainActivity.java. I am trying to make it so that a user can enter notes on a character (each character has their own class) – ConnorM Apr 01 '13 at 11:44
  • @ConnorMcCarthy If you don't know, then you should ask. http://www.connect-utb.com/2012/08/understanding-android-stack-traces/ – Simon Apr 01 '13 at 11:58
  • Hi, I've read that article but I'm not sure what I should be looking for in the debugging profile – ConnorM Apr 01 '13 at 12:13
  • Are you using Eclipse, IDEA or some other IDE? – Simon Apr 01 '13 at 12:16
  • I am using the Eclipse IDE – ConnorM Apr 01 '13 at 12:18
  • http://stackoverflow.com/questions/3280051/how-to-enable-logcat-console-in-eclipse-for-android – Simon Apr 01 '13 at 12:21
  • This is the last thing I got when I filtered for "Exception" and ran the app. 04-01 13:33:45.430: W/InputManagerService(7177): Got RemoteException sending setActive(false) notification to pid 19091 uid 10053 – ConnorM Apr 01 '13 at 12:35