0

有人能解释一下为什么我的代码在..

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

4

1 回答 1

1

您的代码中至少有两个明显的问题。

您的线路:

if(camera!=null) camera=Camera.open();

Camera...如果不是,则初始化您的对象null(这听起来很奇怪 - 可能永远不会被初始化)。

但是下面的所有代码,包括:

Camera.Parameters p = camera.getParameters();

... 不依赖camera != null,因此它可能正在引用一个null指针。

因此,您很可能会抛出一个NullPointerException(我说“可能”,因为您还没有发布堆栈跟踪)。

我会这样更改您的代码:

// removed if(camera!=null) 
camera = Camera.open();

// checking "camera" is not null
if (camera != null) { // using curly brackets, comes very handy sometimes :D
    // ... the rest of the code
}
于 2013-08-18T14:23:05.937 回答