APK continuously crashes

Asked

Viewed 861 times

0

My APK opens correctly when the code is this:

 package com.example.wheresmybusdriver;

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

public class Localizacao extends AppCompatActivity {

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

    public void sairLogin(View view){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}

But when I add to the code:

TextView statusLocation = (TextView)findViewById(R.id.textStatus);

    public void informeStatusSim(View view) {
        statusLocation.setText("Sua localização esta sendo transmitida!");
    }

    public void informeStatusNao(View view) {
        statusLocation.setText("Sua localização não esta sendo transmitida!");
    }

Thus remaining the result:

package com.example.wheresmybusdriver;

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

public class Localizacao extends AppCompatActivity {

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

    public void sairLogin(View view){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    TextView statusLocation = (TextView)findViewById(R.id.textStatus);

    public void informeStatusSim(View view) {
        statusLocation.setText("Sua localização esta sendo transmitida!");
    }

    public void informeStatusNao(View view) {
        statusLocation.setText("Sua localização não esta sendo transmitida!");
    }
}

apk does not open and the message appears ("apk" shows faults continuously). I find it strange that when I enter my apk, I go to his homepage, so it works correctly, but when I click on the button to enter Activity with the above code, it stops working.

1 answer

2


You are looking for a layout element outside any of your Activity method Localizacao, causing the code to be executed before you set the layout with setContent:

TextView statusLocation = (TextView)findViewById(R.id.textStatus);

To solve, you must make the declaration of that variable outside (as a field of your class) and the assignment of value in the onCreate, for example.

public class Localizacao extends AppCompatActivity {
    TextView statusLocation;

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

        // Uma vez que você já indicou qual é o layout com "setContent", você pode procurar
        // pelo elemento "textStatus"
        statusLocation = (TextView)findViewById(R.id.textStatus);
    }

    //...
}


By the way, "apk" apresenta falhas continuamente is not your error message. To find the error message see the Logcat of Android Studio. Once you are reading the correct error message, it will become clearer what you should seek to understand to fix the issue.

"apk" apresenta falhas continuamente is a message that appears on Android when the user opens an application that breaking (crash) continually. If you open any application that occurs this issue, this message will appear, it doesn’t need to be an application of yours or an application in development.

Browser other questions tagged

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