0

I want to add editText dynamically. But when I click on the button, the next activity is closed forcefully. It gives me a nullpointer exception in the log.

When adding cotrol, is it needed to have control in the xml file for initialization??

package com.example.snooder;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText ed1;
    Button btn1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed1 = (EditText) findViewById(R.id.editText1);
        btn1 =(Button) findViewById(R.id.button1);

        btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this,players.class);
                i.putExtra("name",ed1.getText().toString());
                startActivity(i);       
            }
        });
    }
}

players.java

package com.example.snooder;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;

public class players extends Activity {

    LinearLayout player_layout;
    Bundle b;

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

        b = getIntent().getExtras();
        String resStr = b.getString("name");
        player_layout = (LinearLayout) findViewById(R.id.player_layout);

        EditText[] ed1 = new EditText[Integer.parseInt(resStr)];

        for(int i=1;i<=Integer.parseInt(resStr);i++)
        {
            ed1[i] = new EditText(this);
            player_layout.addView(ed1[i]);
            ed1[i].setId(i);
            ed1[i].setHint("enter player's name");
            ed1[i].setHeight(50);
            ed1[i].setWidth(500);
        }
    }
}
5
  • Can you post the XMLs and LogCat?
    – TronicZomB
    Jun 12, 2013 at 3:10
  • You haven't initialized each EditText in ed1. In the for loop, put ed1[i] = new EditText(this);, but I think you would also get an IndexOutofBoundsException. Check your for loop if you get that error.
    – Aprian
    Jun 12, 2013 at 3:23
  • Not an array out of bounds exception; a null pointer exception. ed1[] was initialized to an array of null pointers. Jun 12, 2013 at 4:36
  • p.s. if the user enters anything but a number in the "editText1" field, it's all going to blow up anyway. Jun 12, 2013 at 4:38
  • got it .and done it .thank u sir
    – Wilson S
    Jun 15, 2013 at 15:31

1 Answer 1

2

It is in about creating an array of EditTexts.When you do this:

EditText[] ed1 = new EditText[Integer.parseInt(resStr)];

Then you have a not null(ed1 in your case) array but each member of it is null,So when you want to add a null view to the layout,NPP occurs.You have to initialize members of the array before use them.For example:

for(int i=1;i<=Integer.parseInt(resStr);i++)
    {

        ed1[i] = new EditText(this);
        .
        .
        .
    }
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.