按钮关闭应用而不是切换活动

huangapple go评论78阅读模式
英文:

Button closing the app instead of switching activity

问题

Main Activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button play = (Button)findViewById(R.id.play);
        Button quit = (Button)findViewById(R.id.quit);
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGame();
            }
        });
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quitGame();
            }
        });
    }
    void openGame() {
        Intent intent = new Intent(MainActivity.this, Game.class);
        startActivity(intent);
    }
    void quitGame() {
        finish();
        System.exit(0);
    }
}

Game Activity:

public class Game extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        final ConstraintLayout constraintLayout = (ConstraintLayout)findViewById(R.id.bg);
        constraintLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                String color1[] = {"#fb5959", "#fe7738", "#f3980b", "#DBD100", "#A8D700", "#87E50C"};
                String color2[] = {"#54F370", "#00FFBC", "#00FFFF", "#00FFE9", "#00C8FF", "#0085FF"};
                Random random = new Random();
                GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BL_TR,
                    new int[]{Color.parseColor(color1[random.nextInt(6)]), Color.parseColor(color2[random.nextInt(6)])});
                constraintLayout.setBackgroundDrawable(gradientDrawable);
                return false;
            }
        });
        Button one = (Button) findViewById(R.id.one);
        Button two = (Button) findViewById(R.id.two);
        Button three = (Button) findViewById(R.id.three);
        Button four = (Button) findViewById(R.id.four);
        Button five = (Button) findViewById(R.id.five);
        Button six = (Button) findViewById(R.id.six);
        Button seven = (Button) findViewById(R.id.seven);
        Button eight = (Button) findViewById(R.id.eight);
        Button nine = (Button) findViewById(R.id.nine);
    }
}

Game Activity XML:

<androidx.constraintlayout.widget.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:id="@+id/bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0D95DF"
    tools:context=".Game">

    <!-- Buttons one through nine -->
    <Button
        android:id="@+id/one"
        android:layout_width="91dp"
        android:layout_height="90dp"
        android:background="?android:attr/selectable"
        android:text="?"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toStartOf="@+id/guideline4"
        app:layout_constraintHorizontal_bias="0.015"
        app:layout_constraintStart_toStartOf="@+id/guideline3"
        app:layout_constraintTop_toTopOf="@+id/guideline1"
        app:layout_constraintVertical_bias="0.029" />

    <!-- ... More button elements ... -->

    <!-- Player labels and input fields -->
    <TextView
        android:id="@+id/player1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Player 1 (X) :"
        android:textColor="#000000"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.177"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.098" />

    <!-- ... More player input fields ... -->

    <!-- Guidelines for layout positioning -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="20dp"
        app:layout_constraintGuide_percent="0.3" />

    <!-- ... More guidelines ... -->

    <!-- Divider lines for the game board -->
    <View
        android:id="@+id/vertical1"
        android:layout_width="4dp"
        android:layout_height="282dp"
        android:background="?android:attr/listDivider"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toStartOf="@+id/guideline4"
        app:layout_constraintHorizontal_bias="0.333"
        app:layout_constraintStart_toStartOf="@+id/guideline3"
        app:layout_constraintTop_toTopOf="@+id/guideline1"
        app:layout_constraintVertical_bias="0.5"
        tools:visibility="visible" />

    <!-- ... More divider lines ... -->

</androidx.constraintlayout.widget.ConstraintLayout>

Stack Trace (Error message indicating issues with inflating Button and attributes):

05/05 13:14:41: Launching 'app' on OnePlus ONE A2003.
$ adb shell am start -n "com.example.tictactoe/com.example.tictactoe.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 21369 on device 'oneplus-one_a2003-3222ee10'.
...
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
...
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x10101e6 a=-1}
...

Please note that I've omitted the part of the stack trace that is not relevant to the code and translation. If you have any specific questions or need further assistance, feel free to ask.

英文:

I am trying to make a game Tic-Tac-Toe using android studio. When I try to switch from MainActivity to another activity(Game), instead of switching, the button closes the app. I have added several buttons in Game activity but have not used them and android studio is not showing any error about that so is the problem in Game activity or MainActivity? And how to solve this problem.
Main Activity:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button play = (Button)findViewById(R.id.play);
Button quit = (Button)findViewById(R.id.quit);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openGame();
}
});
quit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quitGame();
}
});
}
void openGame() {
Intent intent = new Intent(MainActivity.this, Game.class);
startActivity(intent);
}
void quitGame() {
finish();
System.exit(0);
}
}

Game Activity:

public class Game extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ConstraintLayout constraintLayout = (ConstraintLayout)findViewById(R.id.bg);
constraintLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
String color1[] = {&quot;#fb5959&quot;, &quot;#fe7738&quot;, &quot;#f3980b&quot;, &quot;#DBD100&quot;, &quot;#A8D700&quot;, &quot;#87E50C&quot;};
String color2[] = {&quot;#54F370&quot;, &quot;#00FFBC&quot;, &quot;#00FFFF&quot;, &quot;#00FFE9&quot;, &quot;#00C8FF&quot;, &quot;#0085FF&quot;};
Random random = new Random();
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BL_TR,
new int[]{Color.parseColor(color1[random.nextInt(6)]),Color.parseColor(color2[random.nextInt(6)])});
constraintLayout.setBackgroundDrawable(gradientDrawable);
return false;
}
});
Button one = (Button) findViewById(R.id.one);
Button two = (Button) findViewById(R.id.two);
Button three = (Button) findViewById(R.id.three);
Button four = (Button) findViewById(R.id.four);
Button five = (Button) findViewById(R.id.five);
Button six = (Button) findViewById(R.id.six);
Button seven = (Button) findViewById(R.id.seven);
Button eight = (Button) findViewById(R.id.eight);
Button nine = (Button) findViewById(R.id.nine);
}
}

Stack Trace:

05/05 13:14:41: Launching &#39;app&#39; on OnePlus ONE A2003.
$ adb shell am start -n &quot;com.example.tictactoe/com.example.tictactoe.MainActivity&quot; -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 21369 on device &#39;oneplus-one_a2003-3222ee10&#39;.
Capturing and displaying logcat messages from application. This behavior can be disabled in the &quot;Logcat output&quot; section of the &quot;Debugger&quot; settings page.
I/Choreographer: Skipped 107 frames!  The application may be doing too much work on its main thread.
E/AbstractTracker: Can&#39;t create handler inside thread that has not called Looper.prepare()
D/AppTracker: App Event: start
E/AbstractTracker: Can&#39;t create handler inside thread that has not called Looper.prepare()
D/AppTracker: App Event: stop
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tictactoe, PID: 21369
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tictactoe/com.example.tictactoe.Game}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.tictactoe.Game.onCreate(Game.java:20)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)&#160;
at android.app.ActivityThread.access$900(ActivityThread.java:155)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)&#160;
at android.os.Handler.dispatchMessage(Handler.java:102)&#160;
at android.os.Looper.loop(Looper.java:152)&#160;
at android.app.ActivityThread.main(ActivityThread.java:5497)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)&#160;
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class Button
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)&#160;
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)&#160;
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)&#160;
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)&#160;
at com.example.tictactoe.Game.onCreate(Game.java:20)&#160;
at android.app.Activity.performCreate(Activity.java:6289)&#160;
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)&#160;
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)&#160;
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)&#160;
at android.app.ActivityThread.access$900(ActivityThread.java:155)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)&#160;
at android.os.Handler.dispatchMessage(Handler.java:102)&#160;
at android.os.Looper.loop(Looper.java:152)&#160;
at android.app.ActivityThread.main(ActivityThread.java:5497)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)&#160;
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x10101e6 a=-1}
at android.content.res.TypedArray.getDrawable(TypedArray.java:867)
at android.view.View.&lt;init&gt;(View.java:3964)
at android.widget.TextView.&lt;init&gt;(TextView.java:680)
at android.widget.Button.&lt;init&gt;(Button.java:109)
at android.widget.Button.&lt;init&gt;(Button.java:105)
at androidx.appcompat.widget.AppCompatButton.&lt;init&gt;(AppCompatButton.java:72)
at androidx.appcompat.widget.AppCompatButton.&lt;init&gt;(AppCompatButton.java:68)
at androidx.appcompat.app.AppCompatViewInflater.createButton(AppCompatViewInflater.java:192)
at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:111)
at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)&#160;
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)&#160;
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)&#160;
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)&#160;
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)&#160;
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)&#160;
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)&#160;
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)&#160;
at com.example.tictactoe.Game.onCreate(Game.java:20)&#160;
at android.app.Activity.performCreate(Activity.java:6289)&#160;
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)&#160;
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)&#160;
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)&#160;
at android.app.ActivityThread.access$900(ActivityThread.java:155)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)&#160;
at android.os.Handler.dispatchMessage(Handler.java:102)&#160;
at android.os.Looper.loop(Looper.java:152)&#160;
at android.app.ActivityThread.main(ActivityThread.java:5497)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)&#160;
D/AppTracker: App Event: crash
I/Process: Sending signal. PID: 21369 SIG: 9

Game Activity Xml Code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/bg&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;#0D95DF&quot;
tools:context=&quot;.Game&quot;&gt;
&lt;Button
android:id=&quot;@+id/one&quot;
android:layout_width=&quot;91dp&quot;
android:layout_height=&quot;90dp&quot;
android:background=&quot;?android:attr/selectable&quot;
android:text=&quot;\?&quot;
android:textSize=&quot;30sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.015&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.029&quot; /&gt;
&lt;Button
android:id=&quot;@+id/two&quot;
android:layout_width=&quot;90dp&quot;
android:layout_height=&quot;90dp&quot;
android:background=&quot;?android:attr/selectable&quot;
android:text=&quot;\?&quot;
android:textSize=&quot;30sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.034&quot; /&gt;
&lt;Button
android:id=&quot;@+id/three&quot;
android:layout_width=&quot;91dp&quot;
android:layout_height=&quot;90dp&quot;
android:background=&quot;?android:attr/selectable&quot;
android:text=&quot;\?&quot;
android:textSize=&quot;30sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.984&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.034&quot; /&gt;
&lt;Button
android:id=&quot;@+id/four&quot;
android:layout_width=&quot;91dp&quot;
android:layout_height=&quot;92dp&quot;
android:background=&quot;?android:attr/selectable&quot;
android:text=&quot;\?&quot;
android:textSize=&quot;30sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.015&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.497&quot; /&gt;
&lt;Button
android:id=&quot;@+id/five&quot;
android:layout_width=&quot;91dp&quot;
android:layout_height=&quot;92dp&quot;
android:background=&quot;?android:attr/selectable&quot;
android:text=&quot;\?&quot;
android:textSize=&quot;30sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.497&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.497&quot; /&gt;
&lt;Button
android:id=&quot;@+id/six&quot;
android:layout_width=&quot;91dp&quot;
android:layout_height=&quot;92dp&quot;
android:background=&quot;?android:attr/selectable&quot;
android:text=&quot;\?&quot;
android:textSize=&quot;30sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.984&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.497&quot; /&gt;
&lt;Button
android:id=&quot;@+id/seven&quot;
android:layout_width=&quot;91dp&quot;
android:layout_height=&quot;92dp&quot;
android:background=&quot;?android:attr/selectable&quot;
android:text=&quot;\?&quot;
android:textSize=&quot;30sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.015&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.975&quot; /&gt;
&lt;Button
android:id=&quot;@+id/eight&quot;
android:layout_width=&quot;91dp&quot;
android:layout_height=&quot;92dp&quot;
android:background=&quot;?android:attr/selectable&quot;
android:text=&quot;\?&quot;
android:textSize=&quot;30sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.502&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.975&quot; /&gt;
&lt;Button
android:id=&quot;@+id/nine&quot;
android:layout_width=&quot;91dp&quot;
android:layout_height=&quot;92dp&quot;
android:background=&quot;?android:attr/selectable&quot;
android:text=&quot;\?&quot;
android:textSize=&quot;30sp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.984&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.975&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/player1&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Player 1 (X) :&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.177&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.098&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/name1&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:ems=&quot;10&quot;
android:hint=&quot;Name&quot;
android:inputType=&quot;textPersonName&quot;
android:textAlignment=&quot;center&quot;
android:textSize=&quot;16sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.513&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/player1&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.088&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/player2&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Player 2 (O) :&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.175&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.177&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/name2&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:ems=&quot;10&quot;
android:hint=&quot;Name&quot;
android:inputType=&quot;textPersonName&quot;
android:textAlignment=&quot;center&quot;
android:textSize=&quot;16sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.513&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/player1&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.17&quot; /&gt;
&lt;androidx.constraintlayout.widget.Guideline
android:id=&quot;@+id/guideline1&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;horizontal&quot;
app:layout_constraintGuide_begin=&quot;20dp&quot;
app:layout_constraintGuide_percent=&quot;0.3&quot; /&gt;
&lt;androidx.constraintlayout.widget.Guideline
android:id=&quot;@+id/guideline2&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;horizontal&quot;
app:layout_constraintGuide_begin=&quot;462dp&quot;
app:layout_constraintGuide_percent=&quot;0.7&quot; /&gt;
&lt;androidx.constraintlayout.widget.Guideline
android:id=&quot;@+id/guideline3&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
app:layout_constraintGuide_begin=&quot;20dp&quot;
app:layout_constraintGuide_percent=&quot;0.15&quot; /&gt;
&lt;androidx.constraintlayout.widget.Guideline
android:id=&quot;@+id/guideline4&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
app:layout_constraintGuide_begin=&quot;358dp&quot;
app:layout_constraintGuide_percent=&quot;0.85&quot; /&gt;
&lt;View
android:id=&quot;@+id/vertical1&quot;
android:layout_width=&quot;4dp&quot;
android:layout_height=&quot;282dp&quot;
android:background=&quot;?android:attr/listDivider&quot;
android:visibility=&quot;visible&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.333&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.5&quot;
tools:visibility=&quot;visible&quot; /&gt;
&lt;View
android:id=&quot;@+id/vertical2&quot;
android:layout_width=&quot;4dp&quot;
android:layout_height=&quot;282dp&quot;
android:background=&quot;?android:attr/listDivider&quot;
android:visibility=&quot;visible&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.666&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.5&quot;
tools:visibility=&quot;visible&quot; /&gt;
&lt;View
android:id=&quot;@+id/horizontal1&quot;
android:layout_width=&quot;282dp&quot;
android:layout_height=&quot;4dp&quot;
android:background=&quot;?android:attr/listDivider&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.333&quot; /&gt;
&lt;View
android:id=&quot;@+id/horizontal2&quot;
android:layout_width=&quot;282dp&quot;
android:layout_height=&quot;4dp&quot;
android:background=&quot;?android:attr/listDivider&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/guideline2&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/guideline4&quot;
app:layout_constraintHorizontal_bias=&quot;0.5&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/guideline3&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/guideline1&quot;
app:layout_constraintVertical_bias=&quot;0.666&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 0

你遇到的是膨胀异常,Android 无法展开 Button,因为它无法识别 ?attr/...。我之前也遇到过类似的问题。尝试在 XML 中移除这行。

android:background="?android:attr/selectable"

如果问题仍然存在,移除所有带有以下内容的行。

android:.......="?attr/...."
英文:

what you have is an inflation exception, Android is unable to inflate Button because it does not recognize an ?attr/... i had a similar problem a while back. Try removing this line in xml.

android:background=&quot;?android:attr/selectable&quot;

if problem persists remove all lines with.

android:.......=&quot;?attr/....&quot;

huangapple
  • 本文由 发表于 2020年5月5日 15:38:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61608059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定