Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:54:02+00:00 2026-05-31T19:54:02+00:00

Here’s my code, I know there’s a small mistake somewhere but being a noob

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:54:04+00:00Added an answer on May 31, 2026 at 7:54 pm

    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..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code sample, let me know if it can be further improved?
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here is my code, which takes two version identifiers in the form 1, 5,
Here is an example: I write html code inside of textarea, then I swap
Here's my Manifest: <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=com.mappp.mobile android:versionCode=1 android:versionName=1.0 > <supports-screens android:largeScreens=true
Here is the situation. I am making changes to an application but I do
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code (Say we have a single button on the page that
here are 2 screen shots when i try to debug my code in visual
Here a simple question : What do you think of code which use try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.