【问题标题】:my app closes when I press "Jugar" button on screen当我按下屏幕上的“Jugar”按钮时,我的应用程序关闭
【发布时间】:2018-10-03 09:24:16
【问题描述】:

我正在制作一个用于工作的宾果游戏。我制作了几个按钮来从一个屏幕转到另一个屏幕。一切正常,直到我在模拟器屏幕中按下称为“jugar”的“botonJugar”。

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);
            }

        }
    });
}

}

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" />

Android 清单:

<?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>

另外,当我运行应用程序时,会出现一条消息:等待目标设备上线,屏幕总是黑屏。 logcat 什么也没有显示

谢谢。

【问题讨论】:

标签: java android android-studio


【解决方案1】:

把你的代码改成这个

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);
            }
     }

【讨论】:

    【解决方案2】:

    您不能将 id 的大写切换 case R.id.botonJugar:case R.id.boton_jugar: 并在您的 xml 中修改它。 还要检查您是否已在 manifest.xml 中注册您的活动

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多