MainActivity线程中的ClasscastException(ClasscastException in MainActivity thread)
找到的错误的堆栈跟踪是:它是一个非常简单的代码,我不知道它为什么显示,请尝试修复它。
03-04 21:09:11.837: E/AndroidRuntime(902): FATAL EXCEPTION: main 03-04 21:09:11.837: E/AndroidRuntime(902): java.lang.RuntimeException: Unable to start activity ComponentInfo{candyhive.bitcream.candy/candyhive.bitcream.candy.Loadscreen}: java.lang.ClassCastException: android.widget.TextView 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.os.Handler.dispatchMessage(Handler.java:99) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.os.Looper.loop(Looper.java:123) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread.main(ActivityThread.java:3683) 03-04 21:09:11.837: E/AndroidRuntime(902): at java.lang.reflect.Method.invokeNative(Native Method) 03-04 21:09:11.837: E/AndroidRuntime(902): at java.lang.reflect.Method.invoke(Method.java:507) 03-04 21:09:11.837: E/AndroidRuntime(902): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-04 21:09:11.837: E/AndroidRuntime(902): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-04 21:09:11.837: E/AndroidRuntime(902): at dalvik.system.NativeStart.main(Native Method) 03-04 21:09:11.837: E/AndroidRuntime(902): Caused by: java.lang.ClassCastException: android.widget.TextView 03-04 21:09:11.837: E/AndroidRuntime(902): at candyhive.bitcream.candy.Loadscreen.onCreate(Loadscreen.java:87) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 03-04 21:09:11.837: E/AndroidRuntime(902): ... 11 more
码:
public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); setContentView(R.layout.activity_loadscreen); e1= (EditText) findViewById(R.id.editText1); r1=new Random(); iv1 = (ImageView) findViewById(R.id.imageView1); iv1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { s= e1.getText().toString(); if(s.matches("")){ int time=2000; Toast.makeText(Loadscreen.this, "Please specify the level", time).show(); } else{ int str= Integer.valueOf(s); if(str>0 && str<11){ calculate(str); r2=new Random(); int nxt = r2.nextInt(2); if(nxt==0){ Intent i1= new Intent(Loadscreen.this,Game.class); i1.putExtra("music", music); i1.putExtra("sound", sound); i1.putExtra("max",max); i1.putExtra("candymax", candymax); startActivity(i1); Loadscreen.this.finish(); } else{ Intent i1= new Intent(Loadscreen.this,Game01.class); i1.putExtra("music", music); i1.putExtra("sound", sound); i1.putExtra("max",max); i1.putExtra("candymax", candymax); startActivity(i1); Loadscreen.this.finish(); } } else{ int time=2000; Toast.makeText(Loadscreen.this, "Enter a valid level", time).show(); } } } }); iv2= (ImageView) findViewById(R.id.imageView2); iv2.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { AlertDialog.Builder build= new AlertDialog.Builder(Loadscreen.this); build.setMessage("Are you sure you want to quit the game?"); build.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { Loadscreen.this.finish(); } }); build.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { arg0.cancel(); } }); build.setCancelable(false); build.show(); } }); }
它显示行iv2中的错误= findViewbyId(R.id.imageview2)表示Classcast异常。 请说明这段代码有什么问题? 谢谢
The stacktrace of the error found is: Its a very simple code and I am not getting why it is showing so, Please try to fix it.
03-04 21:09:11.837: E/AndroidRuntime(902): FATAL EXCEPTION: main 03-04 21:09:11.837: E/AndroidRuntime(902): java.lang.RuntimeException: Unable to start activity ComponentInfo{candyhive.bitcream.candy/candyhive.bitcream.candy.Loadscreen}: java.lang.ClassCastException: android.widget.TextView 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.os.Handler.dispatchMessage(Handler.java:99) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.os.Looper.loop(Looper.java:123) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread.main(ActivityThread.java:3683) 03-04 21:09:11.837: E/AndroidRuntime(902): at java.lang.reflect.Method.invokeNative(Native Method) 03-04 21:09:11.837: E/AndroidRuntime(902): at java.lang.reflect.Method.invoke(Method.java:507) 03-04 21:09:11.837: E/AndroidRuntime(902): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-04 21:09:11.837: E/AndroidRuntime(902): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-04 21:09:11.837: E/AndroidRuntime(902): at dalvik.system.NativeStart.main(Native Method) 03-04 21:09:11.837: E/AndroidRuntime(902): Caused by: java.lang.ClassCastException: android.widget.TextView 03-04 21:09:11.837: E/AndroidRuntime(902): at candyhive.bitcream.candy.Loadscreen.onCreate(Loadscreen.java:87) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-04 21:09:11.837: E/AndroidRuntime(902): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 03-04 21:09:11.837: E/AndroidRuntime(902): ... 11 more
Code:
public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); setContentView(R.layout.activity_loadscreen); e1= (EditText) findViewById(R.id.editText1); r1=new Random(); iv1 = (ImageView) findViewById(R.id.imageView1); iv1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { s= e1.getText().toString(); if(s.matches("")){ int time=2000; Toast.makeText(Loadscreen.this, "Please specify the level", time).show(); } else{ int str= Integer.valueOf(s); if(str>0 && str<11){ calculate(str); r2=new Random(); int nxt = r2.nextInt(2); if(nxt==0){ Intent i1= new Intent(Loadscreen.this,Game.class); i1.putExtra("music", music); i1.putExtra("sound", sound); i1.putExtra("max",max); i1.putExtra("candymax", candymax); startActivity(i1); Loadscreen.this.finish(); } else{ Intent i1= new Intent(Loadscreen.this,Game01.class); i1.putExtra("music", music); i1.putExtra("sound", sound); i1.putExtra("max",max); i1.putExtra("candymax", candymax); startActivity(i1); Loadscreen.this.finish(); } } else{ int time=2000; Toast.makeText(Loadscreen.this, "Enter a valid level", time).show(); } } } }); iv2= (ImageView) findViewById(R.id.imageView2); iv2.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { AlertDialog.Builder build= new AlertDialog.Builder(Loadscreen.this); build.setMessage("Are you sure you want to quit the game?"); build.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { Loadscreen.this.finish(); } }); build.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { arg0.cancel(); } }); build.setCancelable(false); build.show(); } }); }
It shows an error in the line iv2= findViewbyId(R.id.imageview2) says Classcast Exception. Please suggest whats wrong with this code? Thanks
原文:https://stackoverflow.com/questions/22176694
更新时间:2022-03-21 21:03
最满意答案
没有; 字符串的长度为0(空字符串,表示为
""
)或更高。 并且一个空字符串与null 不一样(在Java中它不被称为空字符串 )。为了更好地理解null在Java中的含义,您可以查看以下问题:
No; the length of a string is 0 (empty string, represented as
""
) or higher. And an empty string is not the same as null either (in Java it is not called a null string).To better understand what null means in Java, you could check out these questions:
相关问答
更多-
Java Unicode字符串长度(Java Unicode String length)[2019-11-21]
找到解决您的问题。 基于这个SO答案,我做了一个使用正则表达式字符类来搜索可能有可选修饰符的字母的程序。 它将您的字符串分成单个(如有必要)组合并将其放入列表中: import java.util.*; import java.lang.*; import java.util.regex.*; class Main { public static void main (String[] args) { String s="குமார்"; Lists = s.substring(0, Math.min(s.length(), 10)); 像这样使用Math.min避免了字符串已经短于10的异常。 笔记: 以上是真正的修剪。 如果你真的想用最后三个(!)字符替换,如果它被截断,那么使用Apache Commons StringUtils.abbreviate 。 如果您的String包含BMP之外的Unicode代码点,则这可能会出错1 例如Emojis。 有关所有Unicode代码点正确运行的解决方案 ,请参阅@ sibnick的解决方案 。 1 ...重复字符串到一定长度(Repeat string to certain length)[2021-11-22]
def repeat_to_length(string_to_expand, length): return (string_to_expand * ((length/len(string_to_expand))+1))[:length] def repeat_to_length(string_to_expand, length): return (string_to_expand * ((length/len(string_to_expand))+1))[:length] For pyth ...获取字符串的长度(Get the length of a String)[2023-08-12]
使用count字符方法: let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪" println("unusualMenagerie has \(count(unusualMenagerie)) characters") // prints "unusualMenagerie has 40 characters" 来自“Apple Swift指南” (注意,对于Swi ...字符串长度的问题(Issues with String length)[2023-09-02]
问题出在这一行: movieArrayList.get(i).getTitle().substring(0, stringSize) 因为i的标题可能少于来自SearchField.getText() 它也可以来自这条线: str.substring(0, 2) 与之前相同的原因, SearchField.getText().length可能小于2 。 您可以像Tom使用startWith方法建议的那样解决它。 movieArrayList.get(i).getTitle().toLowerCase() ...用属性包装它来处理截断: public SomeClass { private const int MaxLength = 20; // for example private String _theString; public String CappedString { get { return _theString; } set { _theString = value != null && value.Length > ...字符串的长度(The length of a string)[2023-07-05]
10^4的值是14,因为^是XOR运算符。 你不能简单地写1E4是10 4因为这是一个浮点常数,数组边界必须是整数常量。 你可以把它( char string[(int)1E4]; ),但为什么不清楚简洁地写出你的意思: char string[10000]; 就像你在第二个例子中所做的那样。 你应该写一个论点: if (scanf("%9999s", string) != 1) …handle input error or EOF… 这可以保护您免受缓冲区溢出。 The value of 10^ ...负字符串长度(Negative String Length)[2023-01-28]
没有; 字符串的长度为0(空字符串,表示为"" )或更高。 并且一个空字符串与null 不一样(在Java中它不被称为空字符串 )。 为了更好地理解null在Java中的含义,您可以查看以下问题: 对象是否为null? Java是否为null关键字? No; the length of a string is 0 (empty string, represented as "") or higher. And an empty string is not the same as null either (i ...如何调整字符串长度(How to adjusting string length)[2022-12-19]
str_pad不会添加等于其第二个参数的空格,它会将字符串str_pad到第二个参数中给出的长度。 即使在文档中,这也不是很清楚。 试试这个(并取出你计算$remaining ): $str = str_pad($string, $length); str_pad doesn't add spaces equal to its second parameter, it pads the string TO the length given in the second parameter. This isn ...这对我有用: x = tf.constant("Hello everyone") # Launch the default graph. with tf.Session() as sess: print(tf.size(tf.string_split([x],"")).eval()) This works for me: x = tf.constant("Hello everyone") # Launch the default graph. with tf.Session() as sess ...相关文章
更多- 线程报错 thread
- 引入thread后socket接受不了报文了
- 关于Thread类中的start()方法和run()方法
- 一步一步掌握java的线程机制(二)----Thread的生命周期
- 过滤器引起的classcastException
- Eclipse下第一个Hadoop程序出现错误ClassCastException
- org.apache.zookeeper.server.NIOServerCnxn - Thread Thread[main,5,main] died java.lang.NoSuchFieldError: AuthFailed
- 在main方法中开启线程与@Test中开启线程
- 关于线程
- 为什么执行JAVA程序时,会出现Exception in thread"main" java.lan
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)