Android 程序中的 AlertDialog.Builder 显示错误
我正在为 Android 中的应用程序构建登录页面。但在测试时,它在 AlertDialog.Builder 中给出错误,表示它尚未定义。我已经在其他应用程序中使用过它并且运行良好。提前致谢。 这是代码:
package project.login;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button launch = (Button)findViewById(R.id.login_button);
launch.setOnClickListener( new OnClickListener ()
{
public void onClick(View view)
{ EditText usernameEditText = (EditText) findViewById(R.id.username);
EditText passwordEditText = (EditText) findViewById(R.id.password);
String sUsername = usernameEditText.getText().toString();
String sPassword = passwordEditText.getText().toString();
if(usernameEditText == null || passwordEditText == null) {
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("You can't let the fields empty")
.show();
}
}
}
);
}
}
I was building a login page for an application in android. but while testing it gives the error at AlertDialog.Builder, saying that it has been not defined. I have used it in another apps and was working perfectly. Thanks in advance.
This is the code:
package project.login;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button launch = (Button)findViewById(R.id.login_button);
launch.setOnClickListener( new OnClickListener ()
{
public void onClick(View view)
{ EditText usernameEditText = (EditText) findViewById(R.id.username);
EditText passwordEditText = (EditText) findViewById(R.id.password);
String sUsername = usernameEditText.getText().toString();
String sPassword = passwordEditText.getText().toString();
if(usernameEditText == null || passwordEditText == null) {
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("You can't let the fields empty")
.show();
}
}
}
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的 OnClickListener 内部的 this 需要经过限定。尝试使用
The problem is that your this inside of your OnClickListener needs to be qualified. Try using
不要忘记先导入 android.app.AlertDialog。
Don't forget to import android.app.AlertDialog first.