Domanda

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

È stato utile?

Soluzione

It's due to one of those reasons:

1) Either you named a TextView within your name like R.id.imageView2, and therefore your findViewById() method is returning it and it cannot be casted to an ImageView (which is logical)

2) Either you have another R.id.imageView2 within your project which is a TextView and same goes here. Remember that ids doesn't have to be unique within your project, but things like these might happen.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top