2015-01-12 4 views
-2

클래스 캐스트 예외로 인해 응용 프로그램이 손상되었습니다. 나는 클래스 편집 텍스트에 대한 예외 캐스팅을 이해하지만이 문제를 해결할 수 없습니다. 나는이 오류에 많은 것을 시도했다. 제발 일하게 해줘. 내 자바 코드 :ClassCast 예외 받기

public static final int targetVendorID = 1155;  
    public static final int targetProductID = 22336; 
    public UsbManager manager; 
    public UsbDeviceConnection usbDeviceConnection; 
    public UsbInterface usbInterfaceFound = null; 
    public UsbEndpoint endpointOut = null; 
    public UsbEndpoint endpointIn = null; 
    public UsbDevice usbdevice,device_details; 
    public UsbEndpoint listusbendpoint; 
    public LifeTest Life_Test_child; 
    HashMap<String, UsbDevice> devicelist= null; 
    int selectedendpoint; 
    static int Coil_No; 
    private static final int VID = 1155; 
    private static final int PID = 22336; 
    private static UsbDriver Usb_Driver_class; 
    public static UsbDriver USB_Driver_Child; 
    public static boolean Communication_Failed,Frame_Ok,Total_Frame_Decoded; 
    static byte[] Communication_Byte; 

    ActionBar actionbar; 
    UsbConnectionHandler connectionHandler; 
    Button clear,signin; 
    public EditText username_edt,password; 
    String i = ""; 
    Intent i2; 
    String User_Name=null; 
    TextView title; 
    ByteBuffer buffer; 
    String Shared_username,shared_pass; 

    static byte[] sample; 
    static boolean Communication_Ok; 
    public static float []Wave_Form_Data=new float[1500]; 
    public static float []Wave_Form_Data_1=new float[1500]; 
    public static float Respsonse_Time,Drive_Voltage; 
    static int Sequence_No,Response_Time; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.loginpage); 

     actionbar = getActionBar(); 
     actionbar.hide(); 
     username_edt = (EditText)findViewById(R.id.edt_username); 
     password = (EditText)findViewById(R.id.edt_password); 

     signin = (Button)findViewById(R.id.btn_signin); 
     signin.setOnClickListener(new View.OnClickListener() 
     { 
      @SuppressWarnings("static-access") 
      @Override 
      public void onClick(View v) 
      { 
       { 
        Communication_Byte=new byte[1]; 
        User_Name=username_edt.getText().toString(); 

        if(User_Name.toLowerCase().equals("medequip") && password.getText().toString().equals("medequip")) 
        { 
         if(Check_Devices_Available()==true) 
         { 
          int Packet_Size = USB_Driver_Child.Data_In_End_Point.getMaxPacketSize(); 
          Toast.makeText(Loginpage.this,""+Packet_Size, Toast.LENGTH_LONG).show(); 
          if(!Receive.isAlive()) 
          { 
          Receive.start(); 
          } 
          else 
          Receive.stop(); 
          Communication_Ok=false; 
          for(int i=0;(i<5 && Communication_Ok!=true);i++)  
          Send_Communication_Check_Command(); 

          if(Communication_Ok) 
           Toast.makeText(Loginpage.this, "Communication Successfully Established", 1000).show(); 
          else 
           Toast.makeText(Loginpage.this, "Communication Failure", 10000).show(); 

          i2 = new Intent(Loginpage.this,TestsPage.class); 
          startActivity(i2); 
         } 

        } 
        else 
        { 
         Toast.makeText(Loginpage.this,"Invalid username and password", Toast.LENGTH_LONG).show(); 
        } 
       } 
      }  
      }); 

     clear = (Button)findViewById(R.id.btn_clear); 
     clear.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
      username_edt.setText(""); 
      password.setText(""); 
      } 
     }); 
     } 


    @SuppressLint("ShowToast") @SuppressWarnings({ "static-access" }) 
    public boolean Check_Devices_Available() 
    { 
     Usb_Driver_class = new UsbDriver(this, connectionHandler, VID, PID); 

     if(USB_Driver_Child.Device_Exception==0) 
     { 

      if(USB_Driver_Child.USB_Device_Connection==null || USB_Driver_Child.Data_Out_End_Point==null) 
      return false; 

      Toast.makeText(Loginpage.this,"Device Found", 1000).show(); 
      return true; 
     } 
     else if(USB_Driver_Child.Device_Exception==1) 
     { 
      Toast.makeText(Loginpage.this,"No Devices Attached ", Toast.LENGTH_LONG).show();  
      return false; 
     } 
     else if(USB_Driver_Child.Device_Exception==2) 
     { 
      Toast.makeText(Loginpage.this,"Device Found,But No End Points", Toast.LENGTH_LONG).show(); 
      return false; 
     } 
     else if(USB_Driver_Child.Device_Exception==3) 
     { 
      Toast.makeText(Loginpage.this,"Unable to Open Device", Toast.LENGTH_LONG).show(); 
      return false; 
     } 
     return false; //un known exception 
    } 

Thread Receive = new Thread(new Runnable() 
    { 

    @SuppressWarnings("unused") 
    @Override 
    public void run() 
    { 
     Sequence_No=0; 
     buffer = ByteBuffer.allocate(1500); 
     sample = new byte[1500]; 
     int Frame_Size; 
     UsbRequest request = new UsbRequest(); 
     int i,j; 
     byte [] datarx=new byte[1]; 
     char q; 
     while (true) 
     { 
     request.initialize(UsbDriver.USB_Device_Connection, UsbDriver.Data_In_End_Point); 
     request.queue(buffer, 1500); 
     if (UsbDriver.USB_Device_Connection.requestWait() == request) 
     { 
       sample=buffer.array(); 
       for(i=0;i<1500;i++) 
       { 
         if(sample[i]=='&') 
         { 
          Communication_Ok=true; 
          break; 
         } 
         else if(sample[i]==0x03) 
         { 
          if(sample[0]==0x02) 
          Frame_Ok=true; 
          break; 
         } 

       } 
       if(Frame_Ok==true) 
       { 
        Frame_Ok=false; 
        if(sample[1]==1) 
        Coil_No=1; 
        else 
        Coil_No=2; 
        Response_Time= (int)(((sample[2]&0x00FF)<<8) + (sample[3]&0x00FF)); 

        Total_Frame_Decoded=true; 
        sample = null; 
       } 
     } 
     } 
    } 
    }); 


private static void Send_Communication_Check_Command() 
{ 
    long i,j; 

    Communication_Byte[0]='&'; 
    UsbDriver.USB_Device_Connection.bulkTransfer(UsbDriver.Data_Out_End_Point,Communication_Byte, 1, 0); 
    for(i=0;(i<1000 && Communication_Ok!=true) ;i++)  
    for(j=0;(j<1000 && Communication_Ok!=true);j++); 
} 
//Test_Name=1; For Drive Response Test 
//Test_Name=2; For Drive Life Test 
//Test_Name=3; To Stop test 

public static void Send_Command_For_Drive_Response_Test(int Test_Name) 
{ 
    int i; 
    byte []Command_To_Transfer= new byte[5]; 
    Communication_Ok=false; 
    for(i=0;i<5;i++) 
    { 
     Send_Communication_Check_Command(); 
     if(Communication_Ok==true) 
     break; 
    } 
    if(Communication_Ok==true) 
    { 
     Command_To_Transfer[0]='*'; 
     if(Test_Name==1) 
     { 
      Command_To_Transfer[1]=Command_To_Transfer[2]=(byte) 0xA1; 
     } 
     else if(Test_Name==2) 
     { 
      Command_To_Transfer[1]=Command_To_Transfer[2]=(byte) 0xA2; 
     } 
     else if(Test_Name==3) 
     { 
      Command_To_Transfer[1]=Command_To_Transfer[2]=(byte) 0xA3; 
     } 
     Command_To_Transfer[3]=0; //For Future Reference 
     Command_To_Transfer[4]='#'; 
     UsbDriver.USB_Device_Connection.bulkTransfer(UsbDriver.Data_Out_End_Point,Command_To_Transfer, 5, 0); 
     Communication_Failed=true; 
    } 
    else 
    { 
     Communication_Failed=false; 
    } 
} 

이 mylogcat입니다 :

01-02 02:58:58.080: W/dalvikvm(9343): threadid=1: thread exiting with uncaught exception (group=0x41765700) 
01-02 02:58:58.090: E/AndroidRuntime(9343): FATAL EXCEPTION: main 
01-02 02:58:58.090: E/AndroidRuntime(9343): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo2c1testpanel/com.example.demo2c1testpanel.Loginpage}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at android.os.Looper.loop(Looper.java:137) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at java.lang.reflect.Method.invoke(Method.java:525) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at dalvik.system.NativeStart.main(Native Method) 
01-02 02:58:58.090: E/AndroidRuntime(9343): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at com.example.demo2c1testpanel.Loginpage.onCreate(Loginpage.java:71) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at android.app.Activity.performCreate(Activity.java:5133) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
01-02 02:58:58.090: E/AndroidRuntime(9343):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 

이 내 XML 파일은 다음과 같습니다

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/linearLayout3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/linearLayout2" 
     android:layout_alignRight="@+id/linearLayout2" 
     android:layout_below="@+id/linearLayout2" 
     android:layout_marginTop="30dp" > 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:shadowColor="@color/text_shadow" 
      android:shadowDx="1" 
      android:shadowDy="1" 
      android:shadowRadius="2" 
      android:text="@string/password" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="@color/light_font" /> 

     <EditText 
      android:id="@+id/edt_password" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:gravity="center" 
      android:hint="@string/password_hint" 
      android:inputType="textPassword" > 

      <requestFocus /> 
     </EditText> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="250dp" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/linearLayout1" 
     android:layout_alignRight="@+id/linearLayout1" 
     android:layout_below="@+id/linearLayout1" 
     android:layout_marginTop="111dp" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:shadowColor="@color/text_shadow" 
      android:shadowDx="1" 
      android:shadowDy="1" 
      android:shadowRadius="2" 
      android:text="@string/user_name" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="@color/light_font" /> 

     <EditText 
      android:id="@+id/edt_username" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:hint="@string/user_name_hint" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="570dp" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:gravity="center" 
      android:marqueeRepeatLimit="marquee_forever" 
      android:scrollHorizontally="true" 
      android:singleLine="true" 
      android:text="@string/Login_title" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/linearLayout3" 
     android:layout_alignRight="@+id/linearLayout3" 
     android:layout_below="@+id/linearLayout3" 
     android:layout_marginTop="60dp" > 

     <Button 
      android:id="@+id/btn_clear" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.36" 

      android:shadowRadius="2" 
      android:text="@string/clear" 
     /> 

     <Button 
      android:id="@+id/btn_signin" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.35" 
      android:text="@string/signin" /> 

    </LinearLayout> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@+id/linearLayout4" 
     android:src="@drawable/logo" /> 

</RelativeLayout> 
+1

''Loginpage'에서''71'' 행은 무엇입니까? –

+2

* 버튼을 android.widget.EditText *로 형변환 할 수 없습니다. 레이아웃에서 'Button'을 선언하고 'EditText'로 캐스팅했습니다. – Blackbelt

+0

편집 텍스트 필드에 대한 참조였습니다 @MD – lakshman

답변

0

당신이로 R.id.edt_username 또는 R.id.edt_password (또는 둘 다) 선언 된 것 같다 레이아웃 파일의 버튼 (R.layout.loginpage). EditText로 변경하십시오.

+0

당신은 어떻게 압니까? –