我正在尝试自己学习 android 开发,因为我试图使用按钮和文本视图制作一个简单的文本更改应用程序,但是当我使用 USB 在我自己的手机上运行它时,它开始显示,不幸的是,该应用程序已停止。如果你不介意帮助我了解这个错误即将到来以及如何解决它。
MainActivity.java
package com.nanb.wishes;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView txt1, txt2;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt1 = (TextView) findViewById(R.id.txt1);
final TextView txt2 = (TextView) findViewById(R.id.txt2);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txt1.setText("Happy birthday to a special person who is bringing so much joy with her smile. I am thankful for every moment we spend together, and I wish your happiness never ends.");
txt2.setText("");
}
});
btn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
txt1.setText(" May this day be as sunny as your smile, and as beautiful as you are. You shine every day, but on this day you will shine the brightest. Happy Birthday");
txt2.setText("");
return true;
}
});
}
}
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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/birthday"
tools:context=".MainActivity">
holdtom
相关分类