每當我運行這個應用程序,我總是收到這個錯誤。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);
}
}
}
}}
一旦清理你的項目,然後再試一次... –
這兩個帖子,並得到應執行一個線程或asynctask或使用凌空 – Raghunandan
乾淨的代碼,請! – Hardik