Here’s my code, I know there’s a small mistake somewhere but being a noob in Android I can’t figure it out. I’ve done my searching but to no avail.
The SimpleListActivity.java:
public class SimpleListActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new myArrayAdapter(this, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria"
};
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</CheckBox>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView">
</TextView>
</LinearLayout>
myArrayAdapter.java:
public class myArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public myArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
CheckBox checkbox = (CheckBox) rowView.findViewById(R.id.checkBox1);
textView.setText(values[position]);
return rowView;
}
}
I don’t understand why we are using main layout as ContentView. I mean the official listview tutorial doesn’t use it and the list works just fine. But I found it on the net that when I use findViewById, I gotta use setContentView(R.layout.main);. I don’t understand if the id is not in the main view, then why should I use it.
Also, this app force closes. I’ve narrowed down the problem to textView.setText(values[position]);. Where am I going wrong?
Update: I removed setContentView as it was giving another error of which solution is posted here: ListView whose id attribute is 'android.R.id.list' Error when I have the ListView id set correctly
Here’s the logcat after removing setContentView:
03-24 10:55:01.558: E/AndroidRuntime(7279): FATAL EXCEPTION: main
03-24 10:55:01.558: E/AndroidRuntime(7279): java.lang.NullPointerException
03-24 10:55:01.558: E/AndroidRuntime(7279): at com.deepakmittal.simplelist.myArrayAdapter.getView(myArrayAdapter.java:32)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.AbsListView.obtainView(AbsListView.java:1467)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.makeAndAddView(ListView.java:1745)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.fillDown(ListView.java:670)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.fillFromTop(ListView.java:727)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.layoutChildren(ListView.java:1598)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.AbsListView.onLayout(AbsListView.java:1273)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.os.Looper.loop(Looper.java:130)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-24 10:55:01.558: E/AndroidRuntime(7279): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 10:55:01.558: E/AndroidRuntime(7279): at java.lang.reflect.Method.invoke(Method.java:507)
03-24 10:55:01.558: E/AndroidRuntime(7279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-24 10:55:01.558: E/AndroidRuntime(7279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-24 10:55:01.558: E/AndroidRuntime(7279): at dalvik.system.NativeStart.main(Native Method)
The line 32 corresponds to textView.setText(values[position]);
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id “@+id/list”….so only if you need a customised layout you should use setContentView() or else.. no need to use R.layout.main.. now for the force close of your app.. you should post the logcat… so that it will be easy to find out where the error is..
and i dont think there is any error… except change the
because i copied the code and tried.. it just works fine for me..