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 6625285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:45:19+00:00 2026-05-25T21:45:19+00:00

I am following a good tutorial on using a gridview . I have been

  • 0

I am following a good tutorial on using a gridview. I have been unable to get the code to work however as ever time I compile and run the app force closes. Logcat says its “unable to instantiate activity CompnentInfo” amount a series of other errors. I’m not to handy with debugging so I’m at an impasse. This is my code:

public class GridViewDemo extends Activity {
    public String[] filenames = { 
            "File 1", 
            "File 2",
            "Roflcopters"
            };


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ButtonAdapter(this));

    }


    //Classes
    public class ButtonAdapter extends BaseAdapter {
         private Context mContext;

         // Gets the context so it can be used later
         public ButtonAdapter(Context c) {
          mContext = c;
         }

         // Total number of things contained within the adapter
         public int getCount() {
          return filenames.length;
         }

          // Require for structure, not really used in my code.
         public Object getItem(int position) {
          return null;
         }

         // Require for structure, not really used in my code. Can
         // be used to get the id of an item in the adapter for 
         // manual control. 
         public long getItemId(int position) {
          return position;
         }

         @SuppressWarnings("null")
        public View getView(int position, 
                                   View convertView, ViewGroup parent) {
          Button btn = null;
          btn.setOnClickListener(new MyOnClickListener(position)); 
          if (convertView == null) {  
           // if it's not recycled, initialize some attributes
           btn = new Button(mContext);
           btn.setLayoutParams(new GridView.LayoutParams(100, 55));
           btn.setPadding(8, 8, 8, 8);
           } 
          else {
           btn = (Button) convertView;

          }
          btn.setText(filenames[position]); 
          // filenames is an array of strings
          btn.setTextColor(Color.WHITE);
          btn.setBackgroundResource(R.drawable.icon);
          btn.setId(position);

          return btn;
         }
        }





    class MyOnClickListener implements OnClickListener {

        private final int position;

        public MyOnClickListener(int position) {
            this.position = position;
        }

        public void onClick(View v) {
            // Preform a function based on the position
            // someFunction(this.position)
            Toast.makeText(getApplicationContext(), this.position, Toast.LENGTH_SHORT).show();
        }
    }

XML:

    <?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

Manifest xml:

<?xml version="1.0" encoding="utf-8"?>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".GridviewActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

Any ideas why this crashes?
This is the logcat output:
enter image description here

  • 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-25T21:45:20+00:00Added an answer on May 25, 2026 at 9:45 pm

    First, I would recommend learning how to debug [see this article and this one for example]. It will become handy very soon…

    Second, next time please add a log from logcat, which shows the details for the exception which caused the “force close”.

    Regarding your problem, you are trying to call a method on a null object:

    Button btn = null;
    btn.setOnClickListener(new MyOnClickListener(position)); 
    

    which causes a null pointer exception. You should add the listener only after you assigned an object to btn, which is after the if-else blocks.

    One more things – you suppressed the null warning (@SuppressWarnings("null")), to avoid the warning, instead of taking care of it, and this way you got the null pointer exception. Unless you are hundred percents sure, don’t ignore the warnings.

    Edit:

    Looking at your manifest, this is a small typo. It should be

    <activity android:name=".GridViewActivity"
    

    Instead of:

    <activity android:name=".GridviewActivity"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a good tutorial on Java + Cucumber integration? I've begun using
I have been reading through what seems to be a good tutorial on rest
I'm new to jQuery and have been following the tutorial here for a Panel
I'm following this tutorial (seems good) for Rails. After I run ruby script/generate scaffold
How might one craft a good explanation of why the following code is not
The following code is from a tutorial ( http://net.tutsplus.com/php/creating-a-php5-framework-part-1/ ), not mine. I have
I have been using the Windsor IoC Container for my web-based application, to resolve
I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with
I am absolutely totally news to Rails and to MongoDB. I have been following
I am following this tutorial, trying to learn to make Magento extensions, however, in

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.