这个程序在relativelayout中创建一个listview, Listview即将推出,但当我点击一个项目时它会崩溃(预计会有吐司) 在调试模式下,代码在粗体线上崩溃(即RelativeLayout relativeLayoutChild =(RelativeLayout)relativeLayoutParent.getChildAt(1);)
public class MainActivity extends Activity {
ListView mListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// URL to the JSON data
String strUrl = "http://10.0.2.2/android_conect/conn.php";
// Creating a new non-ui thread task to download json data
DownloadTask downloadTask = new DownloadTask();
// Starting the download process
downloadTask.execute(strUrl);
// Getting a reference to ListView of activity_main
mListView = (ListView) findViewById(R.id.lv_coordinates);
// Item Click Listener for the listview
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View container, int position, long id) {
// Getting the Container Layout of the ListView
RelativeLayout relativeLayoutParent = (RelativeLayout) container;
// Getting the inner Linear Layout
**RelativeLayout relativeLayoutChild = (RelativeLayout ) relativeLayoutParent.getChildAt(1);**
// Getting the Country TextView
TextView tvId = (TextView) relativeLayoutChild.getChildAt(0);
Toast.makeText(getBaseContext(), tvId.getText().toString(), Toast.LENGTH_SHORT).show();
}
};
// Setting the item click listener for the listview
mListView.setOnItemClickListener(itemClickListener);
}
布局文件是: activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/lv_coordinates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" />
</RelativeLayout>
lv_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_coordinate_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_id" />
</RelativeLayout>
错误信息是:
FATAL EXCEPTION: main
java.lang.ClassCastException:
android.widget.TextView cannot be cast to android.widget.RelativeLayout
at com.example.mylistview.MainActivity$1.onItemClick(MainActivity.java:58)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
at android.widget.AbsListView$1.run(AbsListView.java:3423)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Sending signal. PID: 2591 SIG: 9
答案 0 :(得分:3)
我认为你应该以这种方式从布局(你选择的行)中获取你的子窗口小部件:
TextView tvId = (TextView) container.findViewById(R.id.tv_id);
onItemClick()
方法中的第二个参数表示rowView(在您的情况下为 lv_layout.xml )。每行都有自己的 lv_layout.xml 布局。
因此,您可以通过此方法直接访问每行的每个子窗口小部件(它会在单击的位置自动返回整个rowView)。
答案 1 :(得分:1)
更改以下行
**RelativeLayout relativeLayoutChild = (RelativeLayout ) relativeLayoutParent.getChildAt(1);**
到
**TextView tv_coordinate_details= (TextView ) relativeLayoutParent.getChildAt(1);**
TextView tvId = (TextView) relativeLayoutParent.getChildAt(0);
答案 2 :(得分:0)
View container
是你的lv_layout.xml。 relativeLayoutChild
是不必要的,relativeLayoutParent
的子项将是您的TextView。您正在尝试将tv_coordinate_details
投射到RelativeLayout。