I am trying to create a simple Android app and I'm getting this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twista.shopinglist/com.twista.shopinglist.ItemDetail}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
I tried to google it and fix via some answers here, but unsuccessfuly it didn't resolve :(.
I have a MainActivity
with ListView
, in onCreate()
I call
public class MainActivity extends Activity {
....
@Override
protected void onCreate(Bundle savedInstanceState) {
...
final ListView listView = (ListView) findViewById(R.id.overListView);
List<Item> values = dataSource.getItems();
final ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(this,
android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item i = adapter.getItem(position);
Intent intent = new Intent(MainActivity.this, ItemDetail.class);
Bundle bundle = new Bundle();
bundle.putLong("my_id", i.getId());
intent.putExtras(bundle);
startActivity(intent);
}
});
this code should start new activity, ItemDetail
, whose code is here:
public class ItemDetail extends Activity {
private ItemsDataSource dataSource;
private long itemId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
Bundle bundle = getIntent().getExtras();
itemId = bundle.getLong("my_id");
Intent intent = getIntent();
dataSource = new ItemsDataSource(this);
try {
dataSource.open();
} catch (SQLException e) {
e.printStackTrace();
}
ListView listView = (ListView) findViewById(R.id.detailListView);
List<SubItem> values = dataSource.getSubItemOfItem(itemId);
ArrayAdapter<SubItem> adapter = new ArrayAdapter<SubItem>(this,
android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
i'm still get this error:
09-10 22:30:30.555 10695-10695/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twista.shopinglist/com.twista.shopinglist.ItemDetail}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
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:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
at android.widget.AdapterView.addView(AdapterView.java:477)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:750)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:323)
at android.app.Activity.setContentView(Activity.java:1881)
at com.twista.shopinglist.ItemDetail.onCreate(ItemDetail.java:27)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
layout file for second (detail activity):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/detailListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:id="@+id/groupSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/add_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:onClick="onClick"/>
<Button
android:id="@+id/delete_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete First"
android:onClick="onClick"/>
</LinearLayout>
</ListView>
Many thanks for any advice.
Your XML file contains Views that are children of a ListView, you cannot do this. Your XML file should look liks this: