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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:59:20+00:00 2026-05-31T01:59:20+00:00

I was trying a sample program to understand the behavior of the dynamic GridView

  • 0

I was trying a sample program to understand the behavior of the dynamic GridView. When I tried to execute the program I got a run-time exception at texts.setText(str[position]); in the .java file.

.java

package GridView_dynamic.grid;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class GridView_dynamic extends Activity {
    /** Called when the activity is first created. */
 TextView select; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        select = (TextView)findViewById(R.id.text);
        GridView grid = (GridView)findViewById(R.id.grid);
        grid.setAdapter(new my_Adapter(this));
    }
    public class my_Adapter extends BaseAdapter{
     String[] str = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N"};
     public Context context;
     public my_Adapter(Context context) {
      // TODO Auto-generated constructor stub
      this.context = context;
     }
     @Override
     public int getCount() {
      // TODO Auto-generated method stub
      return str.length;
     }
     @Override
     public Object getItem(int position) {
      // TODO Auto-generated method stub
      return null;
     }
     @Override
     public long getItemId(int position) {
      // TODO Auto-generated method stub
      return 0;
     }
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      if(convertView == null){
       Toast.makeText(context,"convertView is NULL", Toast.LENGTH_LONG).show();
      }
      TextView texts;
      texts = (TextView)convertView;
      //texts = (TextView)findViewById(R.id.text);
      texts.setText(str[position]);
      return texts;
     }

    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView 
android:id="@+id/text"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="#0000aa"
android:textColor="#00aa00"
/>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent" 
android:layout_height="match_parent"
android:background="#aaaaaa"
/>
</LinearLayout>
  • 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-31T01:59:22+00:00Added an answer on May 31, 2026 at 1:59 am

    You get a NullPointerException because when you get a null convertView(the list is first built) you don’t create a new TextView. Your getView() method should be:

        @Override
             public View getView(int position, View convertView, ViewGroup parent) {
              // TODO Auto-generated method stub
              if(convertView == null){
               Toast.makeText(context,"convertView is NULL", Toast.LENGTH_LONG).show();
               //if convertView is null is your job to make a new View 
                   convertView = new TextView(context);
              }
              ((TextView)convertView).setText(str[position]);
              return convertView;         
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying a program to understand the behavior of structure variable, Sample code:
I'm trying to run the sample program, which comes with Google Data API. I
I am trying to understand MVVM pattern and I took a sample program to
I am trying to write a sample Raw socket program to clear my understanding
This is a sample program i was trying to compile this below c program
You can find it here. I'm trying to understand his sample code in order
I am trying to run through a program to check if vouchers are valid
I've been learning C#, and I'm trying to understand lambdas. In this sample below,
I am able to run a sample hadoop program from the command prompt and
I am trying to run a simple program hello world in my android mobile.

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.