2013-11-26 36 views
0

每当我运行这个应用程序,我总是收到这个错误。java.lang.ClassCastException:android.widget.ImageButton不能转换为android.widget.TextView

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代码: 包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); 

     } 
} 

}

}}

+1

一旦清理你的项目,然后再试一次... –

+0

这两个帖子,并得到应执行一个线程或asynctask或使用凌空 – Raghunandan

+0

干净的代码,请! – Hardik

回答

8

代码看起来很好...清理项目&运行

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

+0

谢谢。它的工作。我清理该项目,它工作:)真的非常感谢 – user3032822

+0

非常感谢。真的 – user3032822

+0

如果您添加了proguard混淆,那么非常需要清理项目。 – Shaleen

0

转到项目并选择清理,然后从列表中选择您的项目。它应该做的伎俩。

相关问题