我是新手,Dagger2并试图建立这样的样本,以了解它是如何工作的.
有我的示例代码:
MainActivity
public class MainActivity extends AppCompatActivity {
@Inject
protected ApiInterface apiInterface;
@Inject
protected Integer valueInt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
App.getComponent().inject(this);
}
public void testButton(View view) {
if (apiInterface == null || valueInt == null) {
Log.e("TAG", "apiInterface == null");
} else {
Log.e("TAG", "apiInterface != null : " + apiInterface.value + " : " + valueInt);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Component
@Singleton
@Component(modules = {ModelModule.class, AnotherModule.class})
interface AppComponent {
void inject(MainActivity mainActivity);
} …Run Code Online (Sandbox Code Playgroud) 我有一个遍历整个目录的循环,我需要从每个文件获取路径才能使用它。
这是我的方法
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
for (const auto & entry : fs::directory_iterator(sourceDir))
{
if (fs::is_directory(entry))
continue;
StreamRAII iStream{ entry, StreamMode::READ_BINARY };
}
Run Code Online (Sandbox Code Playgroud)
问题是StreamRAII构造函数必须将std::string其第一个参数作为
StreamRAII(const std::string& filename, const char *mode);
Run Code Online (Sandbox Code Playgroud)
但是,这const auto & entry : fs::directory_iterator(sourceDir),entry是derectory_entry类型。
问题是,如何获取文件路径?怎么转换derectory_entry成std::stringor char[]?
PS,当我使用时,cout我可以看到此文件的路径...
据我所知,我遇到这个问题是因为我的代码有问题,但我可以找到确切的原因
有我的代码
public class MainActivity extends AppCompatActivity {
private MyServerConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String URL = ...;
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
connection = retrofit.create(MyServerConnection.class);
}
interface MyServerConnection {
@POST("/appreg")
Call<JSONObject> postWithJson(@Body JSONObject object);
}
public void test(View view) {
Log.e("TAG", "---!!! TEST !!!---");
final Call<JSONObject> call = connection.postWithJson(getJson());
call.enqueue(new Callback<JSONObject>() {
@Override
public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
Log.e("Response status code: ", String.valueOf(response.code()));
// isSuccess is true if …Run Code Online (Sandbox Code Playgroud) 有这样的问题,我有水平RecyclerView,每个单元格小于屏幕宽度。
所以我在这里找到了解决方案
RecyclerVIew 自动滚动以显示新闻提要等中的所有元素,
如果一个单元格占据屏幕的整个宽度,则所有工作都很好,否则(如果每个单元格占据屏幕宽度的 95%),每次自动滑动都将单元格放在屏幕的初学者(右侧)处,这是合乎逻辑的。所以在一个可见单元格的末尾它是另一个单元格的开始
它看起来不太好。我需要这个单元格像这样位于屏幕中间。
我需要查看上一个单元格 - 当前 - 下一个
现在我想解释一些魔法如何使当前平滑滚动(正如我在上面的链接中提到的)
我的这个方法 CustomLinnearLayoutManager
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position)
{
LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext())
{
@Override
public PointF computeScrollVectorForPosition(int targetPosition)
{
return SmoothLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics)
{
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
Run Code Online (Sandbox Code Playgroud)
但是这种方法可以在没有偏移的情况下工作
我发现了另一种可以提供所需偏移量的方法
scrollToPositionWithOffset(final int position, final int offset)
Run Code Online (Sandbox Code Playgroud)
它看起来正是我所需要的,但是这种方法在没有平滑动画的情况下也能工作。
所以,最终我的问题是:如何将动画逻辑从第一种方法应用到第二种方法(带有偏移量)
随意问
我有以下问题:我有两组不同的文件(主文件和另一组文件),我希望将它们分开。因此,我有一组文件(主文件),我以这种方式配置:
set(libplayersource
....
)
add_library( # Sets the name of the library.
libplayer
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${libplayersource})
Run Code Online (Sandbox Code Playgroud)
然后,我有了第二组文件(其他文件),我以这种方式配置:
set(codec_source
...)
add_library(libcodec SHARED ${codec_source})
Run Code Online (Sandbox Code Playgroud)
最终,我需要链接这两套文件:
target_link_libraries( # Specifies the target library.
libplayer
libcodec)
Run Code Online (Sandbox Code Playgroud)
在完成此配置之后,我还需要包含loglib才能使其正常运行。首先,我需要找到该log库,然后将其包含在本机库中。
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
Run Code Online (Sandbox Code Playgroud)
另外,我应该进行编辑target_link_libraries …
我有这样的方法
public int foo()
{
return (int) MainJsonValues[JsonKeys.DP_PORT.Value];
}
Run Code Online (Sandbox Code Playgroud)
此方法应进行强制转换并返回此值
我是如何调用这个方法的
myObj.foo()
Run Code Online (Sandbox Code Playgroud)
所以,这很奇怪,因为我们可以看到 value 包含在 Dictionary
您可以在屏幕截图上看到我的调试
所以,据我所知,这个方法不能转换long为int...
错误
我究竟做错了什么?
我尝试将 C++ 代码转换为 Swift。我有一个用C++编写的方法(简化示例)
std::vector<int> list;
void foo() {
...
while(isOk) {
list.resize(10000); //resize do it's job only if list.capasity < requested size
for(int i = 0; i < 10000; i++) {
list[i] = i;
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
迅捷的一面:
var arr: [Int] = []
do {
while true {
arr.reserveCapacity(10000)
for i in 0..<10000 {
arr[i] = i
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
错误:执行被中断,原因:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)。
根据文档reserveCapacity分配请求的大小,但是为什么当我尝试向特定单元格写入内容时出现错误?
** 看看编辑部分**
我有一个界面
IInterface<TYPE_ONE, TYPE_TWO> where TYPE_TWO : string
{
void foo();
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个 BaseClass
class BaseClass<TYPE_ONE, TYPE_TWO> where TYPE_TWO: string
Run Code Online (Sandbox Code Playgroud)
现在我想在BaseClass,IInterfase
class BaseClass<TYPE_ONE, TYPE_TWO> where TYPE_TWO: string, IInterface<TYPE_ONE, TYPE_TWO>{}
Run Code Online (Sandbox Code Playgroud)
问题是 - 我希望 VisualStudio 告诉我
你必须实现 IInterface 方法 foo()
但是我没有收到任何这样的通知......而且我能够编译项目......在我看来这很奇怪,因为我有一个实现接口的类,但不是它的方法。
我在这里做错了什么?
编辑
我的实际代码
interface ILooperable<in TASK_TYPE, in CALLBACK_TYPE> where CALLBACK_TYPE : ILooperableCallback<TASK_TYPE>
{
void Start();
void Put(TASK_TYPE task);
void DeleteTask(TASK_TYPE task);
void SetCallback(CALLBACK_TYPE callback);
void Quit();
}
Run Code Online (Sandbox Code Playgroud)
interface ILooperableCallback<in TASK_TYPE>
{
void Finish(TASK_TYPE task);
}
Run Code Online (Sandbox Code Playgroud)
class BaseLooper<TASK_TYPE, …Run Code Online (Sandbox Code Playgroud) 我有这样的结构名称
#define SETA_NAME "SETA"
#define SETB_NAME "SETB"
#define SETC_NAME "SETC"
#define SETD_NAME "SETD"
#define SETE_NAME "SETE"
#define SETF_NAME "SETF"
Run Code Online (Sandbox Code Playgroud)
这样的结构
struct set
{
char name[SET_NAME_LENGTH + 1];
unsigned int input[INPUT_ARR_SIZE];
};
typedef struct set SETA, SETB, SETC, SETD, SETE, SETF;
Run Code Online (Sandbox Code Playgroud)
这是我尝试执行的代码
SETA setA;
memcpy(setA.name, SETA_NAME, sizeof(SETA_NAME));
SETB setB;
memcpy(setA.name, SETB_NAME, sizeof(SETB_NAME));
SETC setC;
memcpy(setA.name, SETC_NAME, sizeof(SETC_NAME));
SETD setD;
memcpy(setA.name, SETD_NAME, sizeof(SETD_NAME));
SETE setE;
memcpy(setA.name, SETE_NAME, sizeof(SETE_NAME));
SETF setF;
memcpy(setA.name, SETF_NAME, sizeof(SETF_NAME));
printf("\nNAME : %s", setA.name);
printf("\nNAME : %s", setB.name);
printf("\nNAME …Run Code Online (Sandbox Code Playgroud) 我在 SO 上找到了这个答案
我在回答中做了一切,但我收到一个错误
有我的代码
我需要传递回调的方法
/*static*/ void Utils::copy_files(std::function<void(int, int)> progress_callback,
std::string const & path_from,
std::string const & path_to)
{
....
}
Run Code Online (Sandbox Code Playgroud)
我的回调实现
void TV_DepthCamAgent::progress_callback(int count, int copied_file)
{
...
}
Run Code Online (Sandbox Code Playgroud)
用法
void TV_DepthCamAgent::foo()
{
...
auto callback = std::bind(&TV_DepthCamAgent::progress_callback,
this);
shared::Utils::copy_files(callback, path_from_copy, path_to_copy);
...
}
Run Code Online (Sandbox Code Playgroud)
我得到了一个错误
SE0312 不存在从“std::_Binder”到“std::function”的合适的用户定义转换
错误 C2664“void shared::Utils::copy_files(std::function,const std::string &,const std::string &)”:无法将参数 1 从“std::_Binder”转换为“std::function” '
我究竟做错了什么?