0

这个程序在 relativelayout 中创建一个 listview,Listview 很好,但是当我点击一个项目时它崩溃(应该是 toast)在调试模式下,代码在粗体行崩溃(即 RelativeLayout relativeLayoutChild = (RelativeLayout) 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
4

3 回答 3

3

我认为您应该以这种方式从布局(您选择的行)中获取您的子小部件:

TextView tvId = (TextView) container.findViewById(R.id.tv_id);

方法中的第二个参数onItemClick()代表 rowView (在您的情况下为 lv_layout.xml)。每行都有自己的lv_layout.xml布局。

因此,您可以通过此方法直接访问每一行的每个子小部件(它会在单击位置自动返回整个 rowView)。

于 2013-08-09T13:53:20.857 回答
1

更改以下行

 **RelativeLayout relativeLayoutChild = (RelativeLayout ) relativeLayoutParent.getChildAt(1);**

 **TextView tv_coordinate_details= (TextView ) relativeLayoutParent.getChildAt(1);**
   TextView tvId = (TextView) relativeLayoutParent.getChildAt(0);
于 2013-08-09T13:56:30.353 回答
0

View container是你的 lv_layout.xml。relativeLayoutChild是不必要的,孩子relativeLayoutParent将是你的 TextViews。您正在尝试转换tv_coordinate_details为 RelativeLayout。

于 2013-08-09T13:56:44.647 回答