java - 当我运行简单的 android studio 应用程序时,它崩溃了。它没有错误

标签 java android android-studio crash

我为我和我的 friend 制作了一个简单的投注应用程序。 我按底部的一个按钮来设置出值(value)。之后,如果我赢了,我按我赢,否则我按我输。当我按下“我输了”或“我赢了”时, TextView 会显示赌注的值(value)。我没有任何错误,我不知道为什么当我打开应用程序时它会崩溃。 enter image description here 这是代码:

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int totalMoney= 0;
int betValue = 0;
String savedMoney;

    TextView textView = (TextView) findViewById(R.id.textView);

    public void clickP(View view){

        totalMoney= totalMoney - betValue;
    }
    public void clickC(View view){

        totalMoney= totalMoney + betValue;
    }
    public void click1(View view){

       betValue = 1;
    }
    public void click2(View view){

       betValue = 2;
    }
    public void click3(View view){

         betValue = 3;
    }
    public void click4(View view){

         betValue = 4;
    }
    public void click5(View view){

         betValue = 5;
    }

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


            savedMoney = textView.getText().toString();

            totalMoney = Integer.parseInt(savedbani);

            SharedPreferences sharedPreferences = this.getSharedPreferences("com.markusappcompany.baniipemasa", Context.MODE_PRIVATE);

            sharedPreferences.edit().putInt("Money", totalMoney).apply();

            sharedPreferences.getInt("Money", totalMoney);

            textView.setText(totalMoney);


    }
}

这是我在 Logcat 中得到的:

05-22 19:54:52.918 20536-20536/com.markusappcompany.baniipemasa E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.markusappcompany.baniipemasa, PID: 20536
                                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.markusappcompany.baniipemasa/com.markusappcompany.baniipemasa.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                      at android.os.Looper.loop(Looper.java:164)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                   Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
                                                                                      at android.content.res.Resources.getText(Resources.java:338)
                                                                                      at android.widget.TextView.setText(TextView.java:5494)
                                                                                      at com.markusappcompany.baniipemasa.MainActivity.onCreate(MainActivity.java:61)
                                                                                      at android.app.Activity.performCreate(Activity.java:6975)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                      at android.os.Looper.loop(Looper.java:164) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

这是我的 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="com.markusappcompany.baniipemasa.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="117dp"
        android:layout_marginEnd="1dp"
        android:layout_marginStart="80dp"
        android:layout_marginTop="228dp"
        android:text="I LOST"
        app:layout_constraintBottom_toTopOf="@+id/button3"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="117dp"
        android:layout_marginEnd="80dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="228dp"
        android:text="I WIN"
        app:layout_constraintBottom_toTopOf="@+id/button4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="44dp"
        android:layout_marginBottom="70dp"
        android:layout_marginStart="16dp"
        android:text="1 EUR"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button4"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="70dp"
        android:layout_marginTop="117dp"
        android:text="2 EUR"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button5"
        app:layout_constraintStart_toEndOf="@+id/button3"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="70dp"
        android:layout_marginTop="117dp"
        android:text="3 EUR"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button6"
        app:layout_constraintStart_toEndOf="@+id/button4"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="70dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="117dp"
        android:text="4 LEI"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button5"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginEnd="148dp"
        android:layout_marginStart="148dp"
        android:text="5 LEI"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="163dp"
        android:layout_marginEnd="221dp"
        android:layout_marginStart="190dp"
        android:layout_marginTop="16dp"
        android:text="0"
        android:textSize="36sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.487"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="25dp"
        android:text="Total money:"
        android:textSize="24sp"
        app:layout_constraintEnd_toStartOf="@+id/textView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

提前致谢!

最佳答案

首先,删除所有点击方法,您的 Activity 应如下所示:

public class MainActivity extends AppCompatActivity {
int totalMoney= 0;
int betValue = 0;
String savedMoney;
TextView textView 

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

        textView = (TextView) findViewById(R.id.textView);
        Button yourButton = findViewById(R.layout.yourButtonId)
        Button yourButton2 = findViewById(R.layout.yourButtonId2)
        // for all Buttons

}}

您还必须在 Activity_main 文件中为按钮指定一个 ID。之后,您必须为所有这些按钮实现一个 onClickListener,如下所示:

yourButton1.setOnClickListener(this);
// the same for every Button 

然后添加这一行:

public class MainActivity extends AppCompatActivity implements View.OnClickListener

最后监听者:

      @Override
      public void onClick(View v) {
          switch (v.getId()) {
                case R.id.yourButtonID:
                     // when button with this ID is clicked do your stuff here 
                case R.id.otherButtonID:
                     // when button with this ID is clicked do your stuff here 

}

}

这里的解释是,首先您找到按钮和 textView,然后设置一个 onclicklistener,以便当您单击一个按钮时,该按钮会调用 onClick 方法。 switch 语句被称为找出哪个按钮被调用。

关于java - 当我运行简单的 android studio 应用程序时,它崩溃了。它没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61955330/

相关文章:

java - 如何在我的图形用户界面中绘制准确的行数?

java - 如何检查对话框是否已关闭?

java - 无法解析以下类的父类(super class)型。请确保您在类路径 : 中具有所需的依赖项

Android Studio 未在设备上加载,本地路径不存在

java - 无法导入 Google Analytics

java - postgresql 与 spring data jpa 中的 SQLGrammarException 和 TransactionRequiredException

java - CodeModel 修饰符顺序

android - React Native assembleRelease无法使用axios获取数据

java - 将货币解析为双倍引发异常

android - 旧版 api 8 的文本按钮文本全部大写