0
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);       
}

在这里,我首先编写了方法代码,然后将edittext元素链接到布局是导致此错误的原因吗?但我不这么认为,因为我以前做过......

当我将editext链接代码行附加到它们各自使用过的方法时,它没有显示任何错误,为什么会这样?我的意思是这不是我第一次在全球范围内声明edittext项目......

4

2 回答 2

1

您可以参考您的观点,利用布局膨胀发生,布局膨胀将在之后setContentView发生

将此代码放在onCreateaftersetContentView

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);
于 2013-05-27T16:25:37.023 回答
0

进行此更正。

@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);


}
于 2013-05-27T16:30:24.773 回答