1
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
4

3 回答 3

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 小部件、使用Views 等。现在是刷新您向用户显示的列表的好时机。

编辑

refreshArticles()方法应该更新 UI 以显示刚刚加载的文章。(还有其他方法可以做到这一点,但关键是onPostExecute()应该通知应用程序的其余部分有可用的新数据。)

请务必GetArticles()从静态初始化程序中删除对的调用,这样您的虚拟项目就不会尝试发出缓慢的网络请求。我将简化GetArticles()如下:

public void GetArticles() {
    new getTask().execute("http://www.ynet.co.il/Iphone/0,13257,MainCategory-V7-2,00.xml");
}

然后,当您准备好刷新 UI 时,只需调用GetArticles()并等待onPostExecute()通知您有新文章可用。

于 2013-10-05T02:28:30.370 回答
0

我认为您的文章在您尝试访问它们之前没有机会加载。这就解释了为什么当您逐步调试它时它会起作用 - 他们有更多的时间来加载。

于 2013-10-04T22:55:10.013 回答
0

当您遇到您在主题行中描述的错误时,通常是因为时间问题 - 一些长时间运行的代码在执行到需要其结果的后续代码部分之前没有完成。在这种情况下,它看起来是由于您正在发出的网络请求并期望几乎立即获得结果。您需要将该请求移至其他地方。

于 2013-10-04T22:58:21.813 回答