[英]Android application with switch statement crashes on my phone
当我在我的 Android 手机上运行此代码时,它不断崩溃。
我的java代码如下
public class MainActivity extends Activity {
EditText edit = (EditText) findViewById(R.id.text);
Button button = (Button) findViewById(R.id.button);
String string = edit.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (string){
case "c":
MediaPlayer mp1 = MediaPlayer.create(getApplicationContext(), R.raw.sample1);
mp1.start();
break;
我的 XML 代码:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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="pembroke.com.example.algorhythmic.MainActivity">
<EditText
android:id="@+id/box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="308dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.781" />
代码崩溃了,但我不知道为什么。 我可能没有正确使用字符串。
代码中没有错误,但它仍然崩溃。 是不是因为 Android IDE 无法计算我的代码?
代码有3个问题:
为了执行 findViewById,我们在活动的 onCreate 方法中的 setContentView 之后执行此操作。 这是因为,在 setContentView 之前,所有的视图都是空的。 因此,我在活动的 onCreate 方法中的 setContentView(R.layout.activity_main) 之后移动了 EditText 和按钮的 findViewById 代码。
对于edittext,你使用的ID在java代码中是R.id.text,但在xml中是R.id.box。 因此,在执行 findViewById 时,我已将其更改为 Java 代码中的 R.id.box。
要找到 EditText 中存在的值,我们必须在 onClickListener 中的 EditText 中获取文本,因为 EditText 中的值随着用户进入 EditText 而不断变化。 因此,我们将值分配给 onClickListener 的 onClick 方法中的变量 string。
所以最终的代码是:
public class MainActivity extends Activity {
EditText edit;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.box);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = edit.getText().toString();
switch (string){
case "c":
MediaPlayer mp1 = MediaPlayer.create(getApplicationContext(), R.raw.sample1);
mp1.start();
break;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.