简体   繁体   English

简单的 android 工作室应用程序在我运行时崩溃。 它没有错误

[英]Simple android studio app is crashing when i run it. it have no errors

i made a simple betting app for me and my friend.我为我和我的朋友制作了一个简单的投注应用程序。 I press one button from the bottom to set the value of the bid.我从底部按下一个按钮来设置出价的价值。 and after that if i win i press i win and otherwise i press i lost.之后,如果我赢了,我按我赢,否则我按我输了。 when i press i lost or i win the text view get the value of the bet.当我按下我输了或我赢了文本视图时,得到下注的价值。 I dont have any errors, i dont know why is it crashing when i open the app.我没有任何错误,我不知道为什么当我打开应用程序时它会崩溃。 在此处输入图像描述 here is the code:这是代码:

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);


    }
}

This is what i get in Logcat:这就是我在 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) 

This is my xml:这是我的 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>

thanks in advance!提前致谢!

Try to find the Buttons and textViews in the onCreate method.尝试在 onCreate 方法中找到 Buttons 和 textViews。 Also try to handle the Buttons with OnClickListener like this:还尝试使用 OnClickListener 处理按钮,如下所示:

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

And this for all Buttons.这适用于所有按钮。 then:然后:

  @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 

    }
}

Place your Textview's findTextViewById inside OnCreateView.将 Textview 的 findTextViewById 放在 OnCreateView 中。

First of all you remove all click methods your activity should look like this:首先,您删除所有点击方法,您的活动应如下所示:

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

}}

You also must give the button an id in the activity_main file.您还必须在 activity_main 文件中为按钮指定一个 ID。 After that you have to implement an onClickListener for all thes buttons like this:之后,您必须为所有这些按钮实现一个 onClickListener,如下所示:

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

then you add this line:然后你添加这一行:

public class MainActivity extends AppCompatActivity implements View.OnClickListener

at the end the listener:最后听者:

      @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 

}

} }

Here the explanation first you find the buttons and the textView after that you set an onclicklistener so when you click one button the button call the method onClick.这里的解释首先你找到了按钮和 textView,然后你设置了一个 onclicklistener,所以当你点击一个按钮时,按钮调用方法 onClick。 The switch statment is called an finds out which button is called.开关语句被称为找出哪个按钮被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM