Note that there are some explanatory texts on larger screens.

plurals
  1. POListView and adapter
    primarykey
    data
    text
    <p>I have created my own adapter for a ListView. Each row of the ListView is represented by two TextVews. I need to display some information in my ListView.</p> <p>Here is my activity code:</p> <pre><code>public class MyCountriesActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); int row_ID = R.layout.list_item; ListAdapter adapt = new MyAdapter(this,row_ID); setListAdapter(adapt); } } </code></pre> <p>Here is my adapter code:</p> <pre><code>public class MyAdapter extends ArrayAdapter&lt;String&gt; { Activity a; int row_ID; String[] countries = { "USA", "France" }; String[] years = { "1992", "2010" }; public MyAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); a = (Activity) context; row_ID = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row; if (convertView == null) { // Create new row view LayoutInflater inflater = a.getLayoutInflater(); row = inflater.inflate(row_ID, parent, false); } else // reuse old row view to save time/battery row = convertView; /* Add new data to row object */ TextView country = (TextView) row.findViewById(R.id.country); TextView year = (TextView) row.findViewById(R.id.year); country.setText(countries[position]); year.setText(years[position]); return row; } } </code></pre> <p>But unfortunately this application crashes. What is wrong in my program?</p> <p><strong>Log:</strong></p> <pre><code>03-04 17:41:11.044: E/AndroidRuntime(1277): FATAL EXCEPTION: main 03-04 17:41:11.044: E/AndroidRuntime(1277): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.ledinov.namespace/android.ledinov.namespace.MyCountriesActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread.access$600(ActivityThread.java:123) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.os.Handler.dispatchMessage(Handler.java:99) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.os.Looper.loop(Looper.java:137) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread.main(ActivityThread.java:4424) 03-04 17:41:11.044: E/AndroidRuntime(1277): at java.lang.reflect.Method.invokeNative(Native Method) 03-04 17:41:11.044: E/AndroidRuntime(1277): at java.lang.reflect.Method.invoke(Method.java:511) 03-04 17:41:11.044: E/AndroidRuntime(1277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 03-04 17:41:11.044: E/AndroidRuntime(1277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 03-04 17:41:11.044: E/AndroidRuntime(1277): at dalvik.system.NativeStart.main(Native Method) 03-04 17:41:11.044: E/AndroidRuntime(1277): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ListActivity.onContentChanged(ListActivity.java:243) 03-04 17:41:11.044: E/AndroidRuntime(1277): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.Activity.setContentView(Activity.java:1835) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.ledinov.namespace.MyCountriesActivity.onCreate(MyCountriesActivity.java:21) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.Activity.performCreate(Activity.java:4465) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 03-04 17:41:11.044: E/AndroidRuntime(1277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 03-04 17:41:11.044: E/AndroidRuntime(1277): ... 11 more </code></pre> <p><strong>main.xml:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload