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);
}
}