我的代码有问题。当我选择一个按钮时,我试图禁用所有其他按钮。还有其他选项可以禁用其他按钮吗?
我希望它能同时突出显示一个按钮。
public void onClick(View v) {
if (flaga == 0) {
switch (v.getId()) {
case R.id.vga1:
try {
buttonVmute.setEnabled(true);
buttonStart.setEnabled(false);
ClientThread clientThread = new ClientThread("192.168.1.123");
new Thread(clientThread).start();
Thread.sleep(20);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(soket.getOutputStream())), true);
Thread.sleep(10);
out.println("");
Thread.sleep(10);
out.println("bbbb");
Thread.interrupted();
} catch (UnknownHostException | NullPointerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.hdmi1:
try {
buttonVmute.setEnabled(false);
buttonStart.setEnabled(true);
ClientThread clientThread = new ClientThread("192.168.1.124");
new Thread(clientThread).start();
Thread.sleep(10);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(soket.getOutputStream())), true);
Thread.sleep(10);
out.println("");
Thread.sleep(10);
out.println("nnnnnn");
Thread.interrupted();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
} else {
Toast.makeText(getApplicationContext(), "Podłącz wifi", Toast.LENGTH_LONG).show();
}
}
有一个logcat:
04-21 14:36:37.038 20086-20086/com.example.mateusz.tabfragment W/System.err﹕ java.lang.NullPointerException
感谢您的回复。
答案 0 :(得分:0)
buttonVmute and buttonStart
,它们可能没有标识布局按钮的值。
尝试在Try {}中创建此变量和findView元素的新实例。
try{
buttonVmute = (ImageButton) v.findViewById(R.id.button_name);
buttonVmute.setEnabled(true);
}
看看它是否在同一行崩溃,或者你的问题已经解决了。