Java 为什么我的Android应用程序只在我的手机上产生一个黑屏?
我一直在学习googledeveloper.android.com课程,但我遇到了一些小问题。也就是说,我已经完成了Android基础01.2的A部分,但是对代码做了一些修改,这样我将有两个按钮和屏幕用于计数,一个按钮用于显示文本。对于Android的东西,我是一个初学者,所以非常感谢您的帮助 以下是我的活动_main.xml:Java 为什么我的Android应用程序只在我的手机上产生一个黑屏?,java,android,Java,Android,我一直在学习googledeveloper.android.com课程,但我遇到了一些小问题。也就是说,我已经完成了Android基础01.2的A部分,但是对代码做了一些修改,这样我将有两个按钮和屏幕用于计数,一个按钮用于显示文本。对于Android的东西,我是一个初学者,所以非常感谢您的帮助 以下是我的活动_main.xml: <?xml version="1.0" encoding="utf-8"?> <androidx.constr
<?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>
问题是我已经完成了所有的步骤,但是Android开发站点上没有任何东西可以帮助我解决这个问题,IDE也没有显示任何错误 您使用
样式/主题.NoDisplay
主题的原因是什么?事实上,我以前从未见过它,但它的名字似乎是为了不显示。。。?!这是另一个SO问题的答案,我认为可能会解决这个问题(没有它,屏幕仍然是黑色的),但它没有改变什么,所以我就这样留下了它。我现在删除了这个,以避免混淆。
<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>
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));
}
}
}