static Organizer org = new Organizer();
static {
// Add 3 sample items.
addItem(new DummyItem("1", org.GetArticles().get(0)[0]));
addItem(new DummyItem("2", "Item 2"));
addItem(new DummyItem("3", "Item 3"));
addItem(new DummyItem("4", "Item 4"));
}
并在Organizer.java中
public class Organizer {
XMLGettersSetters data;
ArrayList<String[]> articles = new ArrayList<String[]>();
public ArrayList<String[]> GetArticles(){
new getTask().execute("http://www.ynet.co.il/Iphone/0,13257,MainCategory-V7-2,00.xml");
return articles;
}
private class getTask extends AsyncTask<String, Integer, ArrayList<String[]>> {
//AsyncTask
}
}
它返回ExceptionInInitializerError异常。它是什么? 根据我的理解,它可能与静态dummyContent有关。
当我调试时,可以选择一步一步地执行此操作,如果我进入新的getTask()。execute(),一切正常。但是,只有我踏入。否则会有这个例外。
编辑:添加了logcat。
10-05 16:49:37.099: D/dalvikvm(1959): Late-enabling CheckJNI
10-05 16:49:37.189: W/ActivityThread(1959): Application com.example.newsnow is waiting for the debugger on port 8100...
10-05 16:49:37.199: I/System.out(1959): Sending WAIT chunk
10-05 16:49:37.739: I/dalvikvm(1959): Debugger is active
10-05 16:49:37.799: I/System.out(1959): Debugger has connected
10-05 16:49:37.799: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:37.999: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:38.199: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:38.399: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:38.599: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:38.799: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:38.999: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:39.199: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:39.399: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:39.599: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:39.799: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:39.999: I/System.out(1959): waiting for debugger to settle...
10-05 16:49:40.199: I/System.out(1959): debugger has settled (1491)
10-05 16:49:44.959: W/dalvikvm(1959): Exception Ljava/lang/NullPointerException; thrown while initializing Lcom/example/newsnow/dummy/DummyContent;
10-05 16:49:48.099: D/AndroidRuntime(1959): Shutting down VM
10-05 16:49:48.099: W/dalvikvm(1959): threadid=1: thread exiting with uncaught exception (group=0x41a8d930)
10-05 16:49:48.139: E/AndroidRuntime(1959): FATAL EXCEPTION: main
10-05 16:49:48.139: E/AndroidRuntime(1959): java.lang.ExceptionInInitializerError
10-05 16:49:48.139: E/AndroidRuntime(1959): at com.example.newsnow.ItemListFragment.onCreate(ItemListFragment.java:89)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.support.v4.app.Fragment.performCreate(Fragment.java:1455)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-05 16:49:48.139: E/AndroidRuntime(1959): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.app.Activity.setContentView(Activity.java:1881)
10-05 16:49:48.139: E/AndroidRuntime(1959): at com.example.newsnow.ItemListActivity.onCreate(ItemListActivity.java:59)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.app.Activity.performCreate(Activity.java:5104)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.os.Looper.loop(Looper.java:137)
10-05 16:49:48.139: E/AndroidRuntime(1959): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-05 16:49:48.139: E/AndroidRuntime(1959): at java.lang.reflect.Method.invokeNative(Native Method)
10-05 16:49:48.139: E/AndroidRuntime(1959): at java.lang.reflect.Method.invoke(Method.java:511)
10-05 16:49:48.139: E/AndroidRuntime(1959): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-05 16:49:48.139: E/AndroidRuntime(1959): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-05 16:49:48.139: E/AndroidRuntime(1959): at dalvik.system.NativeStart.main(Native Method)
10-05 16:49:48.139: E/AndroidRuntime(1959): Caused by: java.lang.NullPointerException
10-05 16:49:48.139: E/AndroidRuntime(1959): at com.example.newsnow.dummy.DummyContent.<clinit>(DummyContent.java:32)
10-05 16:49:48.139: E/AndroidRuntime(1959): ... 28 more
答案 0 :(得分:1)
ExceptionInInitializerError
正在发生,因为您正在尝试访问空列表的第一个元素,而您是从静态初始化程序块(static { ... }
)执行此操作。
看起来您期望GetArticles
函数获取并立即返回一组文章,但这不是AsyncTask
的工作方式。当你打电话
new getTask().execute("http://...");
启动后台线程,并运行您的doInBackground()
方法来获取文章。当您使用调试器时,后台线程有足够的时间来完成并填充articles
字段,然后再尝试使用它。但是,如果正常运行程序,后台线程在GetArticles()
返回之前没有时间完成。一旦它返回,你打电话
get(0)[0]
在返回的(空)ArrayList
上。它会引发异常,因为列表为空。
处理此问题的方法是覆盖onPostExecute()
中的AsyncTask
:
private class getTask extends AsyncTask<String, Integer, ArrayList<String[]>> {
@Override
protected ArrayList<String[]> doInBackground(String... args) {
// Fetch your articles...
}
@Override
protected void onPostExecute(ArrayList<String[]> newArticles) {
// This runs on the UI thread
articles = newArticles;
// Refresh the list of articles...
refreshArticles();
}
}
当onPostExecute()
方法完成其工作时,doInBackground()
方法将自动运行,并且它也在UI线程上运行。这意味着您可以安全地修改UI小部件,使用View
等等。这是刷新您向用户显示的列表的好时间和地点。
修改强>:
refreshArticles()
方法应更新UI以显示刚刚加载的文章。 (还有其他方法可以做到这一点,但关键的想法是onPostExecute()
应该通知应用程序的其余部分有可用的新数据。)
请务必从静态初始化程序中删除对GetArticles()
的调用,以便您的虚拟项目不会尝试进行慢速网络请求。我会简化GetArticles()
如下:
public void GetArticles() {
new getTask().execute("http://www.ynet.co.il/Iphone/0,13257,MainCategory-V7-2,00.xml");
}
然后,当您准备好刷新用户界面时,只需致电GetArticles()
并等待onPostExecute()
通知您新文章可用。
答案 1 :(得分:0)
我认为您的文章在尝试访问之前没有机会加载。这解释了为什么它在逐步调试时有效 - 它们有更多的时间加载。
答案 2 :(得分:0)
当您遇到类似于主题行中描述的错误时,通常是因为时间问题 - 一些长时间运行的代码在执行到达需要其结果的后续代码段之前没有完成。在这种情况下,它看起来是由于您正在制作的网络请求,并且几乎是即时的。您需要将该请求移至其他位置。