0
這裏是我的代碼有問題, 下面是完整的代碼BringToFront強制關閉應用程序
public class CameraFragment extends Fragment implements OrientationListener {
private Camera mCamera;
private CameraPreview mPreview;
FrameLayout previewFrameLayout;
private Button cameraButton;
private Button cancelButton;
private Button addButton;
private Button retakeButton;
private Button doneButton;
//coach mark variable
private boolean cocahMarkVisible=true;
private RelativeLayout coachMarkTopLayout;
private TextView coachText;
private ImageView arrowImage;
private LinearLayout cameraPreviewCenterTop;
private LinearLayout cameraPreviewCenterBottom;
private LinearLayout cameraBtnLayout;
private LinearLayout addSectionBtnLayout;
private LinearLayout receiptPreviewLayout;
private ImageView receiptPreviewImage;
private TextView receiptPreviewLabelText;
private TextView cameraPreviewLabelText;
private LinearLayout keepFlatLayout;
private TextView keepFlatTxt;
private TextView receiptEdgeLeft;
private TextView receiptEdgeRight;
private LinearLayout headerView;
private TextView headerTextView;
private boolean cameraFlat = false;
private boolean addSectionDisplayed = false;
private boolean orientationListenerEnabled = true;
private String lastImageFile;
private List<String> imgPaths = new ArrayList<String>();
private static int MAX_NUM_OF_RECEIPTS = 5;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.fragment_camera, container, false);
//View view2 = inflater
// .inflate(R.layout.camera_coachmark, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupViews();
//showInstructionsDialog(false);
}
private void setupViews() {
RelativeLayout fragmentLayout = (RelativeLayout) getActivity().findViewById(R.id.CameraFragmentLayout);
ImageUtils.fixBackgroundRepeat(fragmentLayout);
// Create an instance of Camera
mCamera = getCameraInstance();
setCameraParams(mCamera);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(getActivity(), mCamera);
previewFrameLayout = (FrameLayout) getActivity().findViewById(R.id.camera_preview);
previewFrameLayout.addView(mPreview);
setView();
//coachMarkTopLayout = (RelativeLayout)getActivity().findViewById(R.id.coach_mark_top_layout);
//coachMarkTopLayout.setVisibility(View.VISIBLE);
//coachMarkTopLayout.bringToFront();
//to start make receipt preview hided
if(receiptPreviewLayout != null) {
receiptPreviewLayout.setVisibility(View.GONE);
addSectionBtnLayout.setVisibility(View.GONE);
cameraBtnLayout.setVisibility(View.VISIBLE);
addSectionDisplayed = false;
keepFlatLayout.setVisibility(View.VISIBLE);
keepFlatLayout.bringToFront();
keepFlatTxt.setText(R.string.camera_keep_straight);
keepFlatTxt.bringToFront();
if(cocahMarkVisible == true)
{
coachMarkTopLayout.setVisibility(View.VISIBLE);
coachMarkTopLayout.bringToFront();
}
}
//coachMarkTopLayout.bringToFront();
headerView = (LinearLayout)getActivity().findViewById(R.id.message_header_layout_id);
headerTextView = (TextView)headerView.findViewById(R.id.message_header_textview);
if(NetworkHelper.isOffline(getActivity())) {
enableHeaderAndFooter(R.string.offerslist_no_internet);
} else {
disableHeaderAndFooter();
}
}
protected void enableHeaderAndFooter(int msgId) {
headerView.setVisibility(View.VISIBLE);
headerTextView.setText(msgId);
headerView.bringToFront();
}
protected void disableHeaderAndFooter() {
headerView.setVisibility(View.GONE);
}
private void setCameraParams(Camera mCameraInstance) {
if (mCamera == null)
return;
// set orientation
mCamera.setDisplayOrientation(90);
// set Camera parameters
Camera.Parameters params = mCamera.getParameters();
// following is supported only from android 4.0
/*
* if (params.getMaxNumMeteringAreas() > 0){ // check that metering
* areas are supported List<Camera.Area> meteringAreas = new
* ArrayList<Camera.Area>();
*
* Rect areaRect1 = new Rect(-100, -100, 100, 100); // specify an area
* in center of image meteringAreas.add(new Camera.Area(areaRect1,
* 600)); // set weight to 60% Rect areaRect2 = new Rect(800, -1000,
* 1000, -800); // specify an area in upper right of image
* meteringAreas.add(new Camera.Area(areaRect2, 400)); // set weight to
* 40% params.setMeteringAreas(meteringAreas); }
*/
List<String> focusModes = params.getSupportedFocusModes();
if (focusModes != null && focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
mCamera.setParameters(params);
}
private void cancelButtonPress() {
cancelButton.setEnabled(false);
getActivity().finish();
}
private void cameraButtonPress() {
cameraButton.setEnabled(false);
// only process if camera is flat
if (cameraFlat) {
orientationListenerEnabled = false;
enableAquiringFocus(R.string.camera_acquire_focus);
mCamera.autoFocus(myAutoFocusCallback);
}
}
private void afterPictureTaken() {
try {
if(mCamera != null) {
mCamera.stopPreview();
}
} catch(Exception ex) {
Log.e(Constant.CARTPERK_LOG_TAG, "Not able to stop preview after picture taken");
}
orientationListenerEnabled = true;
keepFlatLayout.setVisibility(View.GONE);
cameraBtnLayout.setVisibility(View.GONE);
addSectionBtnLayout.setVisibility(View.VISIBLE);
retakeButton.setEnabled(true);
addButton.setEnabled(true);
doneButton.setEnabled(true);
addSectionDisplayed = true;
if(this.imgPaths.size() >= MAX_NUM_OF_RECEIPTS) {
addButton.setEnabled(false);
}
//cameraPreviewCenterTop.setBackground(new ColorDrawable(R.color.camera_screen_bg_color));
cameraPreviewCenterTop.setBackgroundColor(getResources().getColor(R.color.camera_screen_bg_color));
}
private void retakeButtonPress() {
retakeButton.setEnabled(false);
addButton.setEnabled(false);
doneButton.setEnabled(false);
addSectionDisplayed = false;
// remove last stored path
if (imgPaths.size() > 0) {
imgPaths.remove(imgPaths.size() - 1);
}
//if there are no images, then remove receipt preview
if(imgPaths.size() == 0) {
receiptPreviewLayout.setVisibility(View.GONE);
}
addSectionBtnLayout.setVisibility(View.GONE);
cameraBtnLayout.setVisibility(View.VISIBLE);
cameraButton.setEnabled(true);
orientationListenerEnabled = true;
keepFlatLayout.setVisibility(View.VISIBLE);
cameraPreviewLabelText.setText(String.valueOf(1 + imgPaths.size()));
cameraPreviewCenterTop.setBackgroundColor(getResources().getColor(R.color.camera_screen_bg_semi_color));
mCamera.startPreview();
}
private void addSectionButtonPress() {
retakeButton.setEnabled(false);
addButton.setEnabled(false);
doneButton.setEnabled(false);
addSectionDisplayed = false;
receiptPreviewLayout.setVisibility(View.VISIBLE);
receiptPreviewLabelText.setText(String.valueOf(imgPaths.size()));
// show portion of existing receipt
// show image
if (lastImageFile != null) {
Bitmap lastRcptBitmap = ImageUtils.createScaledRotatedGreyedImage(lastImageFile, getActivity(), 90f);
Bitmap croppedBitmap = ImageUtils.croppedImageForImageView(lastRcptBitmap);
Bitmap frameWidthAdjustedImage = ImageUtils.adjustFrameLayoutWidthWithZoom(croppedBitmap);
receiptPreviewImage.setImageBitmap(frameWidthAdjustedImage);
receiptPreviewImage.setScaleType(ScaleType.FIT_XY);
}
addSectionBtnLayout.setVisibility(View.GONE);
cameraBtnLayout.setVisibility(View.VISIBLE);
cameraButton.setEnabled(true);
orientationListenerEnabled = true;
keepFlatLayout.setVisibility(View.VISIBLE);
cameraPreviewLabelText.setText(String.valueOf(1 + imgPaths.size()));
cameraPreviewCenterTop.setBackgroundColor(getResources().getColor(R.color.camera_screen_bg_semi_color));
// start preview
mCamera.startPreview();
}
private void doneButtonPress() {
retakeButton.setEnabled(false);
addButton.setEnabled(false);
doneButton.setEnabled(false);
orientationListenerEnabled = false;
enableAquiringFocus(R.string.camera_process_receipts);
submitPictures();
}
public void onReceiptHandlerError() {
//keepFlatLayout.setVisibility(View.VISIBLE);
orientationListenerEnabled = true;
retakeButton.setEnabled(true);
addButton.setEnabled(true);
doneButton.setEnabled(true);
}
public LinearLayout getKeepFlatLayout() {
return this.keepFlatLayout;
}
private void setView() {
cameraButton = (Button)getActivity().findViewById(R.id.btn_camera);
cancelButton = (Button)getActivity().findViewById(R.id.btn_cancel);
addButton = (Button)getActivity().findViewById(R.id.btn_add);
retakeButton = (Button)getActivity().findViewById(R.id.btn_retake);
doneButton = (Button)getActivity().findViewById(R.id.btn_done);
cameraPreviewCenterTop = (LinearLayout)getActivity().findViewById(R.id.camera_preview_center_top_area);
cameraPreviewCenterBottom = (LinearLayout)getActivity().findViewById(R.id.camera_preview_center_bottom_area);
cameraBtnLayout = (LinearLayout)getActivity().findViewById(R.id.btn_camera_layout);
addSectionBtnLayout = (LinearLayout)getActivity().findViewById(R.id.btn_add_section_layout);
receiptPreviewLayout = (LinearLayout)getActivity().findViewById(R.id.receipt_preview_area);
receiptPreviewImage = (ImageView)getActivity().findViewById(R.id.camera_receipt_image);
receiptPreviewLabelText = (TextView)getActivity().findViewById(R.id.receipt_preview_label1);
cameraPreviewLabelText = (TextView)getActivity().findViewById(R.id.camera_preview_label1);
keepFlatLayout = (LinearLayout)getActivity().findViewById(R.id.keep_flat_layout);
keepFlatTxt = (TextView)getActivity().findViewById(R.id.keep_flat_label);
receiptEdgeLeft = (TextView)getActivity().findViewById(R.id.camera_receipt_edge_label1);
receiptEdgeRight = (TextView)getActivity().findViewById(R.id.camera_receipt_edge_label2);
coachMarkTopLayout = (RelativeLayout)getActivity().findViewById(R.id.coach_mark_top_layout);
coachText = (TextView)getActivity().findViewById(R.id.coach_mark_add_msg);
arrowImage = (ImageView)getActivity().findViewById(R.id.arrow_camera_msg);
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cameraButtonPress();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelButtonPress();
}
});
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addSectionButtonPress();
}
});
retakeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
retakeButtonPress();
}
});
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doneButtonPress();
}
});
}
private void submitPictures() {
if(FacebookHelper.isUserLoggedIn(getActivity().getApplicationContext())) {
ReceiptHandler receiptHandler = new ReceiptHandler(this.imgPaths, this.getActivity().getApplicationContext());
receiptHandler.submitAnduploadReceipt();
//show dialog with message, receipt submission in background
keepFlatLayout.setVisibility(View.GONE);
showDialog(R.string.camera_receipt_in_background, true, null);
} else {
previewFrameLayout.removeView(mPreview);
previewFrameLayout.setVisibility(View.INVISIBLE);
Intent intentLogin = new Intent(getActivity(), LoginActivity.class);
intentLogin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentLogin.putExtra("AFTER_LOGIN_STEP", Constant.AFTER_LOGIN_CAMERA);
startActivityForResult(intentLogin, Constant.REQUEST_CODE_FROM_CAMERA);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(Constant.CARTPERK_LOG_TAG, "=================requestCode==="+requestCode+"==========resultCode="+resultCode);
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == Constant.REQUEST_CODE_FROM_CAMERA) {
previewFrameLayout.removeView(mPreview);
previewFrameLayout.setVisibility(View.INVISIBLE);
if(resultCode == Activity.RESULT_OK) {
Log.d(Constant.CARTPERK_LOG_TAG, "========Inside RESULT_OK=========requestCode==="+requestCode+"==========resultCode="+resultCode);
try {
if(mCamera != null) {
mCamera.stopPreview();
}
} catch(Exception ex) {
Log.e(Constant.CARTPERK_LOG_TAG, "Not able to stop preview after picture taken");
}
ReceiptHandler receiptHandler = new ReceiptHandler(this.imgPaths, this.getActivity().getApplicationContext());
receiptHandler.submitAnduploadReceipt();
//show dialog with message, receipt submission in background
keepFlatLayout.setVisibility(View.GONE);
//showDialog(R.string.camera_receipt_in_background, true, null);
} else {
Log.d(Constant.CARTPERK_LOG_TAG, "========Inside not RESULT_OK=========requestCode==="+requestCode+"==========resultCode="+resultCode);
try {
if(mCamera != null) {
mCamera.stopPreview();
}
} catch(Exception ex) {
Log.e(Constant.CARTPERK_LOG_TAG, "Not able to stop preview after picture taken");
}
keepFlatLayout.setVisibility(View.GONE);
//showDialog(R.string.camera_receipts_error, true, null);
}
}
}
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
previewFrameLayout.removeView(mPreview);
mPreview = null;
}
}
private void resumeCamera() {
if (mCamera == null) {
mCamera = getCameraInstance();
setCameraParams(mCamera);
mPreview = new CameraPreview(getActivity(), mCamera);
previewFrameLayout.addView(mPreview);
}
}
private void takePicture() {
// get an image from the camera
// mCamera.takePicture(null, null, mPicture);
mCamera.takePicture(myShutterCallback, myPictureCallback_RAW, mPicture);
}
AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean arg0, Camera arg1) {
// TODO Auto-generated method stub
takePicture();
}
};
ShutterCallback myShutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
// TODO Auto-generated method stub
}
};
PictureCallback myPictureCallback_RAW = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}
};
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = ImageHelper.getOutputMediaFile(ImageHelper.MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
Log.d(Constant.CARTPERK_LOG_TAG,
"Error creating media file, check storage permissions: ");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(Constant.CARTPERK_LOG_TAG,
"File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(Constant.CARTPERK_LOG_TAG,
"Error accessing file: " + e.getMessage());
}
Log.d(Constant.CARTPERK_LOG_TAG, "==================myPicture=======");
// mCamera.startPreview();
lastImageFile = pictureFile.getAbsolutePath();
imgPaths.add(lastImageFile);
afterPictureTaken();
}
};
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(ImageHelper.getOutputMediaFile(type));
}
private void enableAquiringFocus(int messageId) {
keepFlatLayout.setVisibility(View.VISIBLE);
keepFlatTxt.setText(messageId);
}
private void disableAquiringFocus() {
}
public void onResume() {
super.onResume();
//resumeCamera
resumeCamera();
boolean isOrientationSupported = OrientationManager.isSupported();
//for testing
//isOrientationSupported = false;
if (isOrientationSupported) {
OrientationManager.startListening(this);
} else {
//mark it as flat always
cameraFlat = true;
//show same message
keepFlatTxt.setText(R.string.camera_keep_straight);
}
}
public void onDestroy() {
super.onDestroy();
if (OrientationManager.isListening()) {
OrientationManager.stopListening();
}
}
@Override
public void onPause() {
super.onPause();
releaseCamera(); // release the camera immediately on pause event
}
@Override
public void onOrientationChanged(float azimuth, float pitch, float roll) {
// to show actual values
}
@Override
public void onBottomUp() {
// Toast.makeText(getActivity(), "Bottom UP", 1000).show();
}
@Override
public void onLeftUp() {
// Toast.makeText(getActivity(), "Left UP", 1000).show();
}
@Override
public void onRightUp() {
// Toast.makeText(getActivity(), "Right UP", 1000).show();
}
@Override
public void onTopUp() {
// Toast.makeText(getActivity(), "Top UP", 1000).show();
}
@Override
public void onNotFlat() {
cameraFlat = false;
if(orientationListenerEnabled) {
cameraButton.setEnabled(false);
if (!addSectionDisplayed) {
keepFlatTxt.setText(R.string.camera_keep_straight);
keepFlatLayout.bringToFront();
keepFlatTxt.bringToFront();
}
}
if(this.imgPaths.size() >= MAX_NUM_OF_RECEIPTS) {
}
}
@Override
public void onFlat() {
if(orientationListenerEnabled) {
cameraFlat = true;
cameraButton.setEnabled(true);
keepFlatTxt.setText(R.string.camera_is_straight);
keepFlatLayout.bringToFront();
keepFlatTxt.bringToFront();
}
if(this.imgPaths.size() >= MAX_NUM_OF_RECEIPTS) {
}
}
,併爲下圖所示的logcat如果我刪除了那個bringtofront線一切正常
03-25 08:57:00.184: D/CARTPERK APP(7910): ====OfferItemMain==Inside setOfferMain====0=====Oats Idli
03-25 08:57:00.184: D/CARTPERK APP(7910): =======Inside OffersGridView getView step4======0
03-25 08:57:00.184: D/CARTPERK APP(7910): ==================Inside loadDataForScrollDown==============
03-25 08:57:00.184: I/Pontiflex SDK(7910): No storage file found
03-25 08:57:00.364: D/AndroidRuntime(7910): Shutting down VM
03-25 08:57:00.364: E/AndroidRuntime(7910): FATAL EXCEPTION: main
03-25 08:57:00.364: E/AndroidRuntime(7910): Process: com.cartperk.android.cartperk, PID: 7910
03-25 08:57:00.364: E/AndroidRuntime(7910): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cartperk.android.cartperk/com.cartperk.android.cartperk.ui.CameraActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.os.Looper.loop(Looper.java:136)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-25 08:57:00.364: E/AndroidRuntime(7910): at java.lang.reflect.Method.invoke(Native Method)
03-25 08:57:00.364: E/AndroidRuntime(7910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-25 08:57:00.364: E/AndroidRuntime(7910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-25 08:57:00.364: E/AndroidRuntime(7910): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference
03-25 08:57:00.364: E/AndroidRuntime(7910): at com.cartperk.android.cartperk.ui.CameraFragment.setupViews(CameraFragment.java:154)
03-25 08:57:00.364: E/AndroidRuntime(7910): at com.cartperk.android.cartperk.ui.CameraFragment.onActivityCreated(CameraFragment.java:115)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:566)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.app.Activity.performStart(Activity.java:5241)
03-25 08:57:00.364: E/AndroidRuntime(7910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
03-25 08:57:00.364: E/AndroidRuntime(7910): ... 9 more
正如你我已經標註了行評論我怎麼能解決這個
郵政logcat的,所以我們可以當場你的問題的原因 – Geros
@ Glenn--我發佈了日誌貓 –
@AbhishekKGowda你正在拋出一個'NullPoi 'CameraFragment.setupViews'中的'154'線上的nterException。它看起來像'coachMarkTopLayout'是'null'。你可以在你初始化'coachMarkTopLayout'的地方發佈你的代碼部分嗎? – adneal