我制作了一个简单的加速计应用程序,我想制作一个配套小工具。但每次我向widgetprovider添加传感器时都会出现以下错误:
07-25 10:31:01.337:E / AndroidRuntime(8908):致命异常:主要 07-25 10:31:01.337:E / AndroidRuntime(8908): java.lang.RuntimeException:无法启动接收器 com.example.axelo.AxeloAppWidgetProvider: java.lang.NullPointerException 07-25 10:31:01.337: E / AndroidRuntime(8908):at android.app.ActivityThread.handleReceiver(ActivityThread.java:2153) 07-25 10:31:01.337:E / AndroidRuntime(8908):at android.app.ActivityThread.access $ 1500(ActivityThread.java:127)07-25 10:31:01.337:E / AndroidRuntime(8908):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1208) 07-25 10:31:01.337:E / AndroidRuntime(8908):at android.os.Handler.dispatchMessage(Handler.java:99)07-25 10:31:01.337:E / AndroidRuntime(8908):at android.os.Looper.loop(Looper.java:137)07-25 10:31:01.337: E / AndroidRuntime(8908):at android.app.ActivityThread.main(ActivityThread.java:4448)07-25 10:31:01.337:E / AndroidRuntime(8908):at java.lang.reflect.Method.invokeNative(Native Method)07-25 10:31:01.337:E / AndroidRuntime(8908):at java.lang.reflect.Method.invoke(Method.java:511)07-25 10:31:01.337: E / AndroidRuntime(8908):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:823) 07-25 10:31:01.337:E / AndroidRuntime(8908):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)07-25 10:31:01.337:E / AndroidRuntime(8908):at dalvik.system.NativeStart.main(Native Method)07-25 10:31:01.337: E / AndroidRuntime(8908):引起:java.lang.NullPointerException 07-25 10:31:01.337:E / AndroidRuntime(8908):at com.example.axelo.AxeloAppWidgetProvider.onUpdate(AxeloAppWidgetProvider.java:29) 07-25 10:31:01.337:E / AndroidRuntime(8908):at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66) 07-25 10:31:01.337:E / AndroidRuntime(8908):at android.app.ActivityThread.handleReceiver(ActivityThread.java:2146) 07-25 10:31:01.337:E / AndroidRuntime(8908):... 10 more
这是我的widget类看起来像
package com.example.axelo;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.TextView;
public class AxeloAppWidgetProvider extends AppWidgetProvider implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
Log.v("SENSOR_SERVICE","accelerometer found!");
}
else {
Log.v("SENSOR_SERVICE","Not found!");
}
// Create an Intent to launch MainActivity
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.axelo_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// // Show changes on screen.
views.setTextViewText(R.id.coord_X, Float.toString(linear_acceleration[0]));
views.setTextViewText(R.id.coord_Y, Float.toString(linear_acceleration[1]));
views.setTextViewText(R.id.coord_Z, Float.toString(linear_acceleration[2]));
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
float[] gravity={(float) 9.81,(float) 9.81,(float) 9.81};
float[] linear_acceleration=new float[3];
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
final float alpha = (float) 0.8;
Log.v("SENSOR_SERVICE", "on sensor change");
// Isolate the force of gravity with the low-pass filter.
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
// Remove the gravity contribution with the high-pass filter.
linear_acceleration[0] = event.values[0] - gravity[0];
linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];
}
}
请告诉我哪里出错或者无法在小工具中添加传感器?
答案 0 :(得分:0)
我认为传感器小部件不适用于这种方式,你应该扩展Activity而不是AppWidgetProvider并且不要忘记registerListener / unregisterListener,你可以做更多的事情你可以将活动类添加到小部件清单并将你的代码放在这里类正常,从小部件你可以setOnClickPendingIntent一个按钮到这个Activity。