简体   繁体   中英

my app closes when I press "Jugar" button on screen

I am making a bingo app for work. I have made a few buttons to go from one screen to another. Everything goes corectly until I press "botonJugar" that is called "jugar" in the emulator screen.

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class BingoPalabras extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bingo_palabras);
    final Button botonJugar = (Button) findViewById(R.id.botonJugar);

    botonJugar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent miIntent = null;
            switch(v.getId()){
                case R.id.botonJugar:
                    miIntent = new Intent(BingoPalabras.this,VentanaJugar.class);
                    break;
            }
            if(miIntent!=null){
                startActivity(miIntent);
            }

        }
    });
}

}

The ventanaJugar XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VentanaJugar">


<Button
    android:id="@+id/botonRegistrar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:text="@string/registrar_usuario"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.078"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.186" />

The Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.eneko.bingo_dapas">

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".BingoPalabras">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".VentanaJugar" />
    <activity android:name=".VentanaAjustes" />
    <activity android:name=".RegistrarUsuario"></activity>
 </application>

 </manifest>

In addition, when I run the app a message appears: Waiting for target device to come online, the screen is always black. The logcat shows nothing

Thank you.

Change your code to this

Button botonJugar = (Button) findViewById(R.id.botonJugar);

    botonJugar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

               Intent  miIntent = new Intent(BingoPalabras.this,VentanaJugar.class);
                startActivity(miIntent);
            }
     }

You can't have uppercase for id switch case R.id.botonJugar: to case R.id.boton_jugar: and modify it inside your xml. Check also that you have register your activity inside the manifest.xml

  <activity
        android:name="package name of VentanaJugar"
        android:screenOrientation="portrait"
       />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM