小编tom*_*ski的帖子

Java:尝试使用泛型

最后,我尝试了一些仿制药.我想出了这段代码:

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)

没有例外!这怎么可能?

java generics

27
推荐指数
1
解决办法
504
查看次数

活动已泄露窗口 - Android

看看这些代码:

Android上的自定义视图和窗口属性

问题

当我点击"主页按钮"时,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)

java android android-activity cordova

25
推荐指数
2
解决办法
4万
查看次数

C++模板 - 专业功能

我有以下代码:

//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吗?

c++ templates specialization

10
推荐指数
1
解决办法
426
查看次数

Mono.Cecil - 从 System.Type 获取 TypeReference

是否有可能获得TypeReferenceTypeDefinition分配给System.Type变量的类型?

更具体地说,我试图String从以下属性定义中获取类型:

Custom(Value=typeof(String))]
        public string SomeProperty {get; set;}
Run Code Online (Sandbox Code Playgroud)

.net c# mono.cecil

3
推荐指数
1
解决办法
1088
查看次数

Java:在某些情况下ClassCastException

我有以下代码:

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的运行没有错误?

java casting

0
推荐指数
1
解决办法
155
查看次数