Android相机应用程序在关闭时崩溃(Android camera application crashes on closing)
我写了一个相机应用程序。 它在“onCreate”中创建了一个无限循环,每秒拍摄一张照片:
Runnable mRunnable = new Runnable() { @Override public void run() { camera.takePicture(null, null, mPicture); mHandler.postDelayed(this, 1000); } }; mHandler.postDelayed(mRunnable,1000);
之后,它运行基于caffe的分类器。 但是,当我在某个时候退出应用程序时,我总是会收到错误消息:
"App has been stopped"
同时我在
Android Monitor
收到以下错误消息:E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myUsername.myAppName, PID: 25490 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.takePicture(android.hardware.Camera$ShutterCallback, android.hardware.Camera$PictureCallback, android.hardware.Camera$PictureCallback)' on a null object reference at com.myUsername.myAppName.MainActivity$1.run(MainActivity.java:178) at android.os.Handler.handleCallback(Handler.java:746) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:702)
有谁知道如何避免这个错误? 在完成结束无限循环的应用程序或明确关闭在预览模式下运行的相机时,是否需要调用一些特殊功能?
I have written a camera application. It has an infinite loop created inside "onCreate" that takes a picture every second:
Runnable mRunnable = new Runnable() { @Override public void run() { camera.takePicture(null, null, mPicture); mHandler.postDelayed(this, 1000); } }; mHandler.postDelayed(mRunnable,1000);
After that it runs a classifier based on caffe. However when I exit the application at some point I always get an error saying:
"App has been stopped"
At the same time I get the following error message in
Android Monitor
:E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myUsername.myAppName, PID: 25490 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.takePicture(android.hardware.Camera$ShutterCallback, android.hardware.Camera$PictureCallback, android.hardware.Camera$PictureCallback)' on a null object reference at com.myUsername.myAppName.MainActivity$1.run(MainActivity.java:178) at android.os.Handler.handleCallback(Handler.java:746) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:702)
Does anyone have an idea how to avoid this error? Do I need to call some special functions when finishing the app that end the infinite loop or explicitly shut down the camera which runs in preview mode?
原文:https://stackoverflow.com/questions/39100468
最满意答案
我认为你不能用
get_posts()
来做这件事,因为get_posts()
只会调用1个查询(没有子查询),你需要一个子查询来确定要跳过哪些行,具体取决于所需的条件。您必须遵循以下代码片段:
$desiredCategory = array( 2, 6 ); $finalPostID = array(); $allPosts = get_posts( array('category__and' => $desiredCategory) ); foreach ($allPosts as $postVariable) { $categories = get_the_category($postVariable->ID); if( in_array($desiredCategory, $categories) ) $finalPostID [] = $postVariable; } // Now you can use $finalPostID as your collection of posts.
I think you cannot do this with
get_posts()
becauseget_posts()
will call only 1 query (no subquery) and you need a subquery here to determine which rows to skip depending on desired condition.You have to follow this code fragment:
$desiredCategory = array( 2, 6 ); $finalPostID = array(); $allPosts = get_posts( array('category__and' => $desiredCategory) ); foreach ($allPosts as $postVariable) { $categories = get_the_category($postVariable->ID); if( in_array($desiredCategory, $categories) ) $finalPostID [] = $postVariable; } // Now you can use $finalPostID as your collection of posts.
相关问答
更多-
将slug更改为term_id因为您的帖子请求发送了类别ID,而不是它的slug。 此外,它的posts_per_page不是numberposts So i add this if ( isset( $_POST['usecategory'] ) && intval( $_POST['usecategory'] ) != 0 ) { $args['tax_query'] = array( array( 'taxonomy' => 'portfolio-category', ...
-
您需要将分类法传递给get_the_terms() : $my_tax_terms = get_the_terms( $dev->ID, 'developer_category' ); 这将返回一个您可以循环的术语数组。 此外,你不能只是echo它,因为它是一个数组。 你可以print_r( $my_tax_terms )看看你得到了什么,但你需要循环结果来获取你想要的信息。 You need to pass the taxonomy to get_the_terms(): $my_tax_terms = ...
-
不,但您可以创建自定义循环。 检查一下。 编辑: $args = array('s' => 'keyword'); $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); //whatever you want to do with e ...
-
pre_get_posts应该用于修改主循环而不是get_posts() 。 请尝试以下方法: $myposts = get_posts( array( 'cat' => 1, 'no_found_rows' => true, 'order' => 'ASC', 'order_by' => 'post_date', 'posts_per_page' => 3, 'post_status' => 'fu ...
-
您是否回显了$cat变量以查看它包含的内容? 我不确定get_query_var('cat'); 将在archive.php页面中返回您需要的内容。 无论如何,我要求你使用: echo '
'; print_r($GLOBALS['wp_query']); echo '
'; 这样您就可以了解主查询对象及其变量。 将$cat与结果数组中的相应字段进行比较。 在所有情况下,根据$GLOBALS['wp_query']使用具有适当参数的代码源。 祝你好运 Di ... -
我认为你不能用get_posts()来做这件事,因为get_posts()只会调用1个查询(没有子查询),你需要一个子查询来确定要跳过哪些行,具体取决于所需的条件。 您必须遵循以下代码片段: $desiredCategory = array( 2, 6 ); $finalPostID = array(); $allPosts = get_posts( array('category__and' => $desiredCategory) ); foreach ($allPosts as $postVaria ...
-
为什么这个get_posts()WordPress功能不起作用?(Why isn't this get_posts() WordPress function not working?)[2024-03-04]
get_posts()函数需要传递一个数组。 尝试: $myposts = get_posts( array('category' => 3) ); 请参阅wordpress codex The get_posts() function expects an array to be passed. Try: $myposts = get_posts( array('category' => 3) ); See the wordpress codex -
为数组中的每个帖子创建计数器和拉取值。 $posts_info = array(); $counter = 0; foreach( $myposts as $post ) { setup_postdata($post); $posts_info[$counter]['title'] = get_the_title($post->ID); $posts_info[$counter]['excerpt'] = get_the_excerpt($post->ID); $posts_ ...
-
the_title()显示循环中当前帖子/页面的标题。 get_posts创建一个独立的查询,而不会影响您的主循环。 使用 post_title; ?> 显示新闻项目的标题。 或者,应用正确的过滤器会更好: post_title ); ?> 对于永久链接,请使用 ID ); ?> the_title() shows ...
-
我刚刚意识到你的get_field()缺少第二个参数。 如果您没有指定具有该名称的字段的帖子的特定ID,则它很可能会默认为当前帖子的值(如果存在)。 所以你基本上需要遍历你的get_posts()来获得当前正在发布的值,然后执行你正在寻找的功能。 公开更多可能会有帮助。 // Get your custom field from ACF based on field name and the ID of the post $catid = get_field( "id_of_product_category ...