通过findViewById()方法从layout中获取view并进行相应的转换时提示:"Cannot cast from View to AutoCompleteTextView"的解决办法!
2012-04-12 15:02
1231 查看
代码:
第14行提示错误:“Cannot cast from View to AutoCompleteTextView”,从而也导致第16行发生错误。
细查原因,未发现有拼写错误。
上网搜索,有说可能是未导入相应的包,可明明已经按过组合键“ctrl+shift+O”导入了可能的包了。
也有说根本不能这么转换,扯,搞android开发的都是在用这个方法啊!
再细看,恍然,原来定义的类名跟调用的函数名一模一样,冲突了,怪不得呢!!!
经修搞改类名后,错误排除!!!!!
ok!!!!!!
需要注意的是
(1)改完类名后可能需要相应的改动“.java”文件的名称
(2)改动完成后,再按一次组合键“ctrl+shift+O”,确保导入了所需的包。因为之前的名称冲突可能导致你并未导入正确的包。
(3)记得保存一下修改结果!
谨记!
package zyz.example.autocompletetextview; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; public class AutoCompleteTextView extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.autocomplete_country); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES); auttextView.setAdapter(adapter); }
第14行提示错误:“Cannot cast from View to AutoCompleteTextView”,从而也导致第16行发生错误。
细查原因,未发现有拼写错误。
上网搜索,有说可能是未导入相应的包,可明明已经按过组合键“ctrl+shift+O”导入了可能的包了。
也有说根本不能这么转换,扯,搞android开发的都是在用这个方法啊!
再细看,恍然,原来定义的类名跟调用的函数名一模一样,冲突了,怪不得呢!!!
经修搞改类名后,错误排除!!!!!
ok!!!!!!
需要注意的是
(1)改完类名后可能需要相应的改动“.java”文件的名称
(2)改动完成后,再按一次组合键“ctrl+shift+O”,确保导入了所需的包。因为之前的名称冲突可能导致你并未导入正确的包。
(3)记得保存一下修改结果!
谨记!
相关文章推荐
- 通过findViewById()方法从layout中获取view并进行相应的转换时提示:"Cannot cast from View to AutoCompleteTextView"的解决办法!(转+自己错误)
- Cannot cast from View to ProgressBar的解决办法
- Web开发敏捷之道-应用Rails进行敏捷Web开发-第三版 92页 错误提示 syntax error, unexpected ')' ..._cart" , :id => product } do ).to_s) 解决办法
- Android 获取listview中的文本 报错:android.widget.LinearLayout cannot be cast to android.widget.TextView
- Cannot cast from View to ProgressBar的解决办法
- Cannot cast from View to ProgressBar解决办法
- Cannot cast from View to ProgressBar的解决办法
- 类型转换 -- 使用Unicode下的cannot convert parameter 1 from 'CString' to 'const char *' 解决办法
- Intellij IDEA ,按Ctrl+左键,不能进入类或方法的【解决办法】,提示Cannot find declaration to go to
- java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView解决办法
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决办法
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决的方法
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决办法
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决办法
- "无法启动Web服务器。Web服务器无法找到请求的资源" 或 "unable to start debugging on the web server.the web server could not find the request"解决方法
- Unable to read TLD "META-INF/c.tld" from JAR file 解决方法
- 由"Type mismatch: cannot convert from View to ProgressBar"联想到的类型转换错误的根源
- 解决 android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
- Cannot cast from ActionForm to XXForm 解决办法
- 自定义 view oncreate -> onmesure -> onlayout ->在 成员方法findviewbyid 为 null , 在 onmesure则友值