Html 无法正确加载

发布于 2024-12-11 17:24:59 字数 956 浏览 0 评论 0原文

我有 10 个 html 存储在一个数组中...我想通过单击按钮来显示每个 html...但是我的应用程序崩溃了..这是代码..

int xpos=10,ypos=10;

for (int i=0; i<[array count]; i++) {

    UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [but setTag:i];
    but.backgroundColor=[UIColor redColor];
    but.frame=CGRectMake(xpos, ypos, 50, 50);
    xpos+=90;

    [self.view addSubview:but];
    [but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
}


- (void)buttonClicked:(UIButton *)sender {

    NSString *str=[array objectAtIndex:sender.tag];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:str ofType:@"html"]isDirectory:NO]]];
    [webview release];
}

如何克服这个问题?这是崩溃报告

24/10/11 4:56:08 PM 加载 HTML[4655] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSURL initFileURLWithPath:isDirectory: ]: nil 字符串参数'

I have 10 html which are stored in an array...I want to display each html by means of button click...But my app got crashed.. Here is the code..

int xpos=10,ypos=10;

for (int i=0; i<[array count]; i++) {

    UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [but setTag:i];
    but.backgroundColor=[UIColor redColor];
    but.frame=CGRectMake(xpos, ypos, 50, 50);
    xpos+=90;

    [self.view addSubview:but];
    [but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
}


- (void)buttonClicked:(UIButton *)sender {

    NSString *str=[array objectAtIndex:sender.tag];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:str ofType:@"html"]isDirectory:NO]]];
    [webview release];
}

How to overcome this problem? here is the crash report

24/10/11 4:56:08 PM Loading HTML[4655] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:isDirectory:]: nil string parameter'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送你一个梦 2024-12-18 17:24:59

你在哪里初始化网络视图?

请删除 [webview release]; 并立即尝试。

Where you are initializing the webview?

Please remove the [webview release]; and try now.

只是偏爱你 2024-12-18 17:24:59
24/10/11 4:56:08 PM Loading HTML[4655] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:isDirectory:]: nil string parameter'

您的错误日志表明您正在为值 str 发送 nil。

请检查 str 的值。这应该可以解决问题。可能是您拼错了文件名。


评论更新

尝试替换您的文件名,而不是在运行时获取它。

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yourHtmlFileNameGoesHere" ofType:@"html"]isDirectory:NO]]];

如果上述方法有效,那么您应该检查数组。

24/10/11 4:56:08 PM Loading HTML[4655] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:isDirectory:]: nil string parameter'

Your error log states that you are sending nil for the value str.

Please check the value of str. That should fix the problem. May be you have misspelled the name of the file.


Update for comment

Try replacing your file name there instead of getting it at run time.

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yourHtmlFileNameGoesHere" ofType:@"html"]isDirectory:NO]]];

If the above works then you should be checking the array.

¢好甜 2024-12-18 17:24:59

看起来 [[NSBundle mainBundle] pathForResource:str ofType:@"html"] 结果为 nil - 这意味着现在可以在主包中找到该文件。确保具有该名称和扩展名的文件存在。请记住,文件名区分大小写。

Looks like [[NSBundle mainBundle] pathForResource:str ofType:@"html"] results in nil - which means that the file could now be found in your main bundle. Make sure that file with that name and extension exists. Remember that file names are case sensitive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文