0

what`s wrong with this because I cant compile to apk. Error is Cannot use this in a static context but if I change this to context app freeze when dialog must appear.

public static void mydialog(final Context context) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    TextView myMsg = new TextView(this);
    myMsg.setText("Central");
}

Logcat error:

 android.view.WindowLeaked: Activity com.test.testapp.activities.MenuActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{430fc9a8 V.E..... R.....I. 0,0-1048,466} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:457)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:288)
at com.dialog.dialog.showDialog(RateThisApp.java:229)
at com.dialog.dialog.showDialogIfNeeded(RateThisApp.java:108)
at com.test.testapp.activities.MenuActivity.onCreate(MenuActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Desmo
  • 53
  • 6

1 Answers1

2

A static method is not associated with any particular instance of a class. It is a method associated with the class itself. Thus this has no meaning in a static method- there is no instance of the class to reference.

If this method is in a class that is a Context (e.g. an Application subclass or an Activity), then you can remove the static modifier and use this like you are already trying to do.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • but what if I want to keep static method? How can I modify TextView(this) to show proper text in dialog? – Desmo May 27 '15 at 14:04
  • 1
    Switching `this` to `context` should work, but without seeing your logcat from when it freezes it is difficult to say why that wouldn't work for you- it should work just fine. – Bryan Herbst May 27 '15 at 14:05
  • Arhh...Thanks, solve the problem! :) I forgot to include setView. Just want to know; why is in my method text smaller than if I set builder.setMessage("Message")? – Desmo May 27 '15 at 14:13
  • 1
    You don't appear to be applying any sort of styling to your TextView. AlertDialogs typically use ` style="?android:attr/textAppearanceMedium"` for the message in their XML layout, so you will want to do something similar if you want the same styling. – Bryan Herbst May 27 '15 at 14:18
  • Thanks. Now I was detect crash issue. If I change orentation of the phone app crashed. I was paste logcat in original post. Is this related to my TextView? – Desmo May 27 '15 at 14:33
  • I would read through [this post](http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added) – Bryan Herbst May 27 '15 at 14:38