-1

Actually i solved this problem but somehow after a few hours of coding now its doing the same thing. cant figure what is wrong but it crashes every time at the popup_durum is executed. The code works fine when i add it to the same class but in seperate class it crashes and i need to call this function in every page any help will be appritiated thanx advance.

  This is the class i call

public class Antreman extends AppCompatActivity {
AlertDialog.Builder dialogBuilder;
Button AntremanaBasla,Cikis;
View popup_durum;
//Pop up----------------------------------------------------------------------------------------
public void AntremanSayfasi(){

    dialogBuilder = new AlertDialog.Builder(this);
    popup_durum = getLayoutInflater().inflate(R.layout.popup_durum,null);
    AntremanaBasla = popup_durum.findViewById(R.id.basla);
    Cikis = popup_durum.findViewById(R.id.cikis);

    dialogBuilder.setView(popup_durum);
    AlertDialog dialog = dialogBuilder.create();
    dialog.show();

    AntremanaBasla.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    Cikis.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

}

and this is the call function

public class Login extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
       -----------
    Antreman antreman = new Antreman();
    logoImage = (ImageView) findViewById(R.id.logologin);
    logoImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            antreman.AntremanSayfasi();
        }
    });
}

}

edit Application starts with this log

2021-04-05 20:09:29.151 10257-10292/gymholix.assistx D/EGL_emulation: eglCreateContext: 0xa2085120: maj 3 min 0 rcv 3
2021-04-05 20:09:29.160 10257-10292/gymholix.assistx D/EGL_emulation: eglMakeCurrent: 0xa2085120: ver 3 0 (tinfo 0xa20832d0)
2021-04-05 20:09:29.168 10257-10292/gymholix.assistx E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2021-04-05 20:09:29.169 10257-10292/gymholix.assistx E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2021-04-05 20:09:29.169 10257-10292/gymholix.assistx E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2021-04-05 20:09:29.169 10257-10292/gymholix.assistx E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2021-04-05 20:09:29.202 10257-10292/gymholix.assistx D/EGL_emulation: eglMakeCurrent: 0xa2085120: ver 3 0 (tinfo 0xa20832d0)
2021-04-05 20:09:29.240 10257-10292/gymholix.assistx D/EGL_emulation: eglMakeCurrent: 0xa2085120: ver 3 0 (tinfo 0xa20832d0)
2021-04-05 20:09:29.252 10257-10292/gymholix.assistx D/EGL_emulation: eglMakeCurrent: 0xa2085120: ver 3 0 (tinfo 0xa20832d0)
2021-04-05 20:09:29.259 10257-10292/gymholix.assistx D/EGL_emulation: eglMakeCurrent: 0xa2085120: ver 3 0 (tinfo 0xa20832d0)

then when it crashes log is like that

2021-04-05 20:09:29.259 10257-10292/gymholix.assistx D/EGL_emulation: eglMakeCurrent: 0xa2085120: ver 3 0 (tinfo 0xa20832d0)
2021-04-05 20:10:46.808 10257-10292/gymholix.assistx D/EGL_emulation: eglMakeCurrent: 0xa2085120: ver 3 0 (tinfo 0xa20832d0)
2021-04-05 20:10:52.673 10257-10257/gymholix.assistx D/AndroidRuntime: Shutting down VM
2021-04-05 20:10:52.674 10257-10257/gymholix.assistx E/AndroidRuntime: FATAL EXCEPTION: main
    Process: gymholix.assistx, PID: 10257
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
        at androidx.appcompat.app.AlertDialog.resolveDialogTheme(AlertDialog.java:115)
        at androidx.appcompat.app.AlertDialog$Builder.<init>(AlertDialog.java:313)
        at gymholix.assistx.Antreman.AntremanSayfasi(Antreman.java:23)
        at gymholix.assistx.Login$3.onClick(Login.java:76)
        at android.view.View.performClick(View.java:6256)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Ahmet Emre
  • 23
  • 7
  • Have a look at the logcat in Android Studio to see what causes the crash, if you don't know what it means or how you can solve it, update your question with the log of the crash – SABANTO Apr 05 '21 at 16:52
  • Before learning how to make apps, learn how to read logs https://developer.android.com/studio/debug/am-logcat analyze stacktraces https://developer.android.com/studio/debug/stacktraces and learn how to debug https://developer.android.com/studio/debug – Edward van Raak Apr 05 '21 at 16:54
  • Thnx edward this is graduation project and they didnt tell anything about the logcat, but now ill learn it but still cant figure why this line gives this error at gymholix.assistx.Antreman.AntremanSayfasi(Antreman.java:23) line23--->dialogBuilder = new AlertDialog.Builder(this); – Ahmet Emre Apr 05 '21 at 17:44

1 Answers1

0

Since Antreman is not an activity it should not be extending AppCompatActivity

public class Antreman {
AlertDialog.Builder dialogBuilder;
Button AntremanaBasla,Cikis;
View popup_durum;
//Pop up----------------------------------------------------------------------------------------
public void AntremanSayfasi(Context context){

    dialogBuilder = new AlertDialog.Builder(context);
    popup_durum = getLayoutInflater().inflate(R.layout.popup_durum,null);
    AntremanaBasla = popup_durum.findViewById(R.id.basla);
    Cikis = popup_durum.findViewById(R.id.cikis);

    dialogBuilder.setView(popup_durum);
    AlertDialog dialog = dialogBuilder.create();
    dialog.show();

    AntremanaBasla.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    Cikis.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

}

When calling the function to pass the context to that function, providing this keyword inside onclick will not be referencing the current activity

public class Login extends AppCompatActivity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
       -----------
    Antreman antreman = new Antreman();
    context = this;
    logoImage = (ImageView) findViewById(R.id.logologin);
    logoImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            antreman.AntremanSayfasi(context);
        }
    });
}
}
yogi_5466
  • 59
  • 5
  • your solution is worked exept the getLayoutInfiltrator() method. to avoid it i use LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); and then final View popup_durum = li.inflate(R.layout.popup_durum,null); and it worked – Ahmet Emre Apr 05 '21 at 19:55