"The app stopped working", what can it be?

-1

Good evening guys, I'm joining the mobile programming world, and I have a problem. At the request of an exercise in my course, I created a project with a layout, an Activity and a class. The project according to Android Studio is all compliant, but when I pass the project to my device, it installs and the message "my project name has stopped" appears. Since I can not find any errors in the Activity, neither the class nor the layout, could you help me with this? I can not use the emulator because my PC's RAM is crap, so I just have to run the project on the machine. The project has the API 16, I have 2 devices with API 17 and 21, in the 2 the project does not work.

Here is the Project Activity:

TextView txtTitulo;
EditText isbn;
EditText nomeLivro;
EditText tema;
EditText editora;
EditText autor;
EditText numeroPaginas;
EditText ano;
EditText edicao;
EditText preco;

Button btnLimpar;
Button btnSalvar;

Livros objlivros;

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

    txtTitulo.findViewById(R.id.txtTitulo);
    isbn.findViewById(R.id.isbn);
    nomeLivro.findViewById(R.id.nomeLivro);
    tema.findViewById(R.id.tema);
    editora.findViewById(R.id.editora);
    autor.findViewById(R.id.autor);
    numeroPaginas.findViewById(R.id.numeroPaginas);
    ano.findViewById(R.id.ano);
    edicao.findViewById(R.id.edicao);
    preco.findViewById(R.id.preco);

    btnLimpar.findViewById(R.id.btnLimpar);
    btnSalvar.findViewById(R.id.btnSalvar);

    btnLimpar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            isbn.setText("");
            nomeLivro.setText("");
            tema.setText("");
            editora.setText("");
            autor.setText("");
            numeroPaginas.setText("");
            ano.setText("");
            edicao.setText("");
            preco.setText("");

        }
    });

    btnSalvar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            objlivros = new Livros();

            objlivros.setIsbn(isbn.getText().toString());
            objlivros.setNomeLivro(nomeLivro.getText().toString());
            objlivros.setTema(tema.getText().toString());
            objlivros.setEditora(editora.getText().toString());
            objlivros.setAutor(autor.getText().toString());
            objlivros.setNumeroPaginas(numeroPaginas.getText().toString());
            objlivros.setAno(ano.getText().toString());
            objlivros.setEdicao(edicao.getText().toString());
            objlivros.setPreco(preco.getText().toString());

            venderLivros();

            Toast.makeText(getApplicationContext(),"ISBN: ",Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(),"Nome do Livro: ",Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(),"Tema: ",Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(),"Editora: ",Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(),"Autor: ",Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(),"Número de Páginas: ",Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(),"Ano: ",Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(),"Edição: ",Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(),"Preço: ",Toast.LENGTH_LONG).show();
        }
    });

}

public void venderLivros() {

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(MainActivity.this);
    builder.setMessage("ISBN: "+objlivros.getIsbn()+"\nNome do Livro: "+objlivros.getNomeLivro()+"\nTema: "+objlivros.getTema()+"\nEditora: "+objlivros.getEditora()+"\nAutor: "+objlivros.getAutor()+"\nNúmero de Páginas: "+objlivros.getNumeroPaginas()+"\nAno: "+objlivros.getAno()+"\nEdição: "+objlivros.getEdicao()+"\nPreço: "+objlivros.getPreco())
            .setNegativeButton("Livro Vendido", null)
            .create()
            .show();
}
}

Error logs from the moment I debug the project:

07-22 20:01:27.828 13064-13064/com.dzcursoandroidvenderlivros.venderlivros E/dalvikvm: Could not find class 'android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper', referenced from method android.support.v4.view.ViewCompat.addOnUnhandledKeyEventListener
07-22 20:01:27.828 13064-13064/com.dzcursoandroidvenderlivros.venderlivros E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.view.ViewCompat.dispatchApplyWindowInsets
07-22 20:01:27.843 13064-13064/com.dzcursoandroidvenderlivros.venderlivros E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.view.ViewCompat.onApplyWindowInsets
07-22 20:01:27.843 13064-13064/com.dzcursoandroidvenderlivros.venderlivros E/dalvikvm: Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method android.support.v4.view.ViewCompat.removeOnUnhandledKeyEventListener
07-22 20:01:27.851 13064-13064/com.dzcursoandroidvenderlivros.venderlivros E/dalvikvm: Could not find class 'android.support.v4.view.ViewCompat$1', referenced from method android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener
07-22 20:01:27.882 13064-13064/com.dzcursoandroidvenderlivros.venderlivros E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
07-22 20:01:28.101 13064-13064/com.dzcursoandroidvenderlivros.venderlivros E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dzcursoandroidvenderlivros.venderlivros/com.dzcursoandroidvenderlivros.venderlivros.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
        at android.app.ActivityThread.access$700(ActivityThread.java:158)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5365)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at com.dzcursoandroidvenderlivros.venderlivros.MainActivity.onCreate(MainActivity.java:34)
        at android.app.Activity.performCreate(Activity.java:5326)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
        at android.app.ActivityThread.access$700(ActivityThread.java:158) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:5365) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
        at dalvik.system.NativeStart.main(Native Method) 
    
asked by anonymous 23.07.2018 / 00:37

1 answer

2

It's null, yes. Look at your EditText being declared:

EditText txtTitulo;

For instance variables (that is, for object variables as opposed to local variables) this is basically the same as declaring this:

EditText txtTitulo = null;

At the beginning of the% method, onCreate() tries to call txtTitulo.findViewById(R.id.txtTitulo) as if there were an object in that variable txtTitulo with a findViewById() method, except that it is null, has no object at all. Then it pops up NullPointerException .

The correct thing is to do (in onCreate() same):

txtTitulo = (EditText)findViewById(R.id.txtTitulo);

and repeat the same pattern in the other variables, not forgetting to make the appropriate casts (that is, where for example Button cast (Button) and not (EditText) as I did in the example) / p>

Of course for the variable Livros this is not necessary.

    
23.07.2018 / 02:29