从一见钟情到GPS坐标看起来很简单(伪代码):
private void onStart() {
res = ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION);
if (res != PackageManager.PERMISSION_GRANTED) {
requestPermissions();
} else {
startGps();
}
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (all_ok)
startGps();
}
private void startGps() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {...};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
Run Code Online (Sandbox Code Playgroud)
但是如何处理异步事件:
如果在locationManager.requestLocationUpdates用户暂停和恢复应用后,该怎么办
1.1我打电话locationManager.requestLocationUpdates再次收到GPS数据吗?
1.2如果用户在我的应用程序暂停时禁用GPS,用户恢复我的应用程序后会出现什么onProviderDisabled事情?
如果在locationManager.requestLocationUpdates调用之后发生ACCESS_FINE_LOCATION权限,如何处理运行时的撤销?撤销后发生了什么,android会通过onStatusChanged或报告这个onProviderDisabled吗?
我听说新的android允许在屏幕上显示多个窗口,如果一个窗口属于我的应用程序,另一个窗口属于"系统偏好设置",用户撤销ACCESS_FINE_LOCATION而没有暂停/恢复我的应用程序,会发生什么?
use std::rc::Rc;
fn f1(cb: Box<Fn(i32) -> i32>) {
let res = cb(15);
println!("res {}", res);
}
fn main() {
let mut v2 = Rc::new(5_i32);
// 1
// f1(Box::new(move |x: i32| *v2 + x));
// 2
f1(Box::new(move |x: i32| {
let tmp = *v2;
*Rc::get_mut(&mut v2).unwrap() = tmp + 1;
x + *v2
}));
}
Run Code Online (Sandbox Code Playgroud)
如果未注释,引用为“1”的代码可以正常编译并运行,但引用为“2”的代码无法编译,失败并显示消息:
use std::rc::Rc;
fn f1(cb: Box<Fn(i32) -> i32>) {
let res = cb(15);
println!("res {}", res);
}
fn main() {
let mut v2 = Rc::new(5_i32);
// …Run Code Online (Sandbox Code Playgroud) 我想用oracle jdk 1.8编译简单的代码:
class Foo {
public static void test(@NotNull String a) {}
}
Run Code Online (Sandbox Code Playgroud)
但是idea不了解这样的代码,建议添加:
import com.sun.istack.internal.NotNull;
Run Code Online (Sandbox Code Playgroud)
这里面编译后idea的作品,但如果运行构建通过gradle这样的build.gradle:
version '1.0-snaphshot_03.03.2017'
apply plugin: 'java'
targetCompatibility = '1.8'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
artifacts {
archives sourcesJar
}
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
error: package com.sun.istack.internal does not exist
import com.sun.istack.internal.NotNull;
error: cannot find …Run Code Online (Sandbox Code Playgroud) 我想实现一个跳过!或!^num位于字符串开头的算法:
fn extract_common_part(a: &str) -> Option<&str> {
let mut it = a.chars();
if it.next() != Some('!') {
return None;
}
let mut jt = it.clone().peekable();
if jt.peek() == Some(&'^') {
it.next();
jt.next();
while jt.peek().map_or(false, |v| !v.is_whitespace()) {
it.next();
jt.next();
}
it.next();
}
Some(it.as_str())
}
fn main() {
assert_eq!(extract_common_part("!^4324 1234"), Some("1234"));
assert_eq!(extract_common_part("!1234"), Some("1234"));
}
Run Code Online (Sandbox Code Playgroud)
这是可行的,但我找不到从Peekableto返回的方法Chars,所以我必须前进it和jt迭代器。这会导致重复代码。
如何从Peekable迭代器返回到相应的Chars迭代器,或者也许有更简单的方法来实现该算法?
我有一个有向图,想找到从节点 A 到节点 B 的最短路径。我在crates.io上搜索并找到了petgraph,它看起来像是最受欢迎的 crate。它实现了许多算法,但没有一个能解决我的任务。我错过了什么?
例如,Dijkstra 算法返回路径成本,但哪条路径的成本最低?该Bellman-Ford算法返回路径成本和节点,但没有路径。
这是我发现从图中打印路径的最简单方法:
extern crate petgraph;
use petgraph::prelude::*;
use petgraph::algo::dijkstra;
fn main() {
let mut graph = Graph::<&str, i32>::new();
let a = graph.add_node("a");
let b = graph.add_node("b");
let c = graph.add_node("c");
let d = graph.add_node("d");
graph.extend_with_edges(&[(a, b, 1), (b, c, 1), (c, d, 1), (a, b, 1), (b, d, 1)]);
let paths_cost = dijkstra(&graph, a, Some(d), |e| *e.weight());
println!("dijkstra {:?}", paths_cost);
let …Run Code Online (Sandbox Code Playgroud) 我读了关于android数据绑定并希望在我的应用程序中使用它,但我在xml布局阶段失败了.
我activity_main.xml喜欢这个:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/tab1"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</layout>
Run Code Online (Sandbox Code Playgroud)
和tab1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<EditText
...
Run Code Online (Sandbox Code Playgroud)
我想将数据绑定应用到最后EditText,但如果我插入
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<TabHost>
...
Run Code Online (Sandbox Code Playgroud)
这导致
activity_main.xml:9: AAPT: Error parsing XML: duplicate attribute
Run Code Online (Sandbox Code Playgroud)
问题是,我应该如何组合数据绑定并在包含的布局中TabHost绑定EditText?
传递std::array<char, N>给这样的函数的方法是什么:
template<size_t N>
void safe_func(char (&dest)[N]);
Run Code Online (Sandbox Code Playgroud)
?
我试试这个:
#include <array>
template <size_t N> using SafeArray = char[N];
template <size_t N> void safe_func(char (&dest)[N]) {}
int main() {
SafeArray<10> a1;
safe_func(a1);
std::array<char, 10> a2;
safe_func(*static_cast<SafeArray<10> *>(static_cast<void *>(a2.data())));
}
Run Code Online (Sandbox Code Playgroud)
它有效,但我怀疑,我的演员可能有问题,而在其他编译器或平台上(我使用gcc/linux/amd64),我遇到了错误的引用?
该操作可能的伪代码可能是:
fn f32_greater(x: f64) -> f32 {
let mut y = x as f32; //I get closest
while f64::from(y) < x {
y = nextafter(y, f32::INFINITY);
}
y
}
fn f32_smaller(x: f64) -> f32 {
let mut y = x as f32; //I get closest
while f64::from(y) > x {
y = nextafter(y, f32::NEG_INFINITY);
}
y
}
Run Code Online (Sandbox Code Playgroud)
我nextafter在libc板条箱或的方法中找不到等效于C11的功能f64
对于上下文,我有一个使用R树索引f32。我想使用提供为的坐标来搜索区域f64,因此我需要f32其中包含f64值的最小区域。
我遇到了类似的问题:Android Studio 64位错误:32位Linux Android模拟器二进制文件已被删除
我在Gentoo Linux/amd64上运行android studio 2.1.1,当我尝试运行android android studio的模拟器时给我错误:
/ home/user/Android/Sdk/tools/emulator -netdelay none -netspeed full -avd Nexus_5X_API_23 ERROR:32位Linux Android模拟器二进制文件已弃用,要使用它们,您必须至少执行以下操作之一: - 使用调用'模拟器'时'-force-32bit'选项. - 在您的环境中将ANDROID_EMULATOR_FORCE_32BIT设置为"true".任何一个都允许您使用32位二进制文件,但请注意,这些将在未来的Android SDK版本中消失.在此之前考虑迁移到64位Linux系统.
但
$ file/home/user/Android/Sdk/tools/emulator/home/user/Android/Sdk/tools/emulator:ELF 64位LSB可执行文件,x86-64,版本1(SYSV),动态链接,解释器/ lib64 /ld-linux-x86-64.so.2,对于GNU/Linux 2.6.15,被剥离
正如你可以看到模拟器是64位二进制,我运行64位操作系统,还有工具 - > Android-> Avd Manger显示Nexus_5X_API_23有CPU/ABI = x86_64,
那么什么是"32位错误"呢?
更新
看起来它需要一些权限,因为我可以从root用户运行模拟器.但仍无法猜测究竟需要什么.
gcc 时不时地通过 may-uninitialized 发出有关 boost::optional 工作的警告,但我认为这是误报(如此处描述的https://github.com/boostorg/optional/issues/72)。
但是现在valgrind在运行时报告了同样的问题,所以出了点问题,不知道是什么问题:
//Foo.hpp
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <cstdio>
#include <string>
extern "C" {
typedef struct FooOpaque FooOpaque;
FooOpaque *Foo_new();
void Foo_delete(const FooOpaque *self);
}
class Foo {
public:
explicit Foo(FooOpaque *o) noexcept : self_(o) {}
Foo(const Foo &) = delete;
Foo &operator=(const Foo &) = delete;
Foo(Foo &&o) noexcept : self_(o.self_) { o.self_ = nullptr; }
Foo &operator=(Foo &&o) noexcept {
assert(this != &o);
free_mem(this->self_);
self_ = o.self_;
o.self_ = nullptr;
return …Run Code Online (Sandbox Code Playgroud)