Crashing when starts the 2nd Activity

Asked

Viewed 67 times

1

The first screen is the login, normal log, communicates with the server and such, there goes to second, there is a fragment of google maps, when it goes to second crash the app, the error that appears is this (Senai world is the name of the app) :

mundosenai.mundosenai.com.mundosenai E/AndroidRuntime: FATAL EXCEPTION: main
    Process: mundosenai.mundosenai.com.mundosenai, PID: 2551
    java.lang.NullPointerException
    at mundosenai.mundosenai.com.mundosenai.MainActivity$SolicitaDados.onPostExecute(MainActivity.java:83)
    at mundosenai.mundosenai.com.mundosenai.MainActivity$SolicitaDados.onPostExecute(MainActivity.java:73)
    at android.os.AsyncTask.finish(AsyncTask.java:632)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5072)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
    at dalvik.system.NativeStart.main(Native Method)

1ª Activity

public class MainActivity extends AppCompatActivity {

    EditText editNome,editSenha;
    Button botaoContinuar;
    TextView textoRegistrar;

    String url = "";
    String parametros ="";

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

        editNome = (EditText) findViewById(R.id.nome1Id);
        editSenha = (EditText) findViewById(R.id.senhaId);
        botaoContinuar = (Button) findViewById(R.id.botaoCancelarId);
        textoRegistrar = (TextView) findViewById(R.id.textoRegistrarId);

        textoRegistrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, RegisterActivity.class));
            }
        });

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

                ConnectivityManager connMgr = (ConnectivityManager)
                        getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isConnected()) {

                    String nome = editNome.getText().toString();
                    String senha = editSenha.getText().toString();

                    if(nome.isEmpty() || senha.isEmpty() ){
                        Toast.makeText(MainActivity.this,"Nenhum campo deve estar vazio",Toast.LENGTH_SHORT).show();
                    }else{
                        url = "http://192.168.43.40:80/Login/logar.php";

                        parametros = "nome=" + nome + "&senha=" + senha;

                        new SolicitaDados().execute(url);
                    }


                } else {
                    Toast.makeText(MainActivity.this,"Nenhuma Conexão",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private class SolicitaDados extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            return Conexao.postDados(urls[0], parametros);
        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {


            if(result.contains("login_ok")){

                String[] dados = result.split(",");

                //editNome.setText(dados[0] + " - " + dados[1] + " + " + dados[2]);


                Intent abreHome = new Intent(MainActivity.this, HomeActivity.class);
                abreHome.putExtra("id_user", dados[1]);
                //abrePost.putExtra("nome_usuario", dados[2]);
                startActivity(abreHome);
            } else {
                Toast.makeText(MainActivity.this,"Usuario ou Senha incorretos!",Toast.LENGTH_SHORT).show();
            }

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

}

2nd Actvity (the one that crashes when it starts)

public class HomeActivity extends AppCompatActivity{

    private Button botaoPostar;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        fragmentManager = getSupportFragmentManager();

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        transaction.add(R.id.container, new MapsFragment(), "MapsFragment");

        transaction.commitAllowingStateLoss();


        setContentView(R.layout.activity_home);

        botaoPostar = (Button) findViewById(R.id.botaoPostarId);

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

                Intent abrePost = new Intent(HomeActivity.this, PostActivity.class);
                String id_user = getIntent().getExtras().getString("id_user");



                String txt = "";
                txt = id_user.toString();
                Bundle bundle = new Bundle();

                bundle.putString("txt", txt);
                abrePost.putExtras(bundle);
                startActivity(abrePost);

            }
        });

    }

}

Activity do Map

public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}
  • Your problem is inside the onPostExecute, would have to see if the result or dados is null

1 answer

1

MainActivity$SolicitaDados.onPostExecute(MainActivity.java:83)

On the line 83 of MainActivity, there is an object that is null!

It is likely that the Conexao.postDados(urls[0], parametros) is returning null!

Try the following:

@Override
protected void onPostExecute(String result) {

if(result == null){
    // Deve se tratar o retorno conforme sua lógica
    Toast.makeText(MainActivity.this,”Mensagem de erro ao usuário!”,Toast.LENGTH_SHORT).show();
}else{
    if(result.contains("login_ok")){
…..
    }
}

Browser other questions tagged

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