java.lang.ClassCastException:android.widget.ImageButton无法强制转换为android.widget.TextView

时间:2013-11-26 11:13:35

标签: java android

每当我运行此应用程序时,我都会遇到此错误。

11-26 17:38:52.973: E/AndroidRuntime(2220): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.footballtesting/com.example.footballtesting.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView

这就是我的XML代码:     

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="94dp" >

    <ImageButton
        android:id="@+id/imgbtn_lsave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@null"
        android:src="@drawable/btn1" />

    <EditText
        android:id="@+id/txt_teamID"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imgbtn_lsave"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="24dp"
        android:background="#ffffff"
        android:ems="10"
        android:inputType="textPersonName"
        android:paddingTop="10dp" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/txt_teamID"
        android:layout_alignBottom="@+id/txt_teamID"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="16dp"
        android:text="チームID"
        android:textColor="#ffffff" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/Relativelayoutrequest"
    android:layout_width="wrap_content"
    android:layout_height="120dp"
    android:layout_alignLeft="@+id/relativeLayout1"
    android:layout_below="@+id/relativeLayout1" >

    <TextView
        android:id="@+id/layout2textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="21dp"
        android:text="チームから参加要請があります"
        android:textColor="#ffffff"
        android:textSize="18dp" />

    <ImageButton
        android:id="@+id/imgbtn_layout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout2textView1"
        android:layout_centerHorizontal="true"
        android:background="@null"
        android:src="@drawable/btn2" />
</RelativeLayout>

<TextView
    android:id="@+id/textView1"
    android:layout_width="288dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/relativeLayout1"
    android:layout_alignTop="@+id/relativeLayout1"
    android:text="   参加したいチームのIDを入カしてください"
    android:textColor="#ffffff" />

多数民众赞成我的Java代码:     package com.example.footballtesting;

import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.footballtesting.CustomHttpClient;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import android.widget.TextView;

public class MainActivity extends Activity {

TextView txt_view1;
TextView txt_view3;
TextView txt_view2;
EditText txt_teamID;
ImageButton btn_save;
ImageButton btn_show;
RelativeLayout Relativelayoutrequest;



@Override
protected void onCreate(Bundle savedInstanceState) {





    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    txt_view1=(TextView)findViewById(R.id.textView1);
    txt_view2=(TextView)findViewById(R.id.textView2);
    txt_view3=(TextView)findViewById(R.id.layout2textView1);
    txt_teamID=(EditText)findViewById(R.id.txt_teamID);
    btn_save=(ImageButton)findViewById(R.id.imgbtn_lsave);
    btn_show=(ImageButton)findViewById(R.id.imgbtn_layout2);
    Relativelayoutrequest=(RelativeLayout)findViewById(R.id.Relativelayoutrequest);
    checkOnCreate();

}

public void checkOnCreate()
{

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectDiskReads().detectDiskWrites().detectNetwork() 
    .penaltyLog().build());
    Relativelayoutrequest=(RelativeLayout)findViewById(R.id.Relativelayoutrequest);
    String response=null;
    String st=null;
    String str=null;
    String memberID="000000011";

    int status=1;
    //To pass to php
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("userID",memberID

    ArrayList<NameValuePair> pp = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("userID","409"));
    try {

          st=CustomHttpClient.executeHttpGet     ("http://192.168.200.14/football365/DBConnection.php");
          str= CustomHttpClient.executeHttpPost(
                     "http://192.168.200.14/football365/responseJson.php",
                      pp);
         response = CustomHttpClient.executeHttpPost(
                 "http://10.0.2.2/football365/checkRequestFromTeam.php",
                  postParameters);
        }
   catch (Exception e) {

        e.printStackTrace();
    }



    String result = response.toString();
    try{

        JSONObject jArray = new JSONObject (result);
        for(int i=0;i<jArray.length();i++){

              status=jArray.getInt("requestStatus");
        }
}
catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
}


    status=1;
    if(status==0){

        if(Relativelayoutrequest.getVisibility()==View.VISIBLE){
            Relativelayoutrequest.setVisibility(View.GONE);
        }
      else{
        if(Relativelayoutrequest.getVisibility()==View.INVISIBLE){
            Relativelayoutrequest.setVisibility(View.VISIBLE);

        }
}

}

} }

2 个答案:

答案 0 :(得分:8)

Code Look Fine ...清理您的项目&amp;运行

希望这可以帮助Alt + P + N ---&gt; Eclipse

答案 1 :(得分:0)

转到项目并选择“清理”,然后从列表中选择项目。应该这样做。