这是我在阴影中长时间爬行后的第一篇关于堆栈交换的帖子!
我有一个简单的代码,2个类:
MainActivity
:使用listview
,OnCreate
方法和名为Build List的自定义方法。
其他活动:在线检索数据,然后调用MainActivity' BuildList(String)
方法。
问题是,我的数组适配器正在BuildList中生成 - 并且我得到了可怕的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.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)
请注意&#34;(MainActivity.java:98)&#34;指向:
FolderArray = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,FolderList );
- 编辑2 -
这是我的第二堂课:
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);
}
}
答案 0 :(得分:0)
这是错的:
MainActivity mainact = new MainActivity();
mainact.BuildList(alert);
您不能像这样实例化Activity
,它是从框架创建的。如果您想在Activity
之间发送数据,可以使用intents或SharedPreferences。
使用Intent
的示例:
Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("your_key", yourValue);
startActivity(intent)
还有更多方法可以执行此操作,但删除new MainActivity();
将删除错误,然后您可以从那里获取错误。