有人可以帮我解释为什么这个小部件服务代码只能运行@ api 8,9和10?这不适用于Android API 17 4.2.2,安装时也会出错。
当我在2.3.3终端上运行时,它会运行并显示提要!
我在4.2.2终端上运行时获得的logcat是这样的:
12-13 22:00:27.907: D/widget(1438): onUpdate()
12-13 22:00:27.957: D/widget(1438): Beginning update cycle...
12-13 22:00:28.017: D/AndroidRuntime(1438): Shutting down VM
12-13 22:00:28.017: W/dalvikvm(1438): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-13 22:00:28.059: E/AndroidRuntime(1438): FATAL EXCEPTION: main
12-13 22:00:28.059: E/AndroidRuntime(1438): java.lang.RuntimeException: Unable to start service com.example.rss.widgetservice@40ce59f8 with Intent { cmp=com.example.rss/.widgetservice }: java.lang.NullPointerException: println needs a message
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2673)
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.app.ActivityThread.access$1900(ActivityThread.java:141)
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.os.Looper.loop(Looper.java:137)
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-13 22:00:28.059: E/AndroidRuntime(1438): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 22:00:28.059: E/AndroidRuntime(1438): at java.lang.reflect.Method.invoke(Method.java:511)
12-13 22:00:28.059: E/AndroidRuntime(1438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 22:00:28.059: E/AndroidRuntime(1438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 22:00:28.059: E/AndroidRuntime(1438): at dalvik.system.NativeStart.main(Native Method)
12-13 22:00:28.059: E/AndroidRuntime(1438): Caused by: java.lang.NullPointerException: println needs a message
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.util.Log.println_native(Native Method)
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.util.Log.d(Log.java:138)
12-13 22:00:28.059: E/AndroidRuntime(1438): at com.example.rss.widgetservice.onStart(widgetservice.java:44)
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.app.Service.onStartCommand(Service.java:450)
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2656)
12-13 22:00:28.059: E/AndroidRuntime(1438): ... 10 more
以下是代码:
请有人帮助我理解这一点!
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
public class widgetservice extends Service {
@Override
public void onStart(Intent intent, int startId) {
RemoteViews rviews = new RemoteViews(this.getPackageName(), R.layout.widget_layout);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
ComponentName meuWidget = new ComponentName(this, widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
try {
rviews.setTextViewText(R.id.TextView01, "Updating...");
manager.updateAppWidget(meuWidget, rviews);
Log.d("widget", "Beginning update cycle...");
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse("http://blog.masterd.pt/index.php/feed/");
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("item");
if (items.getLength() > 0) {
Element entrada = (Element) items.item(0);
NodeList titulos = entrada.getElementsByTagName("title");
Element titulo = (Element) titulos.item(0);
String texto = titulo.getFirstChild().getNodeValue();
rviews.setTextViewText(R.id.TextView01, texto);
Log.d("widget", "Update cycle completed!");
manager.updateAppWidget(meuWidget, rviews);
}
}
catch(Exception ex) {
Log.d("widget", ex.getMessage());
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rss"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.rss.widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_info">
</meta-data>
</receiver>
<activity
android:name="com.example.rss.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.rss.widgetservice" />
</application>
</manifest>
我的小部件提供商
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context,
AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.d("widget", "onUpdate()");
context.startService(new Intent(context, widgetservice.class));
}
}
答案 0 :(得分:0)
12-13 22:00:28.059: E/AndroidRuntime(1438): Caused by: java.lang.NullPointerException: println needs a message
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.util.Log.println_native(Native Method)
12-13 22:00:28.059: E/AndroidRuntime(1438): at android.util.Log.d(Log.java:138)
12-13 22:00:28.059: E/AndroidRuntime(1438): at com.example.rss.widgetservice.onStart(widgetservice.java:44)
java.lang.NullPointerException:println需要一条消息
似乎ex.getMessage()
在此块返回null
catch(Exception ex) {
Log.d("widget", ex.getMessage());
}
将代码更改为:
catch(Exception ex) {
String error = (ex.getMessage()==null)?"Some message error":ex.getMessage();
Log.d("widget", error);
}
答案 1 :(得分:0)
感谢大家的帮助! @Ramaral你让我看到了什么问题...在蜂窝到果冻豆我们必须使用异步任务...所以一个简单的方法来获得这个工作在果冻豆是删除使用sdk最小和最大版本强>
现在一切都很完美!非常感谢!案例已解决