![](/img/trans.png)
[英]Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
[英]java.lang.ClassCastException: android.widget.RelativeLayout
我開發了一個簡單的listview
應用程序,它顯示了listview
但是當我選擇一個listview
我得到以下錯誤。
這是我的單個列表項XML文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/product_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:padding="10dip"
android:textColor="#ffffff"/>
</LinearLayout>
這是我的MainActivity.java文件
import java.util.List;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity implements FetchDataListener{
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String product = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("product", product);
startActivity(i);
}
});
}
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://pubbapp.comze.com/pubapp.php";
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
@Override
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
}
@Override
public void onFetchFailure(String msg) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
這是我的第二個屏幕java文件,
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SingleListItem extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_list_item_view);
TextView txtProduct = (TextView) findViewById(R.id.product_label);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
txtProduct.setText(product);
}
}
我不知道如何解決這個錯誤。 有人可以幫我解決這個問題嗎?
當你得到:
java.lang.ClassCastException: android.widget.RelativeLayout
因為這里:
String product = ((TextView) view).getText().toString();
您正在嘗試將ListView選定的行視圖(RelativeLayout)強制轉換為TextView。 如果要從選定的行布局訪問TextView,請執行以下操作:
TextView txtview = ((TextView) view.findViewById(R.id.your_textview_id));
String product = txtview.getText().toString();
更改
// selected item
String product = ((TextView) view).getText().toString();
至
TextView txtview= (TextView) view.findViewById(R.id.product_label);
String product = txtview.getText().toString();
右鍵單擊項目,然后在列表中選擇屬性。 轉到java構建路徑將Gen文件夾移動到up並將src文件夾移動到down。 清理項目並運行。如果它沒有告訴我這對我有用嗎?
如果您使用ListItem的自定義xml文件,那么您可以使用您在xml文件中編寫的textview id。使用注釋行替換這些行
// String product = ((TextView) view).getText().toString();
TextView txtview= (TextView) view.findViewById(R.id.your_textview_id);
String product = txtview.getText().toString();
如果您在適配器中使用android.R.layout.simple_list_item_1,則必須在代碼中設置android.R.id.text1資源ID。 同樣適用於這種情況
// String product = ((TextView) view).getText().toString();
TextView txtview= (TextView) view.findViewById(android.R.id.text1);
String product = txtview.getText().toString();
對於Eg。
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item------------->replace below line with this
// String product = ((TextView) view).getText().toString();
TextView txtview= (TextView) view.findViewById(R.id.your_textview_id);
String product = txtview.getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(MainActivity.this, SingleListItem.class);
// sending data to new activity
i.putExtra("product", product);
startActivity(i);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.