Html 无法正确加载
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你在哪里初始化网络视图?
请删除
[webview release];
并立即尝试。Where you are initializing the webview?
Please remove the
[webview release];
and try now.您的错误日志表明您正在为值 str 发送 nil。
请检查 str 的值。这应该可以解决问题。可能是您拼错了文件名。
评论更新
尝试替换您的文件名,而不是在运行时获取它。
如果上述方法有效,那么您应该检查数组。
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.
If the above works then you should be checking the array.
看起来
[[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.