java.lang.ClassCastException:android.widget.ImageButton不能转换为android.widget.TextView [英] java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView

查看:290
本文介绍了java.lang.ClassCastException:android.widget.ImageButton不能转换为android.widget.TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我运行这个应用程序,我不断收到此错误。

Whenever I run this application I keep getting this Error .

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 code:
    

Thats My XML Code:

<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 code:
    包com.example.footballtesting;

Thats My Java code: 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);

        }
}

}

}
}

推荐答案

code看起来很好...清理项目和放大器;运行

Code Look Fine ...Clean your Project & Run

希望能帮助Alt键+ P + N --->的Eclipse

Hope this could help Alt + P + N--->Eclipse

这篇关于java.lang.ClassCastException:android.widget.ImageButton不能转换为android.widget.TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆