Error while calling Activity

Asked

Viewed 347 times

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();
            }
        });
    }
}

cadastral

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);

    }

}

error log

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) 

I’m debugging the code on my own phone, and every time I call the register, it fails and closes. already refiz database, but also does not work.

  • 1

    the error was in style, by accident I must have deleted Theme, then Manifest did not work

2 answers

0

In the Androidmanifest.xml file try to add or change the property android:Theme from the Activity you want to open. Example:

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>
  • Could you please complement the answer by showing the style XML pointing to Apptheme, and also.

0

All you need to do is add android:theme="@style/ Theme.AppCompat.Light" to your Activity, in the archive AndroidManifest.xml.

Browser other questions tagged

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