Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class cast exception

Tags:

android

This is my code:

package com.example.userpage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class UserPage extends Activity {

     String tv,tv1;
    EditText name,pass;
     TextView x,y;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         Button button = (Button) findViewById(R.id.widget44);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                name.setText(" ");
                pass.setText(" ");
            }
        });

        x = (TextView) findViewById(R.id.widget46);
        y = (TextView) findViewById(R.id.widget47);
         name = (EditText)findViewById(R.id.widget41);
         pass = (EditText)findViewById(R.id.widget42);
        Button button1 = (Button) findViewById(R.id.widget45);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

              tv= name.getText().toString();
               tv1 = pass.getText().toString();
               x.setText(tv);
               y.setText(tv1);
           }
        });
    }
}

And this is my log cat:

02-16 12:28:32.648: ERROR/AndroidRuntime(1039): FATAL EXCEPTION: main
02-16 12:28:32.648: ERROR/AndroidRuntime(1039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.userpage/com.example.userpage.UserPage}: java.lang.ClassCastException: android.widget.TextView
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at android.os.Looper.loop(Looper.java:123)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at java.lang.reflect.Method.invokeNative(Native Method)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at java.lang.reflect.Method.invoke(Method.java:521)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at dalvik.system.NativeStart.main(Native Method)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039): Caused by: java.lang.ClassCastException: android.widget.TextView
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at com.example.userpage.UserPage.onCreate(UserPage.java:34)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):     ... 11 more
02-16 12:28:32.698: WARN/ActivityManager(67):   Force finishing activity com.example.userpage/.UserPage
02-16 12:28:32.967: DEBUG/dalvikvm(292): GC_EXPLICIT freed 46 objects / 2240 bytes in 6840ms
02-16 12:28:33.247: WARN/ActivityManager(67): Activity pause timeout for HistoryRecord{43ee7b70 com.example.userpage/.UserPage}
02-16 12:28:36.947: INFO/Process(1039): Sending signal. PID: 1039 SIG: 9
02-16 12:28:37.017: INFO/ActivityManager(67): Process com.example.userpage (pid 1039) has died.
02-16 12:28:37.128: WARN/InputManagerService(67): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43e872f8
02-16 12:28:42.087: DEBUG/dalvikvm(176): GC_EXPLICIT freed 156 objects / 11488 bytes in 145ms
02-16 12:28:45.391: WARN/ActivityManager(67): Activity destroy timeout for HistoryRecord{43ee7b70 com.example.userpage/.UserPage}
02-16 12:28:47.177: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
like image 572
Piyush Avatar asked Feb 16 '11 07:02

Piyush


People also ask

How do you find class cast exception?

A class cast exception is thrown by Java when you try to cast an Object of one data type to another. Java allows us to cast variables of one type to another as long as the casting happens between compatible data types.

How do I remove a class cast exception?

// type cast an parent type to its child type. In order to deal with ClassCastException be careful that when you're trying to typecast an object of a class into another class ensure that the new type belongs to one of its parent classes or do not try to typecast a parent object to its child type.

How do you overcome class cast exception in Java?

To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.

What is class cast?

The cast() method of java. lang. Class class is used to cast the specified object to the object of this class. The method returns the object after casting in the form of an object.


3 Answers

If this

Caused by: java.lang.ClassCastException: android.widget.TextView 02-16 12:28:32.648: ERROR/AndroidRuntime(1039): at com.example.userpage.UserPage.onCreate(UserPage.java:34)

...is in your code, then it seems as if you're trying to cast some (non-textview) object to a TextView. Try to set a debug point in that onCreate and check what is going on.

Update
Are you sure R.id.widget46 and R.id.widget47 are a TextView or did you accidentally create an EditText, given that widget41 and widget42 are EditTexts. Or are the last two TextViews and you try to cast them to EditTexts. Check that.

like image 149
Juri Avatar answered Sep 26 '22 15:09

Juri


Had the same problem. My solution: just clean project and rebuild it. For some reason R.class didn't update properly.

like image 34
higsBozone Avatar answered Sep 25 '22 15:09

higsBozone


I had the same problem. The solution worked for me was:

I have deleted the textview and recreated it by dragging a new textview from graphical layout and named it with different ID. Also the other change was its order inside the xml. It had been on top, after recreating it was on bottom of the xml page.

After that I did not get any class cast exception.

like image 38
mtekeli Avatar answered Sep 25 '22 15:09

mtekeli