首页 > 解决方案 > 为什么我的 Android 应用程序只在我的手机上产生黑屏?

问题描述

我一直在学习 Google developer.android.com 课程,但遇到了一些问题。也就是说,我已经完成了 Android 基础知识 01.2 第 A 部分,但对代码进行了一些修改,以便我有两个用于计数的按钮和屏幕,以及一个用于显示文本的按钮。我是 Android 方面的初学者,因此不胜感激。

这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#1A237E"
    tools:context="com.example.android.hellotoast.MainActivity">

    <TextView
        android:id="@+id/POTATOCOUNT"
        android:layout_width="434dp"
        android:layout_height="149dp"
        android:layout_marginStart="160dp"
        android:layout_marginTop="120dp"
        android:layout_marginEnd="160dp"
        android:layout_marginBottom="33dp"
        android:background="#1B9288"
        android:text="@string/potato_count"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="120sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/ready"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/GRAVYCOUNT"
        android:layout_width="434dp"
        android:layout_height="149dp"
        android:layout_marginStart="160dp"
        android:layout_marginEnd="160dp"
        android:layout_marginBottom="92dp"
        android:background="#1B9288"
        android:text="@string/gravy_count"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="120sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/gravyUp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/potatoUp"
        android:layout_width="436dp"
        android:layout_height="49dp"
        android:layout_marginStart="160dp"
        android:layout_marginTop="42dp"
        android:layout_marginEnd="160dp"
        android:text="@string/add_potatoes"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:onClick="addPotato"/>

    <Button
        android:id="@+id/gravyUp"
        android:layout_width="436dp"
        android:layout_height="49dp"
        android:layout_marginStart="160dp"
        android:layout_marginEnd="160dp"
        android:layout_marginBottom="44dp"
        android:text="@string/add_gravy"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        android:onClick="addGravy"/>

    <Button
        android:id="@+id/ready"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_marginStart="160dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="160dp"
        android:text="@string/ready_to_go"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/POTATOCOUNT"
        android:onClick="showDinner"/>
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的strings.xml:

<resources>
    <string name="app_name">My Application</string>
    <string name="potato_count">0</string>
    <string name="gravy_count">0</string>
    <string name="add_potatoes">POTATO</string>
    <string name="add_gravy">Gravy</string>
    <string name="ready_to_go">ready</string>
    <string name="ready_message">BON APPETIT</string>
</resources>

这是我的Java代码:

package com.example.android.hellotoast;


import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;

public class MainActivity extends AppCompatActivity {

    private int mcount1=0;
    private int mcount2=0;
    private TextView showmcount1;
    private TextView showmcount2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showmcount1 = (TextView) findViewById(R.id.GRAVYCOUNT);
        showmcount2 = (TextView) findViewById(R.id.POTATOCOUNT);
    }

    public void showDinner(View view) {
        Toast dinner = Toast.makeText(this, R.string.ready_message, Toast.LENGTH_SHORT);

        dinner.show();
    }

    public void addGravy(View view) {
        mcount1++;
        if(showmcount1!=null){
            showmcount1.setText(Integer.toString(mcount1));
        }
    }

    public void addPotato(View view) {
        mcount2++;
        if(showmcount2!=null){
            showmcount2.setText(Integer.toString(mcount2));
        }
    }
}

问题是我已经完成了所有步骤,但是 Android 开发站点上没有任何东西可以帮助我解决这个问题,而且 IDE 也没有显示任何错误!

标签: javaandroid

解决方案


推荐阅读