java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView

时间:2013-05-01 12:33:04

标签: android textview android-linearlayout

使用我的应用程序时出现此错误:

05-01 14:18:41.000: E/AndroidRuntime(26607): FATAL EXCEPTION: main
05-01 14:18:41.000: E/AndroidRuntime(26607): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

带问题的方法:

public void onItemClick(AdapterView<?> adapterView, View view, int postion,
        long index) {

    // Get MAC adress matching the last 17 characters of the TextView
   String info = ((TextView) view).getText().toString();    //  HERE IS THE ISSUE
   String address = info.substring(info.length() - 17);

    Intent intent = new Intent();
    intent.putExtra(EXTRA_DEVICE_ADDRESS, address);

    setResult(Activity.RESULT_OK, intent);


    BluetoothDevice device = (BluetoothDevice) view.getTag();

    showEquipementActivity(device);
}

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

     <ImageView
        android:id="@+id/ImageView_tactea"
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:src="@drawable/tactea"
        android:layout_gravity="center"
        android:visibility="visible" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/label_list_of_devices"
            android:textSize="17dip"
            android:textStyle="bold" />

        <Button
            android:id="@+id/buttonScan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/label_scan" />
    </LinearLayout>

    <ListView
        android:id="@+id/listViewDevices"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

此XML文件用于显示配对的蓝牙设备和找到的新闻设备。

你能帮助我吗?

如何更改这些行?

   String info = ((TextView) view).getText().toString();    //  HERE IS THE ISSUE
   String address = info.substring(info.length() - 17);

更新:我收到以下错误:

05-01 14:39:33.150: E/AndroidRuntime(7160): FATAL EXCEPTION: main 
05-01 14:39:33.150: E/AndroidRuntime(7160): java.lang.NullPointerException 
05-01 14:39:33.150: E/AndroidRuntime(7160): at com.example.cajou.DiscoverDevicesActivity.onItemClick(DiscoverDevicesActivity.ja‌​va:179) 
05-01 14:39:33.150: E/AndroidRuntime(7160): at android.widget.AdapterView.performItemClick(AdapterView.java:301) 
05-01 14:39:33.150: E/AndroidRuntime(7160): at android.widget.AbsListView.performItemClick(AbsListView.java:1276)

更新2:

我试过了:

TextView textview=(TextView) ((LinearLayout)view).findViewById(R.id.textView1);
String info = textview.getText().toString(); 

但我对这一行有一个问题:

String info = textview.getText().toString(); 

更新3:

我试过了:

LinearLayout ll = (LinearLayout) view;
TextView tv = (TextView) ll.findViewById(R.id.textView1);
final String info = tv.getText().toString();

但同样的问题...... 这一行就是问题所在:

final String info = tv.getText().toString();

3 个答案:

答案 0 :(得分:6)

仅使用

TextView textview = (TextView)findViewById(R.id.textView1);
String info = textview.getText().toString();

答案 1 :(得分:0)

使用

 TextView textview=((LinearLayout)view).findViewById(R.id.textView1);
 String info = textView.getText().toString(); 

此处不需要您提供的xml布局文件。列表视图行文件是罪魁祸首。所以使用row.xml中提到的Textview的Id。

答案 2 :(得分:0)

getText()会返回EditableText,而非String。您必须使用toString()方法将其转换为String类型:

String text = (TextView)findViewById(R.id.tv).getText().toString();