2

main Activity

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class principal extends AppCompatActivity {
    Button login, cadastrar, about;

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

        login = (Button) findViewById(R.id.btn_login);
        cadastrar = (Button) findViewById(R.id.btn_cadastrar);
        about = (Button) findViewById(R.id.btn_about);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(principal.this, LoginActivity.class);
                startActivity(i);
            }
        });

        cadastrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(principal.this, CadastroActivity.class);
                startActivity(i);
            }
        });

        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), "MADE BY ZE_CAMINHAUM'S CORPORATION", Toast.LENGTH_LONG).show();
            }
        });
    }
}

cadastroActivity

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

public class CadastroActivity extends AppCompatActivity {

    EditText cad_login, cad_senha1, cad_senha2;
    Button Registrar, voltar;

    DbHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cadastro);
        init();
    }

    private void init() {

        cad_login = (EditText) findViewById(R.id.Cad_usu);
        cad_senha1 = (EditText) findViewById(R.id.Cad_senha1);
        cad_senha2 = (EditText) findViewById(R.id.Cad_senha2);

        Registrar = (Button) findViewById(R.id.btn_cadastrar);
        voltar = (Button) findViewById(R.id.btn_voltar);

        db = new DbHelper(this);

        Registrar.setOnClickListener(dbButtonListener);
        voltar.setOnClickListener(dbButtonListener);

    }
    private View.OnClickListener dbButtonListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()){

                case R.id.btn_cadastrar:
                long res = db.CriarUltilizador(-1,getValue(cad_login), getValue(cad_senha1));
                if(res == -1){
                    Toast.makeText(CadastroActivity.this, "Erro, repita o processo por favor", Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(CadastroActivity.this, "Cadastro feito com sucesso", Toast.LENGTH_SHORT).show();
                }
                break;
                case R.id.btn_voltar:
                    Intent i = new Intent(CadastroActivity.this, principal.class);
                    startActivity(i);
                break;
            }
        }
    };

    private String getValue(EditText editText) {
        return editText.getText().toString().trim();
    }

    @Override
    protected void onStart() {
        super.onStart();
        db.Opendb();
    }

    @Override
    protected void onStop() {
        super.onStop();
        db.Closedb();
    }
}

DBHelper

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by Lucas on 03/12/2017.
 */

public class DbHelper extends SQLiteOpenHelper {

    private static final String nome = "B_dados.bd";
    private static final int version = 1;
    public DbHelper(Context context) {
        super(context, nome, null, version);
    }

    private static final String TABLE_NAME = "Contas";
    private static final String Id = "id";
    private static final String Usuario = "Usuario";
    private static final String Senha = "Senha";

    SQLiteDatabase mydb;
    @Override
    public void onCreate(SQLiteDatabase db) {
        String str = "CREATE TABLE " + TABLE_NAME + "(" + 
              Id + " INTEGER PRIMARY KEY " + 
              Usuario + " TEXT NOT NULL " + 
              Senha + " TEXT NOT NULL " + 
          ")";
        db.execSQL(str);
    }

    public void Opendb(){
        mydb = getReadableDatabase();
    }
    public void Closedb(){
        if (mydb != null && mydb.isOpen()){
            mydb.close();
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long CriarUltilizador (int id, String usu, String senha) {
        ContentValues cv = new ContentValues();
        if (id != -1) {
            cv.put(Id, id);
            cv.put(Usuario, usu);
            cv.put(Senha, senha);
        }
        return mydb.insert(TABLE_NAME, null, cv);
    }
    public long Update (int id, String usu, String senha) {
        ContentValues cv = new ContentValues();
        cv.put(Id, id);
        cv.put(Usuario, usu);
        cv.put(Senha, senha);
        String WHERE = id + " = " + id;
        return mydb.update( TABLE_NAME, cv, WHERE, null);
    }

    public long delete(int id){
        String WHERE  = id + " = " + id;

        return mydb.delete(TABLE_NAME, WHERE, null);
    }

    public Cursor getALLrecords(){
        String query = "SELECT * FROM" + TABLE_NAME;
        return mydb.rawQuery(query, null);

    }

}

log de erro

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ze_caminhaumscorporation.aluguel, PID: 7874
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{ze_caminhaumscorporation.aluguel/ze_caminhaumscorporation.aluguel.CadastroActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                   Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                      at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354)
                      at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                      at ze_caminhaumscorporation.aluguel.CadastroActivity.onCreate(CadastroActivity.java:23)
                      at android.app.Activity.performCreate(Activity.java:6662)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

Estou debugando o código pelo meu próprio celular, e toda vez que chamo o cadastro, ele da erro e fecha. já refiz banco de dados, mas também não funciona.

1
  • 1
    o erro era no style, por acidente devo ter excluido o theme, então o Manifest não funcionava
    – COld
    12/12/2017 às 2:54

2 Respostas 2

0

No arquivo AndroidManifest.xml tente adicionar ou trocar a propriedade android:theme da activity que você deseja abrir. Exemplo:

AndroidManifest.xml

<activity android:name=".MainActivity"
        android:theme="@style/AppTheme">
</activity>

styles.xml

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
1
  • Poderia por favor complementar a resposta, mostrando o XML do style apontando para o AppTheme, e também indicando que este tema deve ser herdado de um tema do AppCompat. 11/12/2017 às 13:46
0

Tudo o que você precisa fazer é adicionar android:theme="@style/ Theme.AppCompat.Light" para sua activity, no arquivo AndroidManifest.xml.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .