public class Dialog { private String icon; private String title; private String message; private Button submit; private Button cancel; private Button close; private Dialog(){ } private Dialog(String icon, String title, String message, Button submit, Button cancel, Button close) { this.icon = icon; this.title = title; this.message = message; this.submit = submit; this.cancel = cancel; this.close = close; } public void show(){ //弹出 } public void dismiss(){ //关闭 } static class Builder{ private Dialog dialog = new Dialog(); public Builder title(String title){ dialog.setTitle(title); return this; } public Builder icon(String icon){ dialog.setIcon(icon); return this; } public Builder message(String message){ dialog.setMessage(message); return this; } public Dialog build(){ return dialog; } } public void setSubmitText(String text){ submit.setLabel(text); } public void setCancelText(String text){ cancel.setLabel(text); } public void setCloseText(String text){ close.setLabel(text); } /** * 回调函数 * @param callback */ public void addListenerOnSubmit(SubmitCallBackListener callback){ submit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //当用户对按钮执行了动作操作以后, //程序会到此处执行 callback.submitCallback(e); } }); } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } public interface SubmitCallBackListener { void submitCallback(ActionEvent e); } public class Test { public static void main(String[] args) { System.out.println("hello world"); // 构建对话框 Dialog.Builder builder = new Dialog.Builder(); builder.message("天气多变,注意增减衣物"); Dialog dialog = builder.build(); // 设置确定按钮的文本和回调函数 dialog.setSubmitText("确定"); dialog.addListenerOnSubmit(new SubmitCallBackListener(){ @Override public void submitCallback(ActionEvent e) { System.out.println("111"); } }); // 显示对话框 dialog.show(); } }为什么出错,怎么解决
时间: 2023-12-20 13:06:49 浏览: 74
在代码中,Dialog类中的submit、cancel和close按钮没有进行初始化,所以在调用它们的方法时会出现NullPointerException异常。你需要在Dialog类的构造函数中对这些按钮进行初始化,或者在Dialog类中添加对应的setter方法。例如:
```
public Dialog(String icon, String title, String message, Button submit, Button cancel, Button close) {
this.icon = icon;
this.title = title;
this.message = message;
this.submit = submit;
this.cancel = cancel;
this.close = close;
this.submit = new Button(); // 初始化submit按钮
this.cancel = new Button(); // 初始化cancel按钮
this.close = new Button(); // 初始化close按钮
}
```
另外,你需要确保导入了Button和ActionEvent类:
```
import java.awt.Button;
import java.awt.event.ActionEvent;
```
相关问题
import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Dialog { private String icon; private String title; private String message; private Button submit; private Button cancel; private Button close; private Dialog(){ } private Dialog(String icon, String title, String message, Button submit, Button cancel, Button close) { this.icon = icon; this.title = title; this.message = message; this.submit = submit; this.cancel = cancel; this.close = close; this.submit = new Button(); // 初始化submit按钮 this.cancel = new Button(); // 初始化cancel按钮 this.close = new Button(); // 初始化close按钮 } public void show(){ //弹出 } public void dismiss(){ //关闭 } static class Builder{ private Dialog dialog = new Dialog(); public Builder title(String title){ dialog.setTitle(title); return this; } public Builder icon(String icon){ dialog.setIcon(icon); return this; } public Builder message(String message){ dialog.setMessage(message); return this; } public Dialog build(){ return dialog; } } public void setSubmitText(String text){ submit.setLabel(text); } public void setCancelText(String text){ cancel.setLabel(text); } public void setCloseText(String text){ close.setLabel(text); } /** * 回调函数 * @param callback */ public void addListenerOnSubmit(SubmitCallBackListener callback){ submit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //当用户对按钮执行了动作操作以后, //程序会到此处执行 callback.submitCallback(e); } }); } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }这段代码怎么在myeclipse中运行出对话框
这段代码只是定义了一个 Dialog 类,它包含了对话框的基本属性和方法,但并没有实现对话框的显示和交互逻辑。你需要在这个类的 show() 方法中添加对话框的显示逻辑,可以使用 Java 的 GUI 库(如 Swing 或 JavaFX)来创建对话框的 UI,并在 submit、cancel、close 按钮的点击事件中添加对应的交互逻辑。具体实现方式可以参考 Java GUI 相关的教程和示例代码。
public class MainActivity extends AppCompatActivity { private EditText ipEditText; private EditText portEditText; private TextView messageTextView; private BufferedReader in; private PrintWriter out; private Socket socket; private Handler handler = new Handler(); @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ipEditText = findViewById(R.id.ipEditText); portEditText = findViewById(R.id.portEditText); messageTextView = findViewById(R.id.messageTextView); Button connectButton = findViewById(R.id.connectButton); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String ip = ipEditText.getText().toString(); int port = Integer.parseInt(portEditText.getText().toString()); new Thread(new Runnable() { @Override public void run() { try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // 在主页面中的窗口中实时显示消息 while (true) { String message = in.readLine(); if (message == null) { break; } handler.post(new Runnable() { @Override public void run() { messageTextView.append(message + "\n"); } }); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }); Button sendMessageButton = findViewById(R.id.sendMessageButton); sendMessageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showSendMessageDialog(); } }); } @Override protected void onDestroy() { super.onDestroy(); try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } private void showSendMessageDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_send_message, null); final EditText messageEditText = view.findViewById(R.id.messageEditText); builder.setView(view) .setTitle("发送消息") .setPositiveButton("发送", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String message = messageEditText.getText().toString(); if (out != null) { out.println(message); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .create() .show(); } }能帮我将这段代码中的发送信息从主线程改成子线程吗
您好,以下是将发送信息从主线程中改为子线程的代码:
```java
sendMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showSendMessageDialog();
}
});
private void showSendMessageDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_send_message, null);
final EditText messageEditText = view.findViewById(R.id.messageEditText);
builder.setView(view)
.setTitle("发送消息")
.setPositiveButton("发送", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String message = messageEditText.getText().toString();
if (out != null) {
new Thread(new Runnable() {
@Override
public void run() {
out.println(message);
}
}).start();
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.create()
.show();
}
```
在这个改动中,我们将发送消息的操作放到了一个新的子线程中,防止阻塞主线程。
阅读全文