最后,我尝试了一些仿制药.我想出了这段代码:
public class Test {
static <T> void f(T x) {
x = (T) (Integer) 1234;
System.out.println(x);
}
public static void main(String[] args) {
f("a");
f(1);
f('a');
f(1.5);
f(new LinkedList<String>());
f(new HashMap<String, String>());
}
}
Run Code Online (Sandbox Code Playgroud)
我跑了这个并得到了这个输出:
1234
1234
1234
1234
1234
1234
Run Code Online (Sandbox Code Playgroud)
没有例外!这怎么可能?
看看这些代码:
问题
当我点击"主页按钮"时,exception抛出:Activity泄漏了窗口......从这一行:
localWindowManager.addView(colourView, layoutParams);
Run Code Online (Sandbox Code Playgroud)
问题(S)
你知道是什么原因引起的吗?
当我用后退按钮关闭应用程序时,问题不会发生.
异常/错误日志
W/InputManagerService( 96): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@40908148 (uid=10056 pid=1368)
D/CordovaActivity( 1368): CordovaActivity.onDestroy()
D/CordovaWebView( 1368): >>> loadUrlNow()
E/WindowManager( 1368): Activity com.phonegap.helloworld.HelloWorld has leaked window pl.edu.uj.tcs.student.xxx.Display$Layer@40589368 that was originally added here
E/WindowManager( 1368): android.view.WindowLeaked: Activity com.phonegap.helloworld.HelloWorld has leaked window pl.edu.uj.tcs.student.xxx.Display$Layer@40589368 that was originally added here
E/WindowManager( 1368): at android.view.ViewRoot.<init>(ViewRoot.java:258)
E/WindowManager( 1368): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
E/WindowManager( 1368): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/WindowManager( 1368): at android.view.Window$LocalWindowManager.addView(Window.java:424)
E/WindowManager( 1368): at pl.edu.uj.tcs.student.xxx.Display.setColorsViews(Display.java:181)
E/WindowManager( 1368): …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
//1
template<typename T>
void c(T in) {
cout << "Template c(" << in << ")" << endl;
}
//2
template<>
void c<>(int* in) {
cout << "Template specialization b(" << in << ")" <<endl;
}
//3
template<typename T>
void c(T* in) {
cout << "Template for pointers c(" << in << ")" <<endl;
}
//..
int i = 8;
c(&i);
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下为什么在下面的示例编译器中选择函数#3,但是当我改变函数#2和#3的顺序时,编译器会选择函数#2吗?
是否有可能获得TypeReference或TypeDefinition分配给System.Type变量的类型?
更具体地说,我试图String从以下属性定义中获取类型:
Custom(Value=typeof(String))]
public string SomeProperty {get; set;}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
static Object f(Object x) {
x = (Integer) 1234; // <- it runs OK (why?)
System.out.println(x);
return x;
}
public static void main(String[] args) {
HashMap<String, String> before = new HashMap<String, String>();
before.put("a", "b");
HashMap<String, String> after = (HashMap<String,String>) f(before); // <- it fails
System.out.println(after);
}
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
1234
Exception in thread "main" java.lang.ClassCastException:
java.lang.Integer cannot be cast to java.util.HashMap
Run Code Online (Sandbox Code Playgroud)
为什么从HashMap到Intger的运行没有错误?