gpt4 book ai didi

java - jsoup 代码可以在 Java 中运行,但不能在 Android 中运行 - NullpointerException

转载 作者:行者123 更新时间:2023-12-01 07:09:06 26 4
gpt4 key购买 nike

我正在尝试使用 jsoup 编写一个天气抓取 Android 应用程序。我有使用 Java 运行的代码,但即使是我可以在 android 中编写的最简单的形式也会引发错误。这是我的代码的一部分:

private class Retrieve_Weather extends AsyncTask<Void, Void, String> {
private final String TAG = "Retrieve_Weather";

protected String doInBackground(Void... params) {

String weatherline= "oTHErly";
Log.i(TAG, weatherline);
Document doc = null;
try {
Log.i(TAG, "trying document...");
doc = Jsoup.connect("http://m.wund.com/cgi-bin/findweather/getForecast?brand=mobile&query=02139").get();
Log.i(TAG, "after document");
weatherline = "newline";
}
catch (IOException except) {
Log.i(TAG, "Cannot Connect", except);
except.printStackTrace();
weatherline = "Can't Connect";
Log.i(TAG, "End stacktrace");

}

Log.i(TAG, weatherline);
return weatherline;
}

@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}

@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
@Override
protected void onPostExecute(String result){
//super.onPostExecute(result);
Log.i(TAG, "onPostExecute");
}


}

我已经对我的代码和 logcat 进行了充分的研究,发现我的错误是运行 .get() 时抛出的空指针异常。我只是不明白为什么或如何解决它。这是我的堆栈跟踪:

07-09 03:18:13.901: I/Retrieve_Weather(1754): trying document...
07-09 03:18:15.900: D/dalvikvm(1754): GC_CONCURRENT freed 80K, 7% free 2711K/2912K, paused 83ms+20ms, total 244ms
07-09 03:18:15.910: D/dalvikvm(1754): WAIT_FOR_CONCURRENT_GC blocked 157ms
07-09 03:18:16.351: D/dalvikvm(1754): GC_CONCURRENT freed 297K, 15% free 2841K/3340K, paused 81ms+124ms, total 329ms
07-09 03:18:16.740: W/dalvikvm(1754): Exception Ljava/lang/NullPointerException; thrown while initializing Lorg/jsoup/nodes/Entities;
07-09 03:18:16.740: W/dalvikvm(1754): Exception Ljava/lang/ExceptionInInitializerError; thrown while initializing Lorg/jsoup/nodes/Entities$EscapeMode;
07-09 03:18:16.810: W/System.err(1754): java.util.concurrent.ExecutionException: java.lang.ExceptionInInitializerError
07-09 03:18:16.830: W/System.err(1754): at java.util.concurrent.FutureTask.report(FutureTask.java:94)
07-09 03:18:16.830: W/System.err(1754): at java.util.concurrent.FutureTask.get(FutureTask.java:160)
07-09 03:18:16.860: W/System.err(1754): at android.os.AsyncTask.get(AsyncTask.java:482)
07-09 03:18:16.880: W/System.err(1754): at com.example.weatherresponse.Weather.onCreate(Weather.java:35)
07-09 03:18:16.880: W/System.err(1754): at android.app.Activity.performCreate(Activity.java:5104)
07-09 03:18:16.911: W/System.err(1754): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-09 03:18:16.911: W/System.err(1754): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-09 03:18:16.960: W/System.err(1754): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-09 03:18:16.970: W/System.err(1754): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-09 03:18:16.970: W/System.err(1754): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-09 03:18:16.980: W/System.err(1754): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 03:18:16.990: W/System.err(1754): at android.os.Looper.loop(Looper.java:137)
07-09 03:18:16.990: W/System.err(1754): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-09 03:18:16.990: W/System.err(1754): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 03:18:17.010: W/System.err(1754): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 03:18:17.061: W/System.err(1754): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-09 03:18:17.061: W/System.err(1754): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-09 03:18:17.061: W/System.err(1754): at dalvik.system.NativeStart.main(Native Method)
07-09 03:18:17.090: W/System.err(1754): Caused by: java.lang.ExceptionInInitializerError
07-09 03:18:17.090: W/System.err(1754): at org.jsoup.nodes.Document$OutputSettings.<init>(Document.java:210)
07-09 03:18:17.150: W/dalvikvm(1754): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
07-09 03:18:17.150: W/System.err(1754): at org.jsoup.nodes.Document.<init>(Document.java:18)
07-09 03:18:17.160: W/System.err(1754): at org.jsoup.parser.TreeBuilder.initialiseParse(TreeBuilder.java:27)
07-09 03:18:17.160: W/System.err(1754): at org.jsoup.parser.TreeBuilder.parse(TreeBuilder.java:40)
07-09 03:18:17.160: W/System.err(1754): at org.jsoup.parser.HtmlTreeBuilder.parse(HtmlTreeBuilder.java:37)
07-09 03:18:17.170: W/System.err(1754): at org.jsoup.parser.Parser.parseInput(Parser.java:30)
07-09 03:18:17.170: W/System.err(1754): at org.jsoup.helper.DataUtil.parseByteData(DataUtil.java:102)
07-09 03:18:17.170: W/System.err(1754): at org.jsoup.helper.HttpConnection$Response.parse(HttpConnection.java:498)
07-09 03:18:17.215: W/System.err(1754): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:154)
07-09 03:18:17.215: W/System.err(1754): at com.example.weatherresponse.Weather$Retrieve_Weather.doInBackground(Weather.java:85)
07-09 03:18:17.220: W/System.err(1754): at com.example.weatherresponse.Weather$Retrieve_Weather.doInBackground(Weather.java:1)
07-09 03:18:17.220: W/System.err(1754): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-09 03:18:17.230: W/System.err(1754): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-09 03:18:17.230: W/System.err(1754): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-09 03:18:17.240: W/System.err(1754): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-09 03:18:17.240: W/System.err(1754): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-09 03:18:17.240: W/System.err(1754): at java.lang.Thread.run(Thread.java:856)
07-09 03:18:17.260: W/System.err(1754): Caused by: java.lang.ExceptionInInitializerError
07-09 03:18:17.290: W/System.err(1754): at org.jsoup.nodes.Entities$EscapeMode.<clinit>(Entities.java:20)
07-09 03:18:17.290: W/System.err(1754): ... 17 more
07-09 03:18:17.290: W/System.err(1754): Caused by: java.lang.NullPointerException: in == null
07-09 03:18:17.290: W/System.err(1754): at java.util.Properties.load(Properties.java:246)
07-09 03:18:17.290: W/System.err(1754): at org.jsoup.nodes.Entities.loadEntities(Entities.java:151)
07-09 03:18:17.290: W/System.err(1754): at org.jsoup.nodes.Entities.<clinit>(Entities.java:135)
07-09 03:18:17.290: W/System.err(1754): ... 18 more
07-09 03:18:17.383: I/System.out(1754): ExecExceptjava.lang.ExceptionInInitializerError
07-09 03:18:19.570: E/AndroidRuntime(1754): FATAL EXCEPTION: AsyncTask #1
07-09 03:18:19.570: E/AndroidRuntime(1754): java.lang.RuntimeException: An error occured while executing doInBackground()
07-09 03:18:19.570: E/AndroidRuntime(1754): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-09 03:18:19.570: E/AndroidRuntime(1754): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.lang.Thread.run(Thread.java:856)
07-09 03:18:19.570: E/AndroidRuntime(1754): Caused by: java.lang.ExceptionInInitializerError
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.nodes.Document$OutputSettings.<init>(Document.java:210)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.nodes.Document.<init>(Document.java:18)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.parser.TreeBuilder.initialiseParse(TreeBuilder.java:27)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.parser.TreeBuilder.parse(TreeBuilder.java:40)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.parser.HtmlTreeBuilder.parse(HtmlTreeBuilder.java:37)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.parser.Parser.parseInput(Parser.java:30)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.helper.DataUtil.parseByteData(DataUtil.java:102)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.helper.HttpConnection$Response.parse(HttpConnection.java:498)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.helper.HttpConnection.get(HttpConnection.java:154)
07-09 03:18:19.570: E/AndroidRuntime(1754): at com.example.weatherresponse.Weather$Retrieve_Weather.doInBackground(Weather.java:85)
07-09 03:18:19.570: E/AndroidRuntime(1754): at com.example.weatherresponse.Weather$Retrieve_Weather.doInBackground(Weather.java:1)
07-09 03:18:19.570: E/AndroidRuntime(1754): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-09 03:18:19.570: E/AndroidRuntime(1754): ... 4 more
07-09 03:18:19.570: E/AndroidRuntime(1754): Caused by: java.lang.ExceptionInInitializerError
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.nodes.Entities$EscapeMode.<clinit>(Entities.java:20)
07-09 03:18:19.570: E/AndroidRuntime(1754): ... 17 more
07-09 03:18:19.570: E/AndroidRuntime(1754): Caused by: java.lang.NullPointerException: in == null
07-09 03:18:19.570: E/AndroidRuntime(1754): at java.util.Properties.load(Properties.java:246)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.nodes.Entities.loadEntities(Entities.java:151)
07-09 03:18:19.570: E/AndroidRuntime(1754): at org.jsoup.nodes.Entities.<clinit>(Entities.java:135)
07-09 03:18:19.570: E/AndroidRuntime(1754): ... 18 more

除了这一基本行之外,我的代码运行正常。我已经在模拟器和 Android 手机上测试了我的代码,并且两者都可以访问互联网。我尝试过多个网站以及这些网站的移动版本,但没有一个有效。我的 list 和库文件位于正确的位置,具有互联网权限。我在这个网站和其他网站上浏览了很多,寻找解决我的问题的方法。由于我在相关帖子中找到的内容,我已经修改了我的代码和权限,但我还没有找到适合我的已回答帖子。然而,我发现一些未答复的帖子提出了完全相同的问题。我发现我没有尝试过的唯一答案是这个问题Error when releasing APK with AsyncTask and Jsoup :我不知道要更改哪个文件来测试它是否能解决我的问题。我对编程相当陌生 - 任何帮助将不胜感激。谢谢!

最佳答案

将这些行添加到 proguard-project.txt

-keep public enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

关于java - jsoup 代码可以在 Java 中运行,但不能在 Android 中运行 - NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17539730/

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