hi i am a new developer in android, i am being a trainer and i am trying to create an alert box.
In my project i have placed 2 edit text boxes and if any of them is being empty i want to pop up an alert box if both the boxes are filled up it will moves over to a new page.
the following is my coding
{ b = (Button)findViewById(R.id.widget30);
et1 = (EditText)findViewById(R.id.et1);
et2 = (Edit开发者_如何学GoText)findViewById(R.id.et2);
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(et1.getText().toString().length() == 0 )
{
et.setVisibility(View.VISIBLE);
alertbox();
}
else if (et2.getText().toString().length() == 0)
{
et.setVisibility(View.VISIBLE);
alertbox();
}
else
{
main.this.finish();
Intent myIntent = new Intent(v.getContext(), T.class);
startActivityForResult(myIntent, 0);
}
}
});
}
public void alertbox()
{
et = new TextView(this);
Builder alert =new AlertDialog.Builder(main.this);
alert.setTitle("Alert");
alert.setMessage("Required all fields");
alert.setView(et);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.cancel();
}
});
AlertDialog alert1 = alert.create();
alert1.show();
}
}
It is moving over to a new page but the alert box is not opening, following is the error in logcat
java.lang.NullPointerException
in the following lines of my coding
{
if(et1.getText().toString().length() == 0 )
{
et.setVisibility(View.VISIBLE);
alertbox();
}
else if (et2.getText().toString().length() == 0)
{
et.setVisibility(View.VISIBLE);
alertbox();
}
}
}
pls help me to identify what mistake i have did
probably on this line
et.setVisibility(View.VISIBLE);
et
is a null
object as it not exists. It seams you create only after you call alertbox()
You should consider looking at the documentation: http://developer.android.com/guide/topics/ui/dialogs.html
The Android SDK takes care of dialogs for you via the showDialog() and dismissDialog() methods. You would add need to add an onCreateDialog method and add your creation code here.
Android also reuses a dialog once it's been created, whereas in your code you would create a new one each time.
This is the very basics of what you can do and there are many more options.
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(et1.getText().toString().length() == 0 )
{
alertbox();
}
else if (et2.getText().toString().length() == 0)
{
alertbox();
}
else
{
Intent myIntent = new Intent(main.this, T.class);
startActivity(myIntent);
}
}
});`
精彩评论