这个程序在 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