0

I'm having a FragmentActivity that consists of a lot of elements that causes a pop-up window shown on every item click regarding to that item, I'm creating the popup the regular way:

LayoutInflater inflater;
View layout;
inflater = (LayoutInflater) MyActivity.this
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.my_popup_layout,
        (ViewGroup) findViewById(R.id.my_popup));

this way works very efficient when clicking on any item but this is only one item that when clicking on the app crashes and raises system services not available exception :

07-07 11:49:14.905: E/AndroidRuntime(2684): FATAL EXCEPTION: main
07-07 11:49:14.905: E/AndroidRuntime(2684): Process: com.automation.isolace, PID: 2684
07-07 11:49:14.905: E/AndroidRuntime(2684): java.lang.IllegalStateException: System services not available to Activities before onCreate()
07-07 11:49:14.905: E/AndroidRuntime(2684):     at android.app.Activity.getSystemService(Activity.java:4532)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at com.automation.isolace.MyActivity$3.onClick(Security.java:767)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at android.view.View.performClick(View.java:4438)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at android.view.View$PerformClick.run(View.java:18422)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at android.os.Handler.handleCallback(Handler.java:733)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at android.os.Looper.loop(Looper.java:136)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at java.lang.reflect.Method.invoke(Method.java:515)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-07 11:49:14.905: E/AndroidRuntime(2684):     at dalvik.system.NativeStart.main(Native Method)

the item that this behavior happens when clicking on is the only item that is programmacally created:

 Button rooms = new Button(MyApplication.getAppContext());
 container_layout.addView(rooms);
 params = (RelativeLayout.LayoutParams) rooms.getLayoutParams();
 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
 rooms.setLayoutParams(params);
 rooms.setTag(sys_id);
 rooms.setTag(R.string.app_name, zone_name.getText());
 rooms.setBackgroundResource(R.drawable.a_drawable_button_drawable);
 rooms.setText(R.string.rooms);
 rooms.setTextColor(Color.WHITE);
 rooms.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
 rooms.setPadding(5, 3, 5, 3);
 rooms.setOnClickListener(on_zone_roomsClick);
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • The stacktrace says the code is in `Security` but you're using `MyActivity.this` for a `Context`. What is the context you're using, really? – laalto Jul 07 '14 at 12:27
  • @laalto if I left my code with my classes and variables names the people wouldn't understand it, So that i changed it to a readable form. – Muhammed Refaat Jul 07 '14 at 12:31
  • I see. The exception comes when the `Context` is not properly set up (base context missing, normally in activity lifecycle it is set up just before `onCreate()`). That's why it would be interesting to see more context around how you're setting up the context you're calling `getSystemService()` on. – laalto Jul 07 '14 at 12:34
  • @laalto I can test my contexts before clicking on that button and I did and it worked. that's why I couldn't trace the Contexts. and I don't know how an app can lose it's contexts – Muhammed Refaat Jul 07 '14 at 12:39

0 Answers0