在我的程序中,我希望允许用户背靠背捕获多个图像,但是一旦我点击捕获按钮获取空指针异常(NPE)。我创建了一个自定义相机。< / p>
如果我将从现有代码中删除闪光灯代码,那么它对我来说工作正常(是的,然后我能够在不面对任何NPE的情况下背靠背捕获多个图像),但每当我使用Flash Light代码时 NPE
在行号:324
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
在行号:301
pictureFile = getOutputMediaFile();
Java代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mCamera = getCameraInstance();
mCameraPreview = new PreviewSurface(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("folder_name");
Log.d(CameraLauncherActivity.LOG_TAG, "folder_name :: " + value);
}
Button captureButton = (Button) findViewById(R.id.btnCapture);
Log.d(CameraLauncherActivity.LOG_TAG, "captureButton :: " + captureButton);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
Log.d(CameraLauncherActivity.LOG_TAG, "mCamera.takePicture :: " + mCamera);
}
});
Button viewButton = (Button) findViewById(R.id.btnView);
Log.d(CameraLauncherActivity.LOG_TAG, "SingleAngelActivityButton :: " + viewButton);
viewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentNewEvent = new Intent(CameraLauncherActivity.this, UploadActivity.class);
String event_id = customFolder;
intentNewEvent.putExtra("event_id", event_id);
startActivity(intentNewEvent);
}
});
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(CameraLauncherActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
// Check if Internet present
if (!hasFlash) {
// device doesn't support flash
alert.showAlertDialog(CameraLauncherActivity.this, "Sorry",
"your Device doesn't support Flash Light !", false);
// stop executing code by return
return;
}
// displaying button image
toggleButtonImage();
// flash switch button
btnSwitch = (ImageButton) findViewById(R.id.btnFlash);
btnSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
// Get event id, angel id
Intent i = getIntent();
event_id = i.getStringExtra("event_id");
angel_id = i.getStringExtra("angel_id");
// calling background thread
new LoadSingleTrack().execute();
}
/**
* Background Async Task to get single angel information
* */
class LoadSingleTrack extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(CameraLauncherActivity.this);
pDialog.setMessage("Initializing Camera...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting angel json and parsing
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// post event id, angel id as GET parameters
params.add(new BasicNameValuePair("event", event_id));
params.add(new BasicNameValuePair("angel", angel_id));
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(URL_angel, "GET",
params);
// Check your log cat for JSON reponse
Log.d("Single Track JSON: ", json);
try {
JSONObject jObj = new JSONObject(json);
if(jObj != null){
angel_name = jObj.getString(TAG_ANGEL);
event_name = jObj.getString(TAG_EVENT);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting angel information
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
TextView txt_angel_name = (TextView) findViewById(R.id.angel_title);
String timeStamp = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
txt_angel_name.setText(event_name + " Event_"+ angel_name + "-" + timeStamp);
customFolder = txt_angel_name.getText().toString();
// Change Activity Title with angel title
setTitle(angel_name);
// folder name
mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"/CaptureImages/"+ customFolder + "/");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("App", "failed to create directory");
}
}
}
});
}
}
}
logcat的: -
12-04 23:23:04.610: E/AndroidRuntime(798): FATAL EXCEPTION: main
12-04 23:23:04.610: E/AndroidRuntime(798): java.lang.NullPointerException
12-04 23:23:04.610: E/AndroidRuntime(798): at com.example.camera.CameraLauncherActivity.getOutputMediaFile(CameraLauncherActivity.java:324)
12-04 23:23:04.610: E/AndroidRuntime(798): at com.example.camera.CameraLauncherActivity$1.onPictureTaken(CameraLauncherActivity.java:301)
12-04 23:23:04.610: E/AndroidRuntime(798): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:773)
12-04 23:23:04.610: E/AndroidRuntime(798): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 23:23:04.610: E/AndroidRuntime(798): at android.os.Looper.loop(Looper.java:137)
12-04 23:23:04.610: E/AndroidRuntime(798): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-04 23:23:04.610: E/AndroidRuntime(798): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 23:23:04.610: E/AndroidRuntime(798): at java.lang.reflect.Method.invoke(Method.java:525)
12-04 23:23:04.610: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-04 23:23:04.610: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-04 23:23:04.610: E/AndroidRuntime(798): at dalvik.system.NativeStart.main(Native Method)
12-04 23:23:07.570: I/Process(798): Sending signal. PID: 798 SIG: 9
答案 0 :(得分:1)
我无法弄清楚代码的各个部分的确切执行顺序,但是该行唯一可以生成NPE的是mediaStorageDir
是null
。由于在mediaStorageDir
实例异步执行后初始化LoadSingleTrack
,我只能假设它在第324行执行后正在执行。解决这种无序的代码执行顺序,问题就会消失。
正如旁注:从runOnUiThread
的{{1}}致电onPostExecute
是没有意义的。调用AsyncTask
时,您已经在UI线程上。