gpt4 book ai didi

java - 应用程序只有在我手动进入时才能工作;否则异常

转载 作者:行者123 更新时间:2023-11-30 03:20:44 25 4
gpt4 key购买 nike

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 异常。它是什么?据我了解,它可能与静态虚拟内容有关。

当我调试时,可以选择逐步执行此操作,如果我进入新的 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

最佳答案

ExceptionInInitializerError 的发生是因为您试图访问空列表的第一个元素,并且您是从静态初始化 block (static { ... } )。

看起来您希望 GetArticles 函数获取并立即返回一组文章,但AsyncTask 并非如此> 作品。当你打电话时

new getTask().execute("http://...");

后台线程启动,它运行您的doInBackground() 方法来获取文章。当您使用调试器时,后台线程有足够的时间在您尝试使用它之前完成并填充您的 articles 字段。但是,如果您正常运行该程序,则后台线程没有时间在 GetArticles() 返回之前完成。一旦它返回,你打电话

get(0)[0]

在返回的(空)ArrayList 上。它抛出一个异常,因为列表是空的。

处理此问题的方法是在 AsyncTask 中覆盖 onPostExecute():

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

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

关于java - 应用程序只有在我手动进入时才能工作;否则异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19191982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com