Null Object Reference when opening a Dialog Fragment

Asked

Viewed 161 times

0

Making a mistake opening a Dialog Fragment in a another 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.displayGPSFragmentDialog()' 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();

                }
            });


        }
    }

2 answers

0

You didn’t initialize the object GPSFragmentActivity gpsFragmentActivity nowhere in the Aboutscreen class code.

  • I’ve tried doing gpsFragmentActivity = new Gpsfragmentactivity(); and it gives: android.support.v4.app.Fragmenttransaction android.support.v4.app.Fragmentmanager.beginTransaction()' on a null Object Reference

0

I bypassed the error as follows:

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

2) Insert this function in Mainactivity (Aboutscreen) as follows:

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

3) Then I changed the button click 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();

    }
});

Browser other questions tagged

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