我有2项活动并使用AlertDialog
。每当我点击AlertDialog
的选项时,我的应用就崩溃了。我试图使用Intent将所选选项传递给Second Activity。这是一些代码。
MainActivity
if (d == R.id.play_btn) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose")
.setSingleChoiceItems(difficultyNames, 0, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialogInterface, int selected) {
dialogInterface.dismiss();
startPlay(selected);
}
});
// Creates the dialog
AlertDialog ad = builder.create();
ad.show();
}
private void startPlay(int selected) {
// start gameplay
Intent playIntent = new Intent(this, GameplayActivity.class);
playIntent.putExtra("level", selected);
this.startActivity(playIntent);
}
SecondActivity
Bundle extras = getIntent().getExtras();
if (extras != null) {
int passedLevel = extras.getInt("level", 0);
if (passedLevel >= 0) level = passedLevel;
}
崩溃日志
11:51:12 PM Broken pipe
java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
at com.android.ddmlib.JdwpPacket.write(JdwpPacket.java:179)
at com.android.ddmlib.Client.send(Client.java:654)
at com.android.ddmlib.jdwp.JdwpAgent.send(JdwpAgent.java:92)
at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349)
at com.android.ddmlib.Client.requestAllocationStatus(Client.java:517)
at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:573)
at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:545)
at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:507)
at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:403)
at com.android.ddmlib.DeviceMonitor.a... (show balloon)
11:53:02 PM Can't bind to local 8700 for debugger
11:53:31 PM Can't bind to local 8700 for debugger
11:53:42 PM Can't bind to local 8700 for debugger
11:54:04 PM Can't bind to local 8700 for debugger
11:54:05 PM Can't bind to local 8700 for debugger
11:54:19 PM Can't bind to local 8700 for debugger
11:54:20 PM Can't bind to local 8700 for debugger
11:54:42 PM Can't bind to local 8700 for debugger
11:54:43 PM Can't bind to local 8700 for debugger
11:54:48 PM Can't bind to local 8700 for debugger
11:54:53 PM Can't bind to local 8700 for debugger
11:54:54 PM Can't bind to local 8700 for debugger
11:55:01 PM Can't bind to local 8700 for debugger
11:55:51 PM Broken pipe
java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
at com.android.ddmlib.JdwpPacket.write(JdwpPacket.java:179)
at com.android.ddmlib.Client.send(Client.java:654)
at com.android.ddmlib.jdwp.JdwpAgent.send(JdwpAgent.java:92)
at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349)
at com.android.ddmlib.Client.requestAllocationStatus(Client.java:517)
at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:573)
at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:545)
at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:507)
at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:403)
at com.android.ddmlib.DeviceMonitor.a... (show balloon)
12:02:50 AM Broken pipe
java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
at com.android.ddmlib.JdwpPacket.write(JdwpPacket.java:179)
at com.android.ddmlib.Client.send(Client.java:654)
at com.android.ddmlib.jdwp.JdwpAgent.send(JdwpAgent.java:92)
at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349)
at com.android.ddmlib.Client.requestAllocationStatus(Client.java:517)
at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:573)
at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:545)
at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:507)
at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:403)
at com.android.ddmlib.DeviceMonitor.a... (show balloon)
12:05:58 AM Broken pipe
java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
at com.android.ddmlib.JdwpPacket.write(JdwpPacket.java:179)
at com.android.ddmlib.Client.send(Client.java:654)
at com.android.ddmlib.jdwp.JdwpAgent.send(JdwpAgent.java:92)
at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349)
at com.android.ddmlib.Client.requestAllocationStatus(Client.java:517)
at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:573)
at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:545)
at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:507)
at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:403)
at com.android.ddmlib.DeviceMonitor.a... (show balloon)
12:13:29 AM Broken pipe
java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
at com.android.ddmlib.JdwpPacket.write(JdwpPacket.java:179)
at com.android.ddmlib.Client.send(Client.java:654)
at com.android.ddmlib.jdwp.JdwpAgent.send(JdwpAgent.java:92)
at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349)
at com.android.ddmlib.Client.requestAllocationStatus(Client.java:517)
at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:573)
at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:545)
at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:507)
at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:403)