Null Object Reference when opening a Dialog Fragment

0

You are making a mistake in opening a Dialog Fragment in a other Activity using a method .

ERROR:

  

07-06 11: 21: 25,145 18783-18783 / com.vuforia.samples.Books   E / AndroidRuntime: FATAL EXCEPTION: main                                                                              Process: com.vuforia.samples.Books, PID: 18783                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'void   com.vuforia.samples.Books.ui.ActivityFragmentList.GPSFragmentActivity.exhibitGPSFragmentDialog () '   on a null object reference                                                                                  at   com.vuforia.samples.Books.ui.ActivityList.AboutScreen $ 1.onClick (AboutScreen.java:51)                                                                                  at android.view.View.performClick (View.java:5723)                                                                                  at android.view.View $ PerformClick.run (View.java:22689)                                                                                  at android.os.Handler.handleCallback (Handler.java:836)                                                                                  at android.os.Handler.dispatchMessage (Handler.java:103)                                                                                  at android.os.Looper.loop (Looper.java:203)                                                                                  at android.app.ActivityThread.main (ActivityThread.java:6364)                                                                                  at java.lang.reflect.Method.invoke (Native Method)                                                                                  at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1063)                                                                                  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:924)

GPSFragmentActivity.java

package com.vuforia.samples.Books.ui.ActivityFragmentList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.LayoutInflaterCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.vuforia.samples.Books.R;
import com.vuforia.samples.Books.ui.ActivityList.AboutScreen;

/**
 * Created by th on 05/07/17.
 */

public class GPSFragmentActivity extends DialogFragment {

    private TextView tvShoppingNome;
    private TextView tvShoppingLicenseKey;
    private TextView tvShoppingAccessKey;
    private TextView tvShoppingSecretKey;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        // Cria o objeto para configurar o AlertDialog
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        // Define o Layout do AlertDialog
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.gps_fragment, null);
        builder.setView(dialogView);

        // Define o Layout Personalizado do AlertDialog
        builder.setTitle("GEOLOCALIZAÇÃO");

        /*tvShoppingNome = (TextView)dialogView.findViewById(R.id.txtShoppingNome);
        tvShoppingLicenseKey = (TextView)dialogView.findViewById(R.id.txtShoppingLicenseKey);
        tvShoppingAccessKey = (TextView)dialogView.findViewById(R.id.txtShoppingAccessKey);
        tvShoppingSecretKey = (TextView)dialogView.findViewById(R.id.txtShoppingSecretKey);*/

        builder.setNegativeButton("LOCALIZAR", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Fecha o AlertDialog
                dismiss();
            }
        });

        builder.setPositiveButton("CONFIRMAR", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        // Cria o Objeto AlertDialog
        return builder.create();
    }

    public void exibirGPSFragmentDialog(){
        DialogFragment gpsFragmentDialog = new GPSFragmentActivity();
        gpsFragmentDialog.show(getFragmentManager(), "gpsFragmentDialog");
    }
}

AboutScreen.java

    package com.vuforia.samples.Books.ui.ActivityList;


    import com.vuforia.samples.Books.R;
    import com.vuforia.samples.Books.ui.ActivityFragmentList.GPSFragmentActivity;

    import android.os.Bundle;;
    import android.support.v4.app.FragmentActivity;
    import android.view.View;
    import android.widget.Button;
    ;


    public class AboutScreen extends FragmentActivity {

        // Classes
        private GPSFragmentActivity gpsFragmentActivity;

        // Botões
        private Button btnLocalizar;

        private String mClassToLaunch;
        private String mClassToLaunchPackage;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.about_screen);

            Bundle extras = getIntent().getExtras();
            mClassToLaunchPackage = getPackageName();
            mClassToLaunch = mClassToLaunchPackage + "." + extras.getString("ACTIVITY_TO_LAUNCH");

gpsFragmentActivity = new GPSFragmentActivity();

            // Botão Localizar
            this.btnLocalizar = (Button) findViewById(R.id.btn_Localizar);
            this.btnLocalizar.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    gpsFragmentActivity.exibirGPSFragmentDialog();

                }
            });


        }
    }
    
asked by anonymous 06.07.2017 / 16:28

2 answers

0

You did not initialize the GPSFragmentActivity gpsFragmentActivity object anywhere in the AboutScreen class code.

    
06.07.2017 / 16:48
0

I circumvented the error as follows:

1) I removed the displayGPSFragmentDialog () function from Class GPSFragmentActivity

2) I inserted this function into MainActivity (AboutScreen) as follows:

public void exibirGPSFragmentDialog(){
    GPSFragmentActivity gpsFragmentDialog = new GPSFragmentActivity();
    gpsFragmentDialog.show(getSupportFragmentManager(), "gpsFragmentDialog");
}

3) Then I changed the click of the Button as follows:

// Botão Localizar
this.btnLocalizar = (Button) findViewById(R.id.btn_Localizar);
this.btnLocalizar.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
        exibirGPSFragmentDialog();

    }
});
    
06.07.2017 / 17:15