ceanan007
ceanan007

Reputation: 31

Android:NullpointerException

EditText et1=(EditText)findViewById(R.id.editText1);//gives nulllpointerexception here.. as well at all the linking code below
EditText et2=(EditText)findViewById(R.id.editText2);
EditText et3=(EditText)findViewById(R.id.editText3);
EditText et4=(EditText)findViewById(R.id.editText4);


Button bt1=(Button)findViewById(R.id.button1);
Button bt2=(Button)findViewById(R.id.button2);
Button bt3=(Button)findViewById(R.id.button3);
Button bt4=(Button)findViewById(R.id.button4);
Button bt5=(Button)findViewById(R.id.button5);

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

public void onContacts(View v)
{
    Intent i=new Intent(Intent.ACTION_VIEW, ContactsContract.Contacts.CONTENT_URI);
    startActivity(i);
}

public void onBrowse(View v)
{

    String s1= et1.getText().toString();
    Intent i= new Intent(Intent.ACTION_VIEW, Uri.parse(s1));
    startActivity(i);

}


public void onSearch(View v)
{

    String s1= et2.getText().toString();
    Intent i=new Intent(Intent.ACTION_WEB_SEARCH);
    i.putExtra(SearchManager.QUERY, s1);
    startActivity(i);
}

public void onMap(View v)
{

    String s1= et3.getText().toString();
    Intent i=new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+s1));
    startActivity(i);
}
public void onCall(View v)
{

    String s1= et4.getText().toString();
    Intent i=new Intent(Intent.ACTION_SEARCH,Uri.parse("tel:"+s1));
    startActivity(i);       
}

here i first wrote the method codes then linked the edittext elements to the layout is that the cause to this error? but i dont think so, as i have done that before...

when i attache the editext link code line's to their respective methods where they have been used it doesn't show any error,why is it so? I mean this is not the first time i am declaring the edittext items globally...

Upvotes: 0

Views: 111

Answers (2)

Chintan Soni
Chintan Soni

Reputation: 25287

Make this correction.

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

EditText et1=(EditText)findViewById(R.id.editText1);
EditText et2=(EditText)findViewById(R.id.editText2);
EditText et3=(EditText)findViewById(R.id.editText3);
EditText et4=(EditText)findViewById(R.id.editText4);


Button bt1=(Button)findViewById(R.id.button1);
Button bt2=(Button)findViewById(R.id.button2);
Button bt3=(Button)findViewById(R.id.button3);
Button bt4=(Button)findViewById(R.id.button4);
Button bt5=(Button)findViewById(R.id.button5);


}

Upvotes: 0

Pragnani
Pragnani

Reputation: 20155

You can refer your views, util the layout inflation takes place, Layout inflation will takes place after setContentView so

Place this code inside of onCreate after setContentView

EditText et2=(EditText)findViewById(R.id.editText2);
EditText et3=(EditText)findViewById(R.id.editText3);
EditText et4=(EditText)findViewById(R.id.editText4);


Button bt1=(Button)findViewById(R.id.button1);
Button bt2=(Button)findViewById(R.id.button2);
Button bt3=(Button)findViewById(R.id.button3);
Button bt4=(Button)findViewById(R.id.button4);
Button bt5=(Button)findViewById(R.id.button5);

Upvotes: 1

Related Questions