我使用运行相机的库。需要确保相机全屏工作。为此,请写下
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize and start the bar code recognition.
initializeAndStartBarcodeScanning();
}
public void initializeAndStartBarcodeScanning() {
// Switch to full screen.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
// We instantiate the automatically adjusting barcode picker that will
// choose the correct picker to instantiate. Be aware that this picker
// should only be instantiated if the picker is shown full screen as the
// legacy picker will rotate the orientation and not properly work in
// non-fullscreen.
ScanditSDKAutoAdjustingBarcodePicker picker = new ScanditSDKAutoAdjustingBarcodePicker(
this, sScanditSdkAppKey, ScanditSDKAutoAdjustingBarcodePicker.CAMERA_FACING_BACK);
// Add both views to activity, with the scan GUI on top.
setContentView(picker);
mBarcodePicker = picker;
// Register listener, in order to be notified about relevant events
// (e.g. a successfully scanned bar code).
mBarcodePicker.getOverlayView().addListener(this);
// Show a search bar in the scan user interface.
mBarcodePicker.getOverlayView().showSearchBar(true);
}
并收到错误
02-03 16:08:59.089 6890-6890/com.skip.client.customer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.skip.client.customer, PID: 6890
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skip.client.customer/com.skip.client.customer.activities.ScanActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:249)
at android.app.Activity.requestWindowFeature(Activity.java:3371)
at android.support.v7.app.ActionBarActivityDelegateBase.supportRequestWindowFeature(ActionBarActivityDelegateBase.java:460)
at android.support.v7.app.ActionBarActivity.supportRequestWindowFeature(ActionBarActivity.java:194)
at com.skip.client.customer.activities.ScanActivity.initializeAndStartBarcodeScanning(ScanActivity.java:53)
at com.skip.client.customer.activities.ScanActivity.onCreate(ScanActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5411)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
行上的错误
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
如果我在方法之前调用此行,则所有工作
@Override
protected void onCreate(Bundle savedInstanceState) {
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// Initialize and start the bar code recognition.
initializeAndStartBarcodeScanning();
}
但是你不能这样做?我是对的吗?
答案 0 :(得分:1)
因为您在supportRequestWindowFeature
之前调用了super.onCreate
,所以无需在initializeAndStartBarcodeScanning
方法中再次调用它。
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// Initialize and start the bar code recognition.
initializeAndStartBarcodeScanning();
}
public void initializeAndStartBarcodeScanning() {
.....
}