After Sqlite my application stopped working

Asked

Viewed 297 times

0

I’m developing an app and needed to put the database SQLite, after I put in when I will open the application it gives an error showing that the application stopped working, I asked my teacher of programming he commented that it is an error that happens only at the time of the onCreate of activity because the rest of the programming is right, he’s just not creating the onCreate....

The code I think is making a mistake would be this part:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_futebol_simples);

and my complete code and database is this:

public class FutebolSimples extends AppCompatActivity {

    private ImageButton imgButton_play, imgButton_pause, imgButton_1, imgButton_2;
    private Button vermelho, amarelo;
    private TextView txt_valor1, txt_valor2;
    private EditText nomeTime1, nomeTime2;
    private int contador = 0;
    private int contador1 = 0;
    private Chronometer reloginho;
    long tempoPausado = 0;

    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_futebol_simples);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        imgButton_1 = (ImageButton) findViewById(R.id.imgButton_1);
        imgButton_2 = (ImageButton) findViewById(R.id.imgButton_2);
        imgButton_play = (ImageButton) findViewById(R.id.imgButton_play);
        imgButton_pause = (ImageButton) findViewById(R.id.imgButton_pause);
        reloginho = (Chronometer) findViewById(R.id.chronometer);
        txt_valor1 = (TextView) findViewById(R.id.txt_valor1);
        txt_valor2 = (TextView) findViewById(R.id.txt_valor2);
        nomeTime1 = (EditText) findViewById(R.id.lbl_time1);
        nomeTime2 = (EditText) findViewById(R.id.lbl_time2);
        vermelho = (Button) findViewById(R.id.btnvermelho);
        amarelo = (Button) findViewById(R.id.btnAmarelo);




        imgButton_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador++;
                txt_valor1.setText(" " + contador);
                Toast.makeText(getApplicationContext(), "Goooool!!!", Toast.LENGTH_LONG).show();
            }
        });

        imgButton_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador1++;
                txt_valor2.setText(" " + contador1);
                Toast.makeText(getApplicationContext(), "Goooool!!!", Toast.LENGTH_LONG).show();
            }
        });

        imgButton_play.setEnabled(true);
        imgButton_pause.setEnabled(false);
        imgButton_1.setEnabled(false);
        imgButton_2.setEnabled(false);


        imgButton_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton_play.setEnabled(false);
                imgButton_pause.setEnabled(true);
                imgButton_1.setEnabled(true);
                imgButton_2.setEnabled(true);

                reloginho.setBase(SystemClock.elapsedRealtime());
                reloginho.start();
                reloginho.setBase(SystemClock.elapsedRealtime() + tempoPausado);
            }
        });

        imgButton_pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton_play.setEnabled(true);
                imgButton_pause.setEnabled(false);
                imgButton_1.setEnabled(false);
                imgButton_2.setEnabled(false);

                tempoPausado = reloginho.getBase();
                SystemClock.elapsedRealtime();
                reloginho.stop();

            }
        });

        db = openOrCreateDatabase("Resultado", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS futebol (TimeOne VARCHAR, TimeTwo VARCHAR, FinalOne INT, FinalTwo INT);");

    }


    public void saveR (View view){

        if(txt_valor1.getText().toString().trim().length()==0 || txt_valor2.getText().toString().trim().length()==0 || nomeTime1.getText().toString().trim().length()==0 || nomeTime2.toString().trim().length()==0 || vermelho.getText().toString().trim().length()==0 || amarelo.getText().toString().trim().length()==0){

            Toast.makeText(getApplicationContext(), "Por favor inicia uma partida!", Toast.LENGTH_SHORT).show();

            return;
        }

        db.execSQL("INSERT INTO futebol VALUES('"+txt_valor1.getText()+"','"+txt_valor2.getText()+"','"+nomeTime1.getText()+"','"+nomeTime2.getText()+"','"+vermelho.getText()+"','"+amarelo.getText()+"');");

        Toast.makeText(getApplicationContext(), "Partida Salva", Toast.LENGTH_LONG).show();
    }

    public void listar (View view){
        Cursor c = db.rawQuery("SELECT * FROM futebol", null);

        if (c.getCount() == 0){
            Toast.makeText(getApplicationContext(), "Erro na pesquisa!", Toast.LENGTH_SHORT).show();
            return;
        }

        StringBuffer buffer = new StringBuffer();
        while (c.moveToFirst()) {

            buffer.append("Nome: "+c.getString(0)+"\n");
        }

        Toast.makeText(getApplicationContext(), "Resultado com sucesso!", Toast.LENGTH_SHORT);
    }
 }

The error message that appears when I click on the button to call this Activity is "Allsports Beta app stopped." click Ok and it goes back to the application’s home screen.

If anyone can help me I’ll be very grateful....

  • Put the mistake you get!!

1 answer

1

You are not initiating the bank and already making a request.

For example:

db = new DBHelper(this);

Dbhelper class:

public class DBHelper extends SQLiteOpenHelper {
   public DBHelper(){
      super(context,DATABASE_NAME,null,1);
   }
   public void onCreate(SQLiteDatabase db) {}
   public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {}
}

Ai you define name of the database and version it is currently.

  • But when I created this bank I did not create a class for Dbhelper, I simply put it into onCreate I would need to create a class?

  • Yes, you need this class, I edited my answer with an example, from a look.

  • Ahh... all right I’ll take a look, thanks.

  • db = new Dbhelper(this) stays on the right onCreate?

  • Friend, I did so put the Dbhelper code in the same java code and put db = new Dbhelper(this); and is giving error I don’t know why... it is giving error in the context, and Dbhelper says it is not being used.

  • Create a separate class to be the Dbhelpder, and it matters in its class, it is better.

  • how I would import the class into the project, try import.android.java.Dbhelper; and it’s not going

  • Leave the mouse on top of where you want to import, and press the ALT + ENTER keys that it searches exactly where the class is and does the import, that if you are using Windows, if it is on Mac uses the shortcut CMD + 1

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.