2

i faced this problem when trying to assign a character into an array of text view

counter is a count which i got from reading the number of characters i have in a text file

            TextView[] tv = new TextView[counter];

        for (int i = 0; i < counter; i++)
        {
            tv[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
            tv[i].setText(singleText[i]); 
            setContentView(tv[i]); 
        }

after this, when i try to run the application i the application just force closes.. i have no idea on how to debug it

my application would need to set 1 character into 1 text view

2
  • First step would be to send us the log output, which should have detailed information about the problem. Commented Feb 22, 2011 at 7:09
  • hey mike, the other bros helped me with the code already i shall try their methods... if it still doesn't work i will post the logs here thanks Commented Feb 22, 2011 at 7:50

2 Answers 2

5

You haven't initialized the TextViews properly. That's why you are getting nullpointerexception. You have to initialize the TextView as follows:

tv[i] = new TextView(this);

Here this is the your Activity instance.

And there is a problem in your

setContentView(tv[i]);

If you use this code then in the screen you will see only the last textview.

To see all the TextView you have to add all the TextView in a container like LinearLayout. Then you have set the container as content View.

Here is the code you can use:

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,              
        LayoutParams.WRAP_CONTENT));
TextView[] tv = new TextView[counter];

for (int i = 0; i < counter; i++)
{
        tv[i] = new TextView(this);
        tv[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,              
            LayoutParams.WRAP_CONTENT)); 
        tv[i].setText(singleText[i]); 
        linearLayout.addView(tv[i]); 
}
setContentView(linearLayout);

Hope it will help you.

Sign up to request clarification or add additional context in comments.

6 Comments

@benjamin: is singleText a character array?
@gypsicoder i got an error saying resources not found exception
i think you got it because of the character array. just change this line tv[i].setText(singleText[i]); to tv[i].setText(""+singleText[i]);
hey @gypsicoder the code works however i am facing a problem where if i have a few words the words would not overflow to the next line in the textview they would extend all the way out of screen
you can set singeLine attribute true for the TextViews
|
1

TextView[] tv = new TextView[counter]; Here you creating array filled with null references. Of course it will crash here tv[i].setLayoutParams() with null pointer exception

2 Comments

Actually this code is completely wrong. You should define some kind of layout in .xml and then add you TextViews in that layout
i would need to make x number of textviews depending on how many character i have

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.