当我运行它时,简单的android studio应用就会崩溃。它没有错误

时间:2020-05-22 12:42:57

标签: java android android-studio crash

我为我和我的朋友制作了一个简单的投注应用程序。 我从底部按下一个按钮来设置竞标价格。然后如果我赢了,我按我赢,否则我按我输。当我按我输了或我赢了文本视图时,获得下注的价值。我没有任何错误,我不知道为什么打开应用程序时会崩溃。 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>

提前谢谢!

3 个答案:

答案 0 :(得分:0)

尝试在onCreate方法中找到Buttons和textViews。 还尝试使用OnClickListener处理按钮,如下所示:

Button yourButtton = findViewById(R.id.yourButttonID);
yourButtton.setOnClickListener(this);

这对于所有按钮。 然后:

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

    }
}

答案 1 :(得分:0)

将Textview的findTextViewById放在OnCreateView内。

答案 2 :(得分:0)

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

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。切换语句称为“查找哪个按钮被调用”。