Stackoverflowerror when calling a class

Asked

Viewed 84 times

1

public class MainActivity extends ActionBarActivity{

Conexao c = new Conexao();

public void tela(){

  c.CriaBanco();


  }
}

public class Conexao extends MainActivity{
  String NomeBanco = "Cadastro";
  SQLiteDatabase BancoDados = null;

  public void CriaBanco(){
        try{
            BancoDados = openOrCreateDatabase(NomeBanco, MODE_WORLD_READABLE, null);
            String SQL = "CREATE TABLE IF NOT EXIST tabCadastro ( _id INTEGER PRIMARY KEY, nome TEXT, telefone TEXT) ";
            BancoDados.execSQL(SQL);
            MensagemAlerta("Banco de Dados", "Banco Criado com Sucesso");
        }catch(Exception erro){
            MensagemAlerta("Erro Banco de Dados", "Não foi possivel criar o Banco" + erro);
        }
        finally {
            BancoDados.close();
        }
    }
    public void MensagemAlerta(String TituloAlerta, String MensagemAlerta){
    AlertDialog.Builder Mensagem = new AlertDialog.Builder(MainActivity.this);
    Mensagem.setTitle(TituloAlerta);
    Mensagem.setMessage(MensagemAlerta);
    Mensagem.setNeutralButton("Ok", null);
    Mensagem.show();

}



}

When calling a class on android gives this error:

07-02 18:50:18.090 21687-21687/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
07-02 18:50:18.090 21687-21687/? E/android.os.Debug﹕ failed to load memtrack module: -2
07-02 18:50:18.160 21687-21695/? E/cutils-trace﹕ Error opening trace file: No such file or directory (2)
07-02 18:50:18.800 21703-21703/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
07-02 18:50:18.800 21703-21703/? E/android.os.Debug﹕ failed to load memtrack module: -2
07-02 18:50:21.450 21713-21713/? E/AndroidRuntime﹕ in writeCrashedAppName, pkgName :com.example.gabrielbonatto.oficial
07-02 18:50:22.070 21713-21713/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.gabrielbonatto.oficial, PID: 21713
java.lang.StackOverflowError
  • Post more of your code, only with this you can not know for sure what it is. But whatever it is, gives a StackOverflowError which is what happens when you have an infinite or very deep recursion. In particular, what was the class of Android you called? Called how? What is the class Conexao and the method CriaBanco()?

  • edited the code

  • 1

    What is the class BancoDados, the method execSQL(String) and the class MensagemAlerta? What is NomeBanco?

  • @Gabrielsantanabonatto would be interesting to rename the attributes and methods with the first lowercase letter to make it easier to read the code.

  • Solving Stackoverflow is up to us :P

  • When I first saw the question I thought it was a mistake on the site...

Show 1 more comment

2 answers

3


Your class conexão extend MainActivity. When a new class instance Conexão is created, she has a member Conexão, inherited from MainActivity. That member, when created, will contain another member Conexão, so on and so forth.

You end up creating infinite instances of Connection, one inside the other. Infinite? No, because the stack pops before, and that’s the mistake you get.

-1

As in your mainactivity you "extends" of Actionbaractivty you need to implement the method onCreate

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         // Seu código aqui que chama o Conexao
    }

Done this in your Connected Class, you can remove and extend Mainactvity and leave it as follows:

    public class Conexao {
        public void criaBanco(){
        // Seu código de criação do banco
        }
    }

Browser other questions tagged

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