gpt4 book ai didi

java - ClassCastException : android. widget.RelativeLayout 无法转换为 android.widget.TextView?

转载 作者:行者123 更新时间:2023-11-30 01:16:45 25 4
gpt4 key购买 nike

我正在尝试输入转换数据,但是当我在 onItemClick(...) 下编写 TextView clickData=(TextView) view 时,logcat 显示消息:

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

我不知道为什么。

这属于主要 Activity :

  public class ListViewForAllContact extends AppCompatActivity {
ListView myListView;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewforallcontact);



ContactDatabase onbOfContactDatabase=new ContactDatabase(getBaseContext());

Cursor mCursor= onbOfContactDatabase.phoneName();

String[] fromFileName=new String[]{ContactDatabase.NAME};
int[] toViewId=new int[]{R.id.textView5};
SimpleCursorAdapter simpleCursorAdapter;

simpleCursorAdapter=new SimpleCursorAdapter(this,R.layout.forreading,mCursor,fromFileName,toViewId,0);
myListView=(ListView)findViewById(R.id.listView);
myListView.setAdapter(simpleCursorAdapter);

myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

TextView clickData=(TextView) view;//becouse of this line error will come.

Toast.makeText(getBaseContext(), clickData.getText(), Toast.LENGTH_LONG).show();
}
});

}
}

这是带有 ListView 的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

这是带有 TextView 的第二个 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView5"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>





</RelativeLayout>

最佳答案

您要访问的 View 是您在第二个 xml 上显示的 RelativeLayout。如果你想访问 TextView,你必须在 RelativeLayout 上找到它。

而不是这个

TextView clickData=(TextView) view;

试试这个

TextView clickData=(TextView) view.findViewById(R.id. textView5);

我认为这可以解决您的问题

更新...

我的建议是使用适配器

public class YourAdapter extends BaseAdapter {

private ArrayList<Things> things;
private Context context;
private TextView yearTxtView;
private TextView weekTxtView;
private TextView extraTxtView;


public YourAdapter(Context context, ArrayList<Things> things) {
this.things = things;
this.context = context;
}

@Override
public int getCount() {
return this.things.size();
}

@Override
public Object getItem(int position) {
return this.things.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(this.context).inflate(R.layout.ITEM_LAYOUT, parent, false);

convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do your code here
}
});

return convertView;


}

在你拥有的代码上,只需执行此操作

private YourAdapter yourAdapter;

this.yourAdapter = new YourAdapter(getActivity(),YourArray);

myListView.setAdapter(this.yourAdapter);

关于java - ClassCastException : android. widget.RelativeLayout 无法转换为 android.widget.TextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com