注册 登录
编程论坛 Android开发

这个程序我在华为mate8里面点击按钮会出现闪退,求助,多谢

the_second 发布于 2016-05-18 13:19, 7093 次点击
package com.example.administrator.practice_clock;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btGetMoney;
    private Button pic;
    private Button btnLoseMoney;
    private TextView tvGetMoney;
    private EditText etGoalMoney;
    private ImageView p;
    private int[] images = {
        R.drawable.a,
        R.drawable.b,
        R.drawable.c,
        R.drawable.e,
        R.drawable.f
    };
    private int imageIndex = 0;
    private int money = 10;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btGetMoney = (Button) findViewById(R.id.btnGetMoney);
        tvGetMoney = (TextView) findViewById(R.id.tvGetMoney);
        btnLoseMoney = (Button) findViewById(R.id.btnLoseMoney);
        pic = (Button) findViewById(R.id.pic);
        etGoalMoney = (EditText)findViewById(R.id.etGoalMoney);
        p = (ImageView)findViewById(R.id.p);
        btGetMoney.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                String s = etGoalMoney.getText().toString().trim();
                int iMoney = Integer.parseInt(s);
                if(iMoney == money)
                {
                    Toast.makeText(MainActivity.this,"完成目标",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    money++;
                    tvGetMoney.setText("哈哈,我通过点击轻易赚了"+money+"元");
                }

            }
        });
        btnLoseMoney.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                if(money == 0)
                {
                    Toast.makeText(MainActivity.this,"我现在是穷光蛋了",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    money--;
                    tvGetMoney.setText("哈哈,我通过点击轻易赚了"+money+"元");
                }
            }
        });
        pic.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                imageIndex++;
                if(imageIndex>4)
                {
                    imageIndex = 0;
                }
                p.setImageResource(images[imageIndex]);
            }
        });
    }

}
7 回复
#2
the_second2016-05-18 13:20
上面的代码是我的java文件下MainActivity的
还有手机的系统是安卓6.0的,但是我电脑里面SDK包,安卓6.0的安装了的
#3
the_second2016-05-18 13:21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.
    xmlns:app="http://schemas.
    xmlns:tools="http://schemas.
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5eff00"
    tools:context="com.example.administrator.practice_clock.MainActivity">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etGoalMoney"
        android:hint="the you want to earn"
        android:layout_margin="10px"
        android:inputType="none"/>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10px"
        android:background="#0004ff"
        android:text="点我一次赚一元"
        android:id="@+id/btnGetMoney"/>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10px"
        android:background="#ff0000"
        android:text="点击一次损失一元"
        android:id="@+id/btnLoseMoney"/>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10px"
        android:background="#ff00ff"
        android:text="picture"
        android:id="@+id/pic"/>
    <TextView
        android:text="哈哈我通过点击轻易赚了..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#00d9ff"
        android:textSize="20dp"
        android:layout_margin="10px"
        android:id="@+id/tvGetMoney"/>
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/p"
        android:src="@drawable/a"/>
</LinearLayout>
#4
the_second2016-05-18 13:21
这是我的布局代码
求助,万分感谢
#5
aijiao06242016-07-21 14:41
去看看logcat的错误信息
#6
wangdeyou862017-04-27 07:25
大概是“s”没成功转换成整型数据造成的
#7
佐德2017-10-18 22:31
先搞个APP log 看一下
#8
ADust、2017-11-03 14:15
String s = etGoalMoney.getText().toString().trim();
 int iMoney = Integer.parseInt(s);

输入框输入为空的时候,点击闪退,这个s为" ",转型出错了,还有Edittext如果没有限制输入类型为number的话,转型也会出错
1