0
Good evening, in my application I made a Dialogfragment I put a function to close current Activity and go back to previous, the call put in the onBackPressed()
and the code that makes this function is in the onclick of "OK"... only that it is presenting me an error in Logcat
Could someone help me?
Dialogfragment:
package com.gif.popupsair;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
public class MyDialog extends DialogFragment {
private Activity getActivity;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builderr = new AlertDialog.Builder(getActivity());
builderr.setMessage("Isso e um dialogFragment").setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getActivity.finish();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builderr.setTitle("Hello Mundo");
AlertDialog dialog = builderr.create();
return dialog;
}
}
Segunda Activity:
package com.gif.popupsair;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void AbrirDialog(View view){
Toast.makeText(getApplicationContext(), "Aperte o botao para voltar", Toast.LENGTH_SHORT).show();
}
@Override
public void onBackPressed()
{
MyDialog di = new MyDialog();
di.show(getSupportFragmentManager(), "my_dialog_tag");
}
}
Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gif.popupsair, PID: 1370
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.finish()' on a null object reference
at com.gif.popupsair.MyDialog$2.onClick(MyDialog.java:24)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Name of the first Activity: Mainactivity
Thank you...
So friend.... I created a popup that looks when you click the back button of the phone and that gives me the option to okay and Cancel... in the class at
setPostive
that would be the "OK" put the functiononDestroy();
and it’s not going, why?– Nathan
@Nathan, in class Mydialog and in the onClick() of the button OK call the method
getActivity.finish();
.– Luídne
@Luídne mine worked that way -
getActivity().finish();
... Thanks for the help.– Nathan