从不同函数的 ArrayAdapter 填充 Listview - Android

Populate Listview from a different function's ArrayAdapter - Android

这是我在暗中潜行很长一段时间后第一次 post 进行堆栈交换!

我有一个简单的代码,2 classes: MainActivity:使用 listviewOnCreate 方法和名为 Build List 的自定义方法。

other activity:在线获取数据,然后调用MainActivity的BuildList(String)方法。

问题是,我的阵列适配器正在 BuildList 中 --generated--,我得到了可怕的 System services not available to Activities before onCreate()

相关代码如下:

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..
    }
}

如有任何帮助,我们将不胜感激。 提前致谢!

--编辑--

这是我的 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.access00(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.access00(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)

请注意“(MainActivity.java:98)”指向:

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

--编辑2--

这是我的第二个 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);
    }
}

这是错误的地方:

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

您不能像这样实例化 Activity,它是从框架创建的。如果要在Activity秒之间发送数据,可以使用intents or SharedPreferences.

使用 Intent 的示例:

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

有更多方法可以做到这一点,但删除 new MainActivity(); 将消除错误,然后您可以从那里开始。