应用程序使力量停止

时间:2019-09-04 18:29:43

标签: java android-studio crash

我正在使用android studio创建井字游戏,但我的应用程序不断崩溃

我在模拟器上测试了此应用,并在此处复制了logcat!

这是我的代码

public class LoginActivity extends AppCompatActivity {
private TextView twoPlayerLabel;
private TextView optionsLabel;
private MediaPlayer intro;



  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

    twoPlayerLabel = (TextView) findViewById(R.id.two_player_label);
    optionsLabel = (TextView) findViewById(R.id.options_label);




}


public void twoPlayer(View v) {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();

    final View layout = inflater.inflate(R.layout.two_player_options, null);
    final Intent twoPlayerLocal = new Intent(LoginActivity.this, TwoPlayerActivityLocal.class);
    final TextView playerOneName = (TextView) layout.findViewById(R.id.player_one);
    final TextView playerTwoName = (TextView) layout.findViewById(R.id.player_two);
    final RadioButton localGame = (RadioButton) layout.findViewById(R.id.local_game);
    final RadioButton bluetoothGame = (RadioButton) layout.findViewById(R.id.bluetooth_game);
    final RadioButton oMarker = (RadioButton) layout.findViewById(R.id.o_marker);
    final RadioButton xMarker = (RadioButton) layout.findViewById(R.id.x_marker);

    localGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playerTwoName.setVisibility(View.VISIBLE);
            oMarker.setVisibility(View.VISIBLE);
            xMarker.setVisibility(View.VISIBLE);
        }
    });

    bluetoothGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playerTwoName.setVisibility(View.GONE);
            oMarker.setVisibility(View.GONE);
            xMarker.setVisibility(View.GONE);

        }
    });

    oMarker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            oMarker.setBackgroundResource(R.drawable.tic_tac_toe_o_black);
            xMarker.setBackgroundResource(R.drawable.tic_tac_toe_x);
        }
    });

    xMarker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            xMarker.setBackgroundResource(R.drawable.tic_tac_toe_x_black);
            oMarker.setBackgroundResource(R.drawable.tic_tac_toe_o);
        }
    });
    builder.setView(layout)
            .setTitle("Two Player Game Options")
            .setPositiveButton("Start", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    if (localGame.isChecked()) {

                        if ((playerOneName.getText().length() > 0) && (playerTwoName.getText().length() > 0)) {

                            if (oMarker.isChecked()) {
                                Player playerOne = new Player(playerOneName.getText().toString(), 'o', 0);
                                Player playerTwo = new Player(playerTwoName.getText().toString(), 'x', 0);
                                twoPlayerLocal.putExtra("playerOne", playerOne);
                                twoPlayerLocal.putExtra("playerTwo", playerTwo);
                                twoPlayerLocal.putExtra("isTurn", playerOne.getPlayerMarker());
                                startActivity(twoPlayerLocal);
                            } else if (xMarker.isChecked()) {
                                Player playerOne = new Player(playerOneName.getText().toString(), 'x', 0);
                                Player playerTwo = new Player(playerTwoName.getText().toString(), 'o', 0);
                                twoPlayerLocal.putExtra("playerOne", playerOne);
                                twoPlayerLocal.putExtra("playerTwo", playerTwo);
                                startActivity(twoPlayerLocal);
                            }

                            else {
                                Toast.makeText(LoginActivity.this, "Please select the starting marker", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(LoginActivity.this, "Please enter a name for the players", Toast.LENGTH_SHORT).show();
                        }
                    } else if (bluetoothGame.isChecked()) {
                        Intent bluetoothIntent = new Intent(LoginActivity.this, TwoPlayerActivityBluetooth.class);
                        String playerOne = null;
                        if (playerOneName.getText().length() > 0) {
                            playerOne = playerOneName.getText().toString();
                            bluetoothIntent.putExtra("playerOne", playerOne);
                            startActivity(bluetoothIntent);
                        } else {
                            Toast.makeText(LoginActivity.this, "Please enter a player name", Toast.LENGTH_SHORT).show();
                        }

                    } else {

                        Toast.makeText(LoginActivity.this, "Please Select a game type", Toast.LENGTH_SHORT).show();
                    }

                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                }
            });
    // Create the AlertDialog object and return it

    builder.create().show();
}

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to quit the game?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            });
    builder.create().show();
}


}

这是logcat!

2019-09-04 22:44:37.659 17644-17644/? I/ir.puzzle.douz: Not late-enabling -Xcheck:jni (already on) 2019-09-04 22:44:37.697 17644-17644/? E/ir.puzzle.douz: Unknown bits set in runtime_flags: 0x8000 2019-09-04 22:44:37.699 17644-17644/? W/ir.puzzle.douz: Unexpected CPU variant for X86 using defaults: x86 2019-09-04 22:44:38.014 17644-17670/ir.puzzle.douz D/libEGL: Emulator has host GPU support, qemu.gles is set to 1. 2019-09-04 22:44:38.036 17644-17670/ir.puzzle.douz W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied) 2019-09-04 22:44:38.023 17644-17644/ir.puzzle.douz W/RenderThread: type=1400 audit(0.0:157): avc: denied { write } for name="property_service" dev="tmpfs" ino=6800 scontext=u:r:untrusted_app:s0:c134,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 2019-09-04 22:44:38.082 17644-17670/ir.puzzle.douz D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so 2019-09-04 22:44:38.092 17644-17670/ir.puzzle.douz D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so 2019-09-04 22:44:38.129 17644-17670/ir.puzzle.douz D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so 2019-09-04 22:44:38.272 17644-17644/ir.puzzle.douz W/ir.puzzle.douz: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed) 2019-09-04 22:44:38.273 17644-17644/ir.puzzle.douz W/ir.puzzle.douz: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed) 2019-09-04 22:44:38.412 17644-17644/ir.puzzle.douz D/AndroidRuntime: Shutting down VM 2019-09-04 22:44:38.416 17644-17644/ir.puzzle.douz E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ir.puzzle.douz, PID: 17644
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.puzzle.douz/ir.puzzle.douz.view.LoginActivity}: java.lang.IndexOutOfBoundsException: index=0 out of bounds (limit=0, nb=4)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.IndexOutOfBoundsException: index=0 out of bounds (limit=0, nb=4)
        at java.nio.Buffer.checkIndex(Buffer.java:564)
        at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:570)
        at android.graphics.fonts.FontFileUtil.analyzeStyle(FontFileUtil.java:94)
        at android.graphics.fonts.Font$Builder.build(Font.java:364)
        at android.graphics.Typeface$Builder.build(Typeface.java:596)
        at android.graphics.Typeface.createFromAsset(Typeface.java:960)
        at ir.puzzle.douz.view.LoginActivity.onCreate(LoginActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

我不知道总是强制关闭此代码的问题在哪里!您能告诉我哪一部分代码有问题吗?以及我该如何解决?

0 个答案:

没有答案