I shall try my best to post the relevant code and explain clearly… I’m following a tutorial on Android; it was a ListView Application which I am now converting to fragments. It told me to change one of my classes from extends ListActivity to extends Activity. In doing so, two of my calls particular to ListActivity needed changing. I thought I changed them properly, but I am getting these errors (just the top of the list):
W/dalvikvm(4486): threadid=1: thread exiting with uncaught exception (group=0x40209760)
E/AndroidRuntime(4486): FATAL EXCEPTION: main
E/AndroidRuntime(4486): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.testing/com.example.testing.MainActivity}:
java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.ListView
My main activity code, the part I believe relevant, is:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_fragment);
ListView lv = (ListView) findViewById(R.id.list_fragment);
lv.setAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
R.array.tut_titles, R.layout.activity_main));
final String[] links = getResources().getStringArray(R.array.tut_links);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick......
}
});
}
The lv variable is new, and the two calls made with it used to be ListActivity specific. My list_fragment is merely this so far:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.testing.UrlFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_fragment">
</fragment>
Phew! I think that’s all that’s needed, but if I knew for certain, I could probably fix this myself. Can anyone help?
You have a problem in following line.
From R file you are referencing fragment but you are going to cast it to ListView that is your problem.you can simply solve it as follows.
1) create a class extending Fragment.
2) In your activity class.
3) for more check this out