提问者:小点点

在模拟器中运行应用程序后,带有按钮的主activity屏幕稍后显示


我是安卓应用程序的新手,当我在模拟器中运行一个带有按钮的应用程序时,按钮会在应用程序完成后显示出来,所以onclick等不起作用。

它显示一个空白的白色屏幕

同样的代码来自youtube视频,并且运行良好。 试图调试,但没有取得太大进展。

粘贴代码:

package com.example.weatherapiapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button btn_cityID,btn_getWeatherByID,btn_getWeatherByName;
    EditText et_dataInput;
    ListView lv_weatherReport;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


       //assign values  to each control on the layout
       btn_cityID = findViewById(R.id.btn_getCityID);
       btn_getWeatherByID = findViewById(R.id.btn_getWeatherByCityID);
       btn_getWeatherByName = findViewById(R.id.btn_getWeatherByCityName);

       et_dataInput = findViewById(R.id.et_dataInput);
       lv_weatherReport = findViewById(R.id.lv_weatherReports);

       //click listeners for each button

       btn_cityID.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Toast.makeText(MainActivity.this,"You clicked me 1.", Toast.LENGTH_SHORT);
           }
       });

       btn_getWeatherByID.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Toast.makeText(MainActivity.this,"You clicked me 2.",Toast.LENGTH_SHORT);
           }
       });

       btn_getWeatherByName.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Toast.makeText(MainActivity.this,"You clicked me 3.",Toast.LENGTH_SHORT);
           }
       });

    }
}

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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_getCityID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get City ID"
        app:layout_constraintEnd_toStartOf="@id/btn_getWeatherByCityID"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_getWeatherByCityID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Weather by ID"
        app:layout_constraintEnd_toStartOf="@id/btn_getWeatherByCityName"
        app:layout_constraintStart_toEndOf="@+id/btn_getCityID"
        app:layout_constraintTop_toTopOf="@id/btn_getCityID" />

    <Button
        android:id="@+id/btn_getWeatherByCityName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Weather by Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btn_getWeatherByCityID"
        app:layout_constraintTop_toTopOf="@id/btn_getWeatherByCityID" />

    <EditText
        android:id="@+id/et_dataInput"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="City Name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_getCityID" />

    <ListView
        android:id="@+id/lv_weatherReports"
        android:layout_width="409dp"
        android:layout_height="628dp"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_dataInput" />
</androidx.constraintlayout.widget.ConstraintLayout>

你能帮忙告诉我问题出在哪里吗?


共1个答案

匿名用户

您忘记了吐司中的.show()

应该是

 Toast.makeText(getApplicationContext(),"You clicked me 1.", Toast.LENGTH_SHORT).show();