2

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]);

Community
  • 1
  • 1
Deepak Mittal
  • 328
  • 1
  • 4
  • 10

4 Answers4

2

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

  android:id="@+id/listView1" in main.xml

to 

android:id="@+id/list"

because i copied the code and tried.. it just works fine for me..

5hssba
  • 8,079
  • 2
  • 33
  • 35
  • That explained a lot. Can you post a link which talks about default layouts, I had no idea they existed until you said. I've also posted the logcat. – Deepak Mittal Mar 24 '12 at 05:34
  • if your problem is solved mark an answer as accepted.. so that people wont try to find what the mistake is..in already solved problem.. =] – 5hssba Mar 24 '12 at 06:25
  • Still not solved. I'm getting NullPointerException which you can see in the logcat. – Deepak Mittal Mar 24 '12 at 06:27
  • i just copy-pasted the code from this page... in a new project... and i got the listview displayed along with checkbox...thn put this code in a new project... i mean re-build.. with same code.. it is working fine..have you changed this.. android:id="@+id/list"? – 5hssba Mar 24 '12 at 06:30
  • It worked. I created a new project and the same code worked. Also @+id/list doesn't work, @android:id/list works. Thanks for the help :) – Deepak Mittal Mar 24 '12 at 07:15
1

Instead of this

View rowView = inflater.inflate(R.layout.list_item, parent, false);

Use:

View rowView = inflater.inflate(R.layout.list_item, null);

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
0

Without viewing your Logcat, I'm going to guess your problem actually lies in the fact that you're providing setListAdapter(ListAdapter) with an ArrayAdapter rather than the requested ListAdapter -- but this shouldn't compile..

The other possibility is that you're imporperly dereferrencing the 'values' array (ie accessing an out of bounds location with values[position]).

Yeah -- what I'm guessing is that to set up the ListView, your ListViewActivity is going to iterate through the list 'getting views' untill it get's a null view. What you need to do is check that position is in bounds of the value array. If so, setup the textview like you're normally doing. If not, return null.

dcow
  • 7,765
  • 3
  • 45
  • 65
  • setListAdapter works fine with ArrayAdapter as you can see in http://developer.android.com/resources/tutorials/views/hello-listview.html This code is just extended version of that same code. I'm trying to put checkboxes next to the list items. I tried returning null in getView(), it still doesn't work. I've posted the logcat. – Deepak Mittal Mar 24 '12 at 05:36
  • Interesting. Yeah those are just a few of my guesses without trying anything. – dcow Mar 24 '12 at 10:04
-1

try this code

ArrayAdapter<String> adapter;

 static final String[] COUNTRIES = new String[] {
            "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
            "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
            "Armenia", "Aruba", "Australia", "Austria"
          };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);

        lv = (ListView) findViewById(R.id.edit_lv);
        arrayList = new ArrayList<HashMap<String, String>>();

        mDbHelper = new Database_creat(Show.this);
        mDbHelper.Open();
        adapter= new ArrayAdapter<String>  
                          (this,ndroid.R.layout.simple_list_item_1,COUNTRIES);



        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

            }

        });

    }
Android
  • 1,417
  • 9
  • 11