gpt4 book ai didi

android - 执行doInBackground()时发生错误-Android小部件

转载 作者:行者123 更新时间:2023-12-03 08:28:32 25 4
gpt4 key购买 nike

我正在使用Async的气象应用程序。它非常适合MainActivity,现在我正尝试为其开发一个小部件,但出现异常。

与SO的其他答案一样,对此问题的普遍共识是停止在 doInBackground 中使用UI线程。所以我尽量减少它。

package ali.projecto;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.widget.RemoteViews;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.io.InputStream;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class Widget extends AppWidgetProvider {
String xtemp;
Location loc;
Context context;
//======================================= OnUpdate Method
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
try {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

Intent intent = new Intent(context, Widget.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[0]);
intent.setAction("update");

PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.rButton, pi);

new MyAsyncTask2().execute(context);

appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}catch (Exception e){
e.printStackTrace();
}
}
//======================================= OnReceive Method
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
try {
if (intent.getAction().equals("update")) {
new MyAsyncTask2().execute();
}
} catch (Exception e){
e.printStackTrace();
}
}
//======================================= AsyncTask
public class MyAsyncTask2 extends AsyncTask<Context, Void, String> {
private RemoteViews views;
private Context context;

@Override
protected void onPreExecute() {
//getLocation();
}
@Override
protected String doInBackground(Context... params) {
context = params[0];
return loadXML2();
}
@Override
protected void onPostExecute(String result) {
try {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
views = new RemoteViews(context.getPackageName(), R.layout.widget);
if (result != null && result.length() >= 1) {
views.setTextViewText(R.id.widtemp, result);
}
appWidgetManager.updateAppWidget(new ComponentName(context, Widget.class), views);
} catch (Exception e){
e.printStackTrace();
}
}
}
//======================================= Load XML
public String loadXML2() {
try {
//=============================== Getting Data
URL xmlUrl = new URL("http://api.openweathermap.org/data/2.5/weather?q=dallas%20ga&units=metric&mode=xml&MYIDGOESHERE&units=metric&mode=xml");
InputStream in = xmlUrl.openStream();
Document doc = parse(in);
doc.getDocumentElement().normalize();
//================================ Getting Temperature
Node nNode = doc.getElementsByTagName("temperature").item(0);
Element eElement = (Element) nNode;
double d = Math.round(Double.parseDouble(eElement.getAttribute("value")));
int dx = (int) d;
xtemp = Integer.toString(dx) + "°";
} catch (Exception e) {
e.printStackTrace();
}
return xtemp;
}
//======================================= Document Parsing
public static Document parse (InputStream is) {
Document ret = null;
DocumentBuilderFactory domFactory;
DocumentBuilder builder;

try {
domFactory = DocumentBuilderFactory.newInstance();
domFactory.setValidating(false);
domFactory.setNamespaceAware(false);
builder = domFactory.newDocumentBuilder();

ret = builder.parse(is);
}
catch (Exception ex) {
System.err.println("unable to load XML: " + ex);
}
return ret;
}
}

产生此错误的步骤:
  • 创建小部件(它会更新并显示温度很好)。
  • 单击小部件,它将停止处理此异常。

  • 错误:
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: Process: ali.projecto, PID: 2505
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:300)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at ali.projecto.Widget$MyAsyncTask2.doInBackground(Widget.java:77)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at ali.projecto.Widget$MyAsyncTask2.doInBackground(Widget.java:67)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at android.os.AsyncTask$2.call(AsyncTask.java:288)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
    11-06 22:54:09.869 2505-3375/ali.projecto E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818) 

    最佳答案

    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

    数组为空。

    此行可能导致错误:

    context = params[0];



    解决方案是在执行参数时将参数传递给异步任务。
    new MyAsyncTask2().execute(my_parameter);

    关于android - 执行doInBackground()时发生错误-Android小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33579022/

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