MainActivity线程中的ClasscastException

时间:2014-03-04 15:54:17

标签: android classcastexception

找到的错误的堆栈跟踪是:   它是一个非常简单的代码,我不明白为什么它会显示,请尝试解决它。​​

 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异常。 请说明这段代码有什么问题?感谢

1 个答案:

答案 0 :(得分:0)

这是由于以下原因之一:

1)您在名称中TextView命名为R.id.imageView2,因此您的findViewById()方法会将其返回,并且无法将其转换为ImageView(是合乎逻辑的)

2)你的项目中有另一个R.id.imageView2TextView,同样在这里。请记住,id s在项目中不必是唯一的,但可能会发生类似这样的事情。