小编Pol*_*ing的帖子

Javascript"未捕获TypeError:对象不是函数"关联性问题

代码如下:

<body>
    <a href="javascript:;" id="test">hello</a>
</body>

<script type="text/javascript">
    document.getElementById("test").addEventListener("click", function () {
      test()
    }, false)
    function test() {
      var postTypes = new Array('hello', 'there')   
      (function() { alert('hello there') })()
    }
</script>
Run Code Online (Sandbox Code Playgroud)

这将抛出:

"未捕获的TypeError:对象不是函数"

如果我将匿名函数调用/调用包装在另一组括号中,它将执行警报,但仍然给我一个错误.如果我在"var postTypes"定义之后放置一个分号,那么它将完全没问题.

我被引导相信javascript不需要分号,所以我猜测有一些奇怪的函数应用关联规则,我还没有完全理解.为什么我收到此错误?

javascript

84
推荐指数
4
解决办法
35万
查看次数

如何将ActionBar项放在主ActionBar和Bottom Bar中

在此输入图像描述

Google+应用的布局包含主操作栏和底部的操作栏项.目前我正在使用android:uiOptions="splitActionBarWhenNarrow"在底栏中放置项目.如何在顶部和底部放置物品?

android

20
推荐指数
3
解决办法
2万
查看次数

最初隐藏的MenuItem不显示当调用setVisible(true)时

我有一个基本问题,即最初隐藏的MenuItem无法切换为可见.作为一个警告,我正在使用ActionBarSherlock,但我想知道是否有人知道这是否是一个已知的Android问题,或者我在做一些可怕的事情,然后再调查这是否是ABS内部的一个问题.代码如下:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menu_xml, menu);

    mMenuItem = menu.findItem(R.id.menu_item);
    mMenuItem.setVisible(false);

    return true;
}

// Somewhere elsewhere
// MenuItem is never visible after this line is executed
mMenuItem.setVisible(true);
Run Code Online (Sandbox Code Playgroud)

我还尝试将mMenuItem赋值和可见性移动到对onPrepareOptionsMenu的调用,但显示了相同的行为.

谢谢!

android

18
推荐指数
3
解决办法
7403
查看次数

UnauthorizedAccessException:Silverlight应用程序中的无线跨线程访问(XAML/C#)

相对较新的C#,并希望尝试使用它的一些第三方Web服务API.

这是XAML代码

    <Grid x:Name="ContentGrid" Grid.Row="1">
        <StackPanel>
            <Button Content="Load Data" Click="Button_Click" />
            <TextBlock x:Name="TwitterPost" Text="Here I am"/>
        </StackPanel>
    </Grid>
Run Code Online (Sandbox Code Playgroud)

这是C#代码

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.twitter.com/1/users/show/keykoo.xml");
        request.Method = "GET";

        request.BeginGetResponse(new AsyncCallback(twitterCallback), request);
    }

    private void twitterCallback(IAsyncResult result)
    {
        HttpWebRequest request = (HttpWebRequest)result.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
        TextReader reader = new StreamReader(response.GetResponseStream());
        string strResponse = reader.ReadToEnd();

        Console.WriteLine("I am done here");
        TwitterPost.Text = "hello there";
    }
Run Code Online (Sandbox Code Playgroud)

我猜这是因为回调在一个单独的线程上执行而不是UI?在C#中处理这些类型的交互的正常流程是什么?

谢谢.

c# silverlight

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

for ... in循环,字符串数组输出索引

当我写一些像这样的javascript:

var words = ['word1', 'word2', 'word3']

for (word in words) {
    console.log(word)
}
Run Code Online (Sandbox Code Playgroud)

结果输出是相应单词的数字索引.我在Google上进行了一些搜索,但我找不到这种行为的确切原因.我猜这是完全预期的行为,但我想知道原因.

谢谢!

javascript

12
推荐指数
1
解决办法
1万
查看次数

StreamReader无法正确读取扩展字符集(UTF8)

我有一个问题,我无法读取包含外来字符的文件.据我所知,该文件采用UTF-8格式编码.

这是我的代码的核心:

using (FileStream fileStream = fileInfo.OpenRead())
{
    using (StreamReader reader = new StreamReader(fileStream, System.Text.Encoding.UTF8))
    {
        string line;

        while (!string.IsNullOrEmpty(line = reader.ReadLine()))
        {
            hashSet.Add(line);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

该文件包含单词"achôcre",但在调试期间检查时,它将其添加为"ach cre".

(这是一个亵渎文件,所以如果你说法语我会道歉.我一个人,不知道这意味着什么)

c# unicode streamreader

11
推荐指数
1
解决办法
7207
查看次数

在Swift中将UIInterfaceOrientation枚举转换为AVCaptureVideoOrientation

我在Swift中使用AVFoundation

通常,当我设置摄像机捕获会话时,我会在objective-c中执行以下操作

[[cameraView.previewLayer connection] setVideoOrientation:(AVCaptureVideoOrientation)[self interfaceOrientation]]
Run Code Online (Sandbox Code Playgroud)

在swift中,似乎我必须做这样的事情(因为可选类型)

if let connection = cameraView.previewLayer?.connection {
  connection.videoOrientation = self.interfaceOrientation as AVCaptureVideoOrientation
}
Run Code Online (Sandbox Code Playgroud)

然而,这抱怨

‘AVCaptureVideoOrientation’ is not a subtype of ‘UIInterfaceOrientation’
Run Code Online (Sandbox Code Playgroud)

在阅读了关于下行方法之后,这很有意义,但我很难找到如何实现这种方法.

我是否需要编写一个辅助方法,它基本上通过UIInterfaceOrientation的所有可用值来执行switch语句以使其工作?

ios swift

11
推荐指数
3
解决办法
5340
查看次数

取消后不调用AsyncTask.onCancelled()(true)

Android SDK v15在2.3.6设备上运行.

我有一个问题,onPostExecute()当我在通话cancel()doInBackground()呼叫时,仍在呼叫.

这是我的代码:

@Override
public String doInBackground(String... params) {
    try {
        return someMethod();
    } catch (Exception e) {
        cancel(true);
    }

    return null;
}

public String someMethod() throws Exception {
    ...
}
Run Code Online (Sandbox Code Playgroud)

我强迫someMethod()抛出异常来测试它,而不是onCancelled被调用,我总是返回onPostExecute().如果我检查isCancelled()返回的值是真的,那么我知道cancel(true)正在执行.

有任何想法吗?

android android-asynctask

8
推荐指数
2
解决办法
1万
查看次数

在Ruby中有这样的常量实例变量吗?

我的googlefu糟透了,无法找到相关信息.

基本上我想要一个只在类/模块范围内可见的实例变量,但也是不可变的.

我是Ruby的新手,如果这个问题没有多大意义,我会道歉.

ruby

6
推荐指数
1
解决办法
2095
查看次数

在.NET中是否有相当于"gettimeofday"的东西

我正在使用.NET,但我需要与基于unix的日志服务进行通信,这需要自Unix纪元时间以来的秒和微秒.秒可以轻松检索,例如:

DateTime UnixEpoch = new DateTime(1970, 1, 1);
TimeSpan time = DateTime.UtcNow() - UnixEpoch
int seconds = (int) time.TotalSeconds
Run Code Online (Sandbox Code Playgroud)

但是,我不确定如何计算微秒.我可以使用TotalMilliseconds属性并将其转换为微秒,但我认为这使得使用微秒作为精确测量的目的失败了.我已经研究过使用StopWatch类,但似乎我不能用时间播种它(例如Unix Epoch).

谢谢.

c#

5
推荐指数
2
解决办法
1921
查看次数

使用LINQ将XML解析为Dictionary

我有一个配置文件,如:

<ConfigurationFile>
    <Config name="some.configuration.setting" value="some.configuration.value"/>
    <Config name="some.configuration.setting2" value="some.configuration.value2"/>
    ...
</ConfigurationFile>
Run Code Online (Sandbox Code Playgroud)

我试图将其读取为XML并将其转换为字典.我尝试编写一些代码,但这显然是错误的,因为它不能编译.

Dictionary<string, string> configDictionary = (from configDatum in xmlDocument.Descendants("Config")
                                               select new
                                               {
                                                   Name = configDatum.Attribute("name").Value,
                                                   Value = configDatum.Attribute("value").Value,
                                               }).ToDictionary<string, string>(Something shoudl go here...?);
Run Code Online (Sandbox Code Playgroud)

如果有人能告诉我如何使这个工作,那将是非常有帮助的.当然,我总是可以阅读它

c# linq

4
推荐指数
1
解决办法
8990
查看次数