0

This is my first post on stack exchange after a long while of creeping in the shadows !

I have a simple code, 2 classes: MainActivity: with a listview, an OnCreate method, and a custom method called Build List.

other activity: Retrieves data online, then calls MainActivity's BuildList(String) method.

The problem is, my array adapter is being --generated-- in BuildList, and i'm getting the dreaded System services not available to Activities before onCreate()

Here's the relevant code:

public class MainActivity extends ActionBarActivity {
    private ListView lv;
    private   ArrayAdapter<String> FolderArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
//etcetc
    lv = (ListView)findViewById(R.id.LV1);
    lv.setAdapter(FolderArray);
//etcetc
    }

    public void BuildList(String folders) {
    ArrayList FolderList= new ArrayList(Arrays.asList(folders.split(", ")));    // string comes as "this, is, my, list"
    FolderArray = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,FolderList ); //i realise i cannot use "this" as context but i'm kind of lost now..
    }
}

Any help would be appreciated . Thanks in advance!

--edit--

Here's My Logcat

02-14 11:35:25.456  16059-16059/com.controller E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.controller, PID: 16059
java.lang.RuntimeException: Unable to start receiver com.controller.CustomNotificationReceiver: java.lang.IllegalStateException: System services not available to Activities before onCreate()
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2521)
        at android.app.ActivityThread.access$1700(ActivityThread.java:157)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5293)
        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)
 Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
        at android.app.Activity.getSystemService(Activity.java:4676)
        at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
        at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
        at com.controller.MainActivity.BuildList(MainActivity.java:98)
        at com.controller.CustomNotificationReceiver.onReceive(CustomNotificationReceiver.java:45)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2514)
            at android.app.ActivityThread.access$1700(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5293)
            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)

Please note that "(MainActivity.java:98)" Points to:

         FolderArray = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,FolderList );

--edit 2--

Here's my second class:

public class CustomNotificationReceiver extends BroadcastReceiver {


    String alert;
    String DirList = "ls";

    @Override
    public void onReceive(Context context, Intent intent) {


        try {

            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

            alert = json.getString("alert");  

        } catch (JSONException e) {

        }
        Toast.makeText(context, alert,
            Toast.LENGTH_LONG).show();      //this gives out the correct result

       MainActivity mainact = new MainActivity();
      mainact.BuildList(alert);
    }
}
M Aoun
  • 11
  • 5
  • Add your LogCat please – Marcus Feb 14 '15 at 09:50
  • "other activity: Retrieves data online, then calls MainActivity's BuildList(String) method." - If your MainActivity is not created, you can not call it's methods. Only your MainActivity can call its methods, when it's in its created state. – Marcus Feb 14 '15 at 09:51
  • @Marcus Any Suggested ways around this ? No need for full on code answers, just a tip for a workaround, and i'll accept it and learn some more .. – M Aoun Feb 14 '15 at 09:59
  • It seems that the error is coming from `CustomNotificationReceiver. Please post that code. – Marcus Feb 14 '15 at 10:05
  • @Marcus Done as you requested – M Aoun Feb 14 '15 at 10:18
  • Any luck with my answer? @M Aoun – Marcus Feb 14 '15 at 13:20

1 Answers1

0

This is what's wrong:

MainActivity mainact = new MainActivity();
mainact.BuildList(alert);

You can not instantiate an Activity like that, it's created from the framework. If you want to send data between Activitys, you can use intents or SharedPreferences.

Example using Intent:

Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("your_key", yourValue);
startActivity(intent)

There's more way to do this, but removing new MainActivity(); will remove the error, then you can take it from there.

Community
  • 1
  • 1
Marcus
  • 6,697
  • 11
  • 46
  • 89
  • Thanks ! Following Your note, i did some more research and used " Intent broadcastIntent" and context.sendBroadcast() to solve my issue. – M Aoun Feb 14 '15 at 21:19