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

Asked

Viewed 949 times

-1

Good evening guys, I’m entering the world of mobile programming, and I have a problem. At the request of a course exercise, I created a project with a layout, an Activity and a class. The project according to Android Studio is all in compliance, but when I pass the project to my device, it installs and appears the message "the (name of my project) stopped". Since I can’t find any errors in Activity, class or layout, could you help me with that? I can’t use the emulator because the RAM of my PC is garbage, so I can only run the project by the device. The project has API 16, I have 2 devices with API 17 and 21, in 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();
}
}

Log errors from the moment that Clean 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) 
  • General rule is an exception. You have to look at the Log and see what came.

  • I put the log in the post

  • NullPointerException on line 34 of MainActivity.onCreate(MainActivity.java:34). Just follow around, see what access you make on the line 34 and realize why it’s null

  • all error logs that present an option to click, appears on top of the messages: Decompiled. class file, bytecode version: 52.0 (Java 8), and the message below: Sources for 'Android API 28 Platform (1) not found. And they all open different classes, showing the same message at the top. Only the one of line 34 q goes to the line of the class Activity q was interconnected the Edittext component by the method findViewById, and as far as I could see, there is nothing null there. I try to download this API but error.

  • Colleague @David look for information on how to ask a question, because you can’t just play the code, you need to format it.

1 answer

2


It is null yes. Look at your Edittext being declared:

EditText txtTitulo;

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

EditText txtTitulo = null;

Then you 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 method findViewById(), only that it is null, has no object. Then bursts the NullPointerException.

The correct thing is to do (no onCreate() even):

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

and repeat the same pattern on the other variables, not forgetting to make the appropriate Sts (i.e., wherever Button cast to (Button) and not to (EditText) as I did in the example).

Naturally for the variable Livros that’s not necessary.

  • The problem was almost that, it was almost all right, but instead of calling findViewById, I was supposed to match, I just needed to change the . by the =. Vlw signal by the help :)

Browser other questions tagged

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