我一直在研究Android SDK平台,有点不清楚如何保存应用程序的状态.因此,考虑到'Hello,Android'示例的这种小型重新设计:
package com.android.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
private TextView mTextView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = new TextView(this);
if (savedInstanceState == null) {
mTextView.setText("Welcome to HelloAndroid!");
} else {
mTextView.setText("Welcome back.");
}
setContentView(mTextView);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这对于最简单的情况就足够了,但无论我如何远离应用程序,它总是以第一条消息响应.
我确信解决方案就像覆盖onPause或类似的那样简单,但我已经在文档中捅了大约30分钟左右,并且没有找到任何明显的东西.
我的Google-fu让我失望了.
在Python中,以下两个相等的测试是否等效?
n = 5
# Test one.
if n == 5:
print 'Yay!'
# Test two.
if n is 5:
print 'Yay!'
Run Code Online (Sandbox Code Playgroud)
对于您要比较实例的对象(list比如说),这是否适用?
好的,所以这样的答案我的问题:
L = []
L.append(1)
if L == [1]:
print 'Yay!'
# Holds true, but...
if L is [1]:
print 'Yay!'
# Doesn't.
Run Code Online (Sandbox Code Playgroud)
所以==测试值测试的地方is是否是同一个对象?
我正在将一个最初为Win32 API编写的游戏移植到Linux上(好吧,将Win32端口的OS X端口移植到Linux).
我已经QueryPerformanceCounter通过在进程启动后给出uSeconds来实现:
BOOL QueryPerformanceCounter(LARGE_INTEGER* performanceCount)
{
gettimeofday(¤tTimeVal, NULL);
performanceCount->QuadPart = (currentTimeVal.tv_sec - startTimeVal.tv_sec);
performanceCount->QuadPart *= (1000 * 1000);
performanceCount->QuadPart += (currentTimeVal.tv_usec - startTimeVal.tv_usec);
return true;
}
Run Code Online (Sandbox Code Playgroud)
这一点,加上QueryPerformanceFrequency()给出一个恒定的1000000作为频率,在我的机器上工作得很好,给我一个包含uSeconds自程序启动以来的64位变量.
所以,这是便携式?如果内核是以某种方式或类似的方式编译的,我不想发现它的工作方式不同.不过,我很好,因为它不适用于Linux之外的其他东西.
诸如MRTG之类的工具为特定接口(例如eth0)上的当前网络利用率提供网络吞吐量/带宽图.如何在Linux/UNIX上的命令行中返回该信息?
优选地,除了作为标准的系统上可用的东西之外,这不会安装任何东西.
何时在签名变量上使用无符号变量是否合适?怎么样在一个for循环?
我听到很多关于此的意见,我想看看是否有任何类似的共识.
for (unsigned int i = 0; i < someThing.length(); i++) {
SomeThing var = someThing.at(i);
// You get the idea.
}
Run Code Online (Sandbox Code Playgroud)
我知道Java没有无符号值,这一定是Sun Microsystems的一个有说服力的决定.
所以我最近一直在讨论C#,所有的Generic Collections让我有些困惑.假设我想表示一个数据结构,其中树的头是一个键值对,然后在它下面有一个可选的键值对列表(但没有比这些更多的级别).这适合吗?
public class TokenTree
{
public TokenTree()
{
/* I must admit to not fully understanding this,
* I got it from msdn. As far as I can tell, IDictionary is an
* interface, and Dictionary is the default implementation of
* that interface, right?
*/
SubPairs = new Dictionary<string, string>();
}
public string Key;
public string Value;
public IDictionary<string, string> SubPairs;
}
Run Code Online (Sandbox Code Playgroud)
传递数据只是一个简单的分流.
这不是一个设计问题,实际上,虽然看起来像它.(好吧,好吧,这是一个设计问题).我想知道的是为什么C++ std::fstream类不会std::string在构造函数或开放方法中使用它们.每个人都喜欢代码示例:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string filename = "testfile";
std::ifstream fin;
fin.open(filename.c_str()); // Works just fine.
fin.close();
//fin.open(filename); // Error: no such method.
//fin.close();
}
Run Code Online (Sandbox Code Playgroud)
这使我一直处理文件.当然,C++库会std::string尽可能使用?
我一直在尝试MessageBox使用GTK 实现Win32 .该应用程序使用SDL/OpenGL,因此这不是一个GTK应用程序.
我gtk_init在MessageBox函数内处理initialisation()类的东西,如下所示:
int MessageBox(HWND hwnd, const char* text, const char* caption, UINT type)
{
GtkWidget *window = NULL;
GtkWidget *dialog = NULL;
gtk_init(>kArgc, >kArgv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event), NULL);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
// gcallback calls gtk_main_quit()
gtk_init_add((GtkFunction)gcallback, NULL);
if (type & MB_YESNO) {
dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, text);
} else {
dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, text);
}
gtk_window_set_title(GTK_WINDOW(dialog), caption);
gint result = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_main();
gtk_widget_destroy(dialog); …Run Code Online (Sandbox Code Playgroud) 所以大多数Java资源在谈到软件包时都提到了一个com.yourcompany.project设置.但是,我不为公司工作,也没有网站.是否有常见的命名约定?也许是一个电子邮件地址?
如果我创建一个这样的类:
// B.h
#ifndef _B_H_
#define _B_H_
class B
{
private:
int x;
int y;
};
#endif // _B_H_
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
// main.cpp
#include <iostream>
#include <vector>
class B; // Forward declaration.
class A
{
public:
A() {
std::cout << v.size() << std::endl;
}
private:
std::vector<B> v;
};
int main()
{
A a;
}
Run Code Online (Sandbox Code Playgroud)
编译时编译器失败main.cpp.现在我知道的解决方案是#include "B.h",但我很好奇为什么它失败了.无论是g++或cl的错误信息都在这个问题很有启发.