2013-05-01 3 views
2

J'ai cette erreur lorsque j'utilise ma demande:java.lang.ClassCastException: android.widget.LinearLayout ne peut pas être jeté à android.widget.TextView

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 

La méthode de la question:

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); 
} 

et le fichier 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> 

Ce fichier XML est d'afficher les périphériques bluetooth associés et appareils de nouvelles trouvées.

Pouvez-vous m'aider?

Comment changer ces lignes?

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

Mise à jour: Je me l'erreur suivante:

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) 

MISE À JOUR 2:

J'ai essayé:

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

Mais j'ai un problème avec cette ligne:

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

MISE À JOUR 3:

J'ai essayé:

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

Mais même problème ... Cette ligne est la question:

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

comment allez-vous remplir le ListView? Avez-vous votre adaptateur personnalisé? – Blackbelt

+0

Postez votre fichier listrow personnalisé listrow – Pragnani

Répondre

5

utiliser uniquement

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

Merci beaucoup !!! Ça marche !!! :) – gloops100

0

Utilisez

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

Le fichier de mise en page XML que vous avez fourni n'est pas nécessaire ici. Le fichier de ligne de vue liste est coupable. Utilisez l'ID de Textview mentionné dans row.xml.

+0

Merci pour votre réponse :) Mais j'ai toujours le problème. Je mets: TextView textview = (TextView) ((LinearLayout) voir) .findViewById (R.id.textView1); Chaîne info = textview.getText(). ToString(); L'ERREUR EST: 05-01 14: 47: 07.900: E/AndroidRuntime (16615): FATAL EXCEPTION: principal 05-01 14: 47: 07.900: E/AndroidRuntime (16615): java.lang.NullPointerException 05 -01 14: 47: 07.900: E/AndroidRuntime (16615): à com.example.cajou.DiscoverDevicesActivity.onItemClick (DiscoverDevicesActivity.java:179) 05-01 14:47:07.900: E/AndroidRuntime (16615): \t à android.widget.AdapterView.performItemClick (AdapterView.java:301) – gloops100

+0

Je comprends, mais où est ce fichier XML? – gloops100

+0

Maintenant que vous avez un pointeur Null cela peut être dû à un mauvais identifiant, partagez votre fichier row.xml pour voir la liste. Vous avez créé une mise en page pour l'affichage des données dans la vue de liste. Ce fichier est appelé fichier row.xml. Vous avez également passé cela comme un paramètre dans votre adaptateur tout en l'initialisant. –

0

getText() retourne un EditableText, pas String. Vous devez utiliser la méthode toString() de le jeter dans un type String:

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