java.lang.ClassCastException:android.widget.RelativeLayout [英] java.lang.ClassCastException: android.widget.RelativeLayout
问题描述
我开发了一个简单的列表视图应用程序,它显示的列表视图,但是当我选择一个列表视图出现以下错误了。
i developed a simple listview application and it displayed listview but when i'm selecting a single listview it occurs following error.
这是我的单列表项的XML文件。
This is my single list item xml file.
<?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文件
this is my MainActivity.java file
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文件,
This is my second screen java file,
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);
}
}
我不知道如何解决这个error.plz帮我解决这个问题。
i have no idea to how to solve this error.plz help me to fix this.
推荐答案
为您得到:
java.lang.ClassCastException: android.widget.RelativeLayout
因为这里:
String product = ((TextView) view).getText().toString();
您要投ListView中选定的行视图(RelativeLayout的)到TextView的。如果你想从选定的行布局访问的TextView再去做为:
you are trying to cast ListView selected row view(RelativeLayout) to TextView. if you want to access TextView from selected row layout then do it as:
TextView txtview = ((TextView) view.findViewById(R.id.your_textview_id));
String product = txtview.getText().toString();
这篇关于java.lang.ClassCastException:android.widget.RelativeLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!