有人能解释一下为什么我的代码在..
Camera.Parameters p = camera.getParameters(); //crashing line
代码是
package it.elec29a.ultra.flashlight;
import java.io.IOException;
import java.util.Random;
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.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MyWidgetProvider extends AppWidgetProvider {
private Camera camera=null;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
PackageManager pm = context.getPackageManager();
// if device support camera?
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return;
}
if(camera!=null) camera=Camera.open();
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
Log.d("In Widget ",""+widgetId);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.buttonwidget, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
Camera.Parameters p = camera.getParameters(); //crashing line
if(true) return; //for debug exit
String flashMode = p.getFlashMode();
if(flashMode == null) {
//DO something, if it has no value
} else if(flashMode.equals(Camera.Parameters.FLASH_MODE_OFF)) {
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
} else if(flashMode.equals(Camera.Parameters.FLASH_MODE_TORCH)) {
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
}
}
}
有人可以帮我解决这个问题... thx