Doğancan Batmann
Doğancan Batmann

Reputation: 3

Error on Custom Dialog when trying modify it or make changes on it

I'm new at this Android stuff. So i got stuck maybe at an easy issue. I get an error when i'm trying change the linear layouts background of my custom dialog or set layouts view.

    public void onBackPressed() {

            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

            LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.reklamlar, null);

                alertDialogBuilder.setTitle("Çıkış!");
                alertDialogBuilder
                    .setView(inflater.inflate(R.layout.reklamlar, null))
                    .setMessage("Çıkmak istediğinize emin misiniz?")
                    .setCancelable(false)
                    .setNeutralButton("Oy Ver!", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.addCategory(Intent.CATEGORY_BROWSABLE);
    intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.okidoki.kimmilyonerolmakister"));
                            startActivity(intent);
                        }
                    })
                    .setPositiveButton("Evet",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) 
                            interstitial.show();
                            System.exit(0);
                            finish();
                        }
                      })
                    .setNegativeButton("Hayır",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) 
                            dialog.cancel();
                            interstitial.show();
                        }
                    });
                Log.i("reklamlar","deneme5");

                adView2 = new AdView(this, AdSize.BANNER, "ca-app-pub-4028773540774161/5521683534");
                LinearLayout layout2 = (LinearLayout)findViewById(R.id.reklamdort);
                Log.i("reklamlar","deneme4");
//>>>>>>>>>>>>>>>>>  //layout2.addView(adView2); // also i get error when im trying to add view to layout
//>>>>>>>>>>>>>>>>>  // layout2.setBackgroundResource(R.drawable.paraa); // or change its background
                Log.i("reklamlar","deneme6");
                adView2.loadAd(new AdRequest());
                alertDialogBuilder.setView(view);
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();

        }

And here is log

11-15 13:12:28.376: E/AndroidRuntime(410): FATAL EXCEPTION: main
11-15 13:12:28.376: E/AndroidRuntime(410): java.lang.NullPointerException
11-15 13:12:28.376: E/AndroidRuntime(410):  at com.okidoki.kimmilyonerolmakister.QuizActivity.onBackPressed(QuizActivity.java:134)
11-15 13:12:28.376: E/AndroidRuntime(410):  at android.app.Activity.onKeyUp(Activity.java:1898)
11-15 13:12:28.376: E/AndroidRuntime(410):  at android.view.KeyEvent.dispatch(KeyEvent.java:1280)
11-15 13:12:28.376: E/AndroidRuntime(410):  at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
11-15 13:12:28.376: E/AndroidRuntime(410):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
11-15 13:12:28.376: E/AndroidRuntime(410):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
11-15 13:12:28.376: E/AndroidRuntime(410):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
11-15 13:12:28.376: E/AndroidRuntime(410):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
11-15 13:12:28.376: E/AndroidRuntime(410):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 13:12:28.376: E/AndroidRuntime(410):  at android.os.Looper.loop(Looper.java:123)
11-15 13:12:28.376: E/AndroidRuntime(410):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-15 13:12:28.376: E/AndroidRuntime(410):  at java.lang.reflect.Method.invokeNative(Native Method)
11-15 13:12:28.376: E/AndroidRuntime(410):  at java.lang.reflect.Method.invoke(Method.java:507)
11-15 13:12:28.376: E/AndroidRuntime(410):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-15 13:12:28.376: E/AndroidRuntime(410):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-15 13:12:28.376: E/AndroidRuntime(410):  at dalvik.system.NativeStart.main(Native Method)

I think the issue is about Inflaters. But i can't figure out why i got this error. Am i using inflaters wrong? i have just a linearlaoyut in my custom dialog and it works normal when not try to change anything of custom dialog.

Upvotes: 0

Views: 78

Answers (1)

codeMagic
codeMagic

Reputation: 44571

If the LinearLayout is in the layout you inflated above then you need to tell it to look in there for it. So you would initialize it with

LinearLayout layout2 = (LinearLayout) view.findViewById(R.id.reklamdort);

That is if reklamdort is the id of a LinearLayout in your reklamlar.xml, without telling it to look in the inflated layout by putting view.findViewById() it is looking in the layout file you inflated in setContentView()

Upvotes: 1

Related Questions