简体   繁体   中英

android.widget.LinearLayout cannot be cast to android.widget.TextView (android-studio)

Im Trying To paste data(from an Intent) on a customized ListView ,and then I get this error,but when I did it with an uncustomized ListView it worked properly. I understood that the problem occurs when I change :

ListAdapter theAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,timegone);

to:

 ListAdapter theAdapter=new ArrayAdapter<String>(this,R.layout.row_layout,timegone);

But I dont get why it doesnt work!

Here below I added the row_layout.xml file which Causes the app crash.

Thats how it works :

public class Shifts extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  setContentView(R.layout.shifts);
    Intent MyIntent=getIntent();
    String timepass=MyIntent.getExtras().getString("time");

    ListView thelist=(ListView)findViewById(R.id.MyListView);
   String[] timegone=timepass.split(":");
    ListAdapter theAdapter=new ArrayAdapter<String>(this,**android.R.layout.simple_list_item_1**,timegone);

    thelist.setAdapter(theAdapter);


    }

}

And thats how it doesnt work :

public class Shifts extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  setContentView(R.layout.shifts);
    Intent MyIntent=getIntent();
    String timepass=MyIntent.getExtras().getString("time");

    ListView thelist=(ListView)findViewById(R.id.MyListView);
   String[] timegone=timepass.split(":");
    ListAdapter theAdapter=new ArrayAdapter<String>(this,**R.layout.row_layout**,timegone);

    thelist.setAdapter(theAdapter);


    }
}

This is the row_layout.xml that im using in the adapter

<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainScreen"
android:id="@+id/ly"

>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv1"
    android:textSize="30sp"
    android:textStyle="bold"
    android:padding="15dp"/>

     </LinearLayout>

What should I do in order to fix this ? Thank you!

You need to create custom adapter if you want custom layout of list row..

here you can see the example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM