"Unfortunately, *my app* has stopped" in the data pass to Activity

Asked

Viewed 112 times

1

I’m making an app that formats bibliographic references according to ABNT, but there’s been an error to pass the data typed by the user to the next activity, that displays the formatted data.

Follow the codes and print with the error message.

public class LivroTela extends Activity implements OnClickListener {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.campos_livro);

        setupbotaoGerarLivro(); 
    }

    private void setupbotaoGerarLivro() {
        Button btnGerarLivro = (Button) findViewById(R.id.btnGerarLivro);
        btnGerarLivro.setOnClickListener(new View.OnClickListener() {       
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LivroTela.this, FormatacaoLivro.class);
                EditText etAutor1= (EditText) findViewById(R.id.etAutor1);
                String autor1 = etAutor1.getText().toString();
                intent.putExtra("etAutor1", autor1);

                EditText etAutor2= (EditText) findViewById(R.id.etAutor2);
                String autor2 = etAutor2.getText().toString();
                intent.putExtra("etAutor2", autor2);

                EditText etAutor3= (EditText) findViewById(R.id.etAutor3);
                String autor3 = etAutor3.getText().toString();
                intent.putExtra("etAutor3", autor3);

                EditText etTitulo1= (EditText) findViewById(R.id.etTitulo1);
                String titulo1 = etTitulo1.getText().toString();
                intent.putExtra("etTitulo1", titulo1);

                EditText etEdicao= (EditText) findViewById(R.id.etEdicao);
                String edicao1 = etEdicao.getText().toString();
                intent.putExtra("etEdicao", edicao1);

                EditText etCidade= (EditText) findViewById(R.id.etCidade);
                String cidade1 = etCidade.getText().toString();
                intent.putExtra("etCidade", cidade1);

                EditText etEditora= (EditText) findViewById(R.id.etEditora);
                String editora = etEditora.getText().toString();
                intent.putExtra("etEditora", editora);

                EditText etAnoLivro= (EditText) findViewById(R.id.etAnoLivro);
                String ano = etAnoLivro.getText().toString();
                intent.putExtra("etAnoLivro", ano);

                startActivity(intent);          
            }           
        });         
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub      
    }   
}

Class FormatacaoLivro:

public class FormatacaoLivro extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.formatacao_livro);

        TextView tvFormatacaoLivro = (TextView) findViewById(R.id.tvFormatacaoLivro);
        Intent intent = getIntent();

        String autor1 = intent.getStringExtra("etAutor1");
        String[] a1= autor1.split(" ");
        String a1pronto = a1[1].toUpperCase()+","+a1[0];

        String autor2 = intent.getStringExtra("etAutor2");
        String[] a2= autor2.split(" ");
        String a2pronto = a2[1].toUpperCase()+","+a2[0];

        String autor3 = intent.getStringExtra("etAutor3");
        String[] a3= autor3.split(" ");
        String a3pronto = a3[1].toUpperCase()+"."+a3[0];

        String titulo1 = intent.getStringExtra("etTitulo1");
        String edicao1 = intent.getStringExtra("etEdicao");
        String cidade1 = intent.getStringExtra("etCidade");
        String editora = intent.getStringExtra("etEditora");
        String ano = intent.getStringExtra("etAnoLivro");

        String ref = a1pronto+";"+a2pronto+";"+a3pronto+","+titulo1+","+edicao1+","+cidade1+","+editora+","+ano;
        /*Log.i("aula", ref);*/
        tvFormatacaoLivro.setText(ref); 
    }
}

Error image: http://imgur.com/0oap5rJ

  • When this error appears, a stack of exceptions appears in your system log. Post this stack here so we can better understand the problem.

  • From console or Logcat?

  • Logcat @Felipedeaquinonascimento

  • http://pastebin.com/QSxQ9mZ

1 answer

2


It seems to me a problem with some of these accesses to index 1, as in

    String autor1 = intent.getStringExtra("etAutor1");
    String[] a1= autor1.split(" ");
    String a1pronto = a1[1].toUpperCase()+","+a1[0];

Since your mistake is:

02-01 12:02:51.219: E/AndroidRuntime(2103): java.lang.RuntimeException: Unable to start activity ComponentInfo{si.uemg.appformatadorabnt/si.uemg.appformatadorabnt.FormatacaoLivro}:
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

That means probably one of his "autorN" does not have two words, which makes the split(" ") return only one component (hence index 1 does not exist). I suggest you check these data that come from the Intent extras and make sure they are valid, or change your logic of handling these cases.

  • So I changed the logic of handling these cases, but the error persists. http://pastebin.com/iSKC0FnK

  • You can update the question by logging in the new error?

  • Of course. http://pastebin.com/yX8zsXsq

  • Your log seems to be showing errors of different times (judging by the times). Probably separate errors. Can isolate only the error that is happening now?

  • http://pastebin.com/XB2KUPuX

  • It seems to me that your mistake has changed. Now it is ComponentInfo{si.uemg.appformatadorabnt/si.uemg.appformatadorabnt.FormatacaoLivro}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference . This probably means that some of your ids to EditText or TextView are not valid or do not exist on the current screen.

  • The screen where I am setting the data http://pastebin.com/uUdLYUYz And Androidmanifest.xml are all the activities registered, I really don’t understand why you’re giving this =/////

  • Your layout is called campos_livro but you’re calling him formatacao_livro in your code.

  • So then I want to take all the content that the user type from this field_book and send the formatted information to another Activity that is this formatted_book that contains a Textview called tvFormatacaoLivro

  • But you have to pass this data through the Bundle, as you were doing in the initial code. You can’t take them directly from the other Activity with findViewById, because the ids don’t exist for the other screen.

  • How can I do this so that everything that the user typed in another Active appears? You can give me solution suggestions by code so I can visualize the problem?

  • You were already running everything through the initial code, as I understand it. The problem is that some data did not have two words, and you assumed this in your split. Check why these data are inconsistent. Your problem is the data, not the algorithm. If you have any questions about passing parameters via Bundle, create a new question about this, because we are already going to another direction here.

  • It worked here. Soon I will post on Google Play :)

  • Congratulations :) . Don’t forget to accept the answer if you have helped.

Show 9 more comments

Browser other questions tagged

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