应用程序在运行时崩溃

时间:2018-02-12 05:58:18

标签: java android textwatcher

这将是我的第二个问题,因为之前的帖子似乎已经停止吸引注意力,所以很抱歉垃圾邮件。无论如何,我上次发布是因为我的代码中出现了运行时崩溃以及各种错误,并且有人设法帮助我超越代码中的错误。但是,在修复代码之后我仍然会崩溃。包括我的项目代码和底部的日志cat文件。

快速概述它应该做什么:我正在尝试将一些编辑文本字段添加到TextView字段中,以便用户可以在编辑文本列中键入数字并在textview中查看结果。

package com.example.gideon.timemanagement;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.widget.EditText;
import android.text.TextWatcher;
import android.widget.TextView;



public abstract class Customize extends AppCompatActivity implements TextWatcher {


EditText a;
EditText b;
TextView ht;


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

    EditText a = (EditText) findViewById(R.id.exerciseHours);
    EditText b = (EditText) findViewById(R.id.sleepHours);
    TextView ht = (TextView) findViewById(R.id.healthTotal);
}

private TextWatcher Ht = new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {


        if (!a.getText().toString().equals("") && !b.getText().toString().equals("")) {
            ht.setText(String.valueOf(Integer.valueOf(a.getText().toString()) + Integer.valueOf(b.getText().toString())));

        }
    }
};
  

02-10 18:53:40.167 23713-23713 / com.example.gideon.timemanagement I / zygote:   不迟到-Xcheck:jni(已经开启)   02-10 18:53:40.174 23713-23713 / com.example.gideon.timemanagement W / zygote:   X86的意外CPU变体使用默认值:x86   02-10 18:53:40.393 23713-23713 / com.example.gideon.timemanagement   I / InstantRun:启动即时运行服务器:是主进程   02-10 18:53:40.532 23713-23731 / com.example.gideon.timemanagement   D / OpenGLRenderer:HWUI GL Pipeline   02-10 18:53:40.662 23713-23731 / com.example.gideon.timemanagement I / zygote:   安卓::硬件:: configstore :: V1_0 :: ISurfaceFlingerConfigs :: hasWideColor   检索到:0   02-10 18:53:40.662 23713-23731 / com.example.gideon.timemanagement   I / OpenGLRenderer:初始化的EGL,版本1.4   02-10 18:53:40.662 23713-23731 / com.example.gideon.timemanagement   D / OpenGLRenderer:交换行为1   02-10 18:53:40.662 23713-23731 / com.example.gideon.timemanagement   W / OpenGLRenderer:无法选择带有EGL_SWAP_BEHAVIOR_PRESERVED的配置,   没有...重试   02-10 18:53:40.662 23713-23731 / com.example.gideon.timemanagement   D / OpenGLRenderer:交换行为0   02-10 18:53:40.682 23713-23731 / com.example.gideon.timemanagement   D / EGL_emulation:eglCreateContext:0xb1eabb60:maj 3 min 0 rcv 3   02-10 18:53:40.726 23713-23731 / com.example.gideon.timemanagement   D / EGL_emulation:eglMakeCurrent:0xb1eabb60:ver 3 0(tinfo 0xb1eef120)   02-10 18:53:40.728 23713-23731 / com.example.gideon.timemanagement   E / eglCodecCommon:glUtilsParamSize:unknow param 0x00008cdf   02-10 18:53:40.728 23713-23731 / com.example.gideon.timemanagement   E / eglCodecCommon:glUtilsParamSize:unknow param 0x00008cdf   02-10 18:53:40.728 23713-23731 / com.example.gideon.timemanagement   E / eglCodecCommon:glUtilsParamSize:unknow param 0x00008824   02-10 18:53:40.728 23713-23731 / com.example.gideon.timemanagement   E / eglCodecCommon:glUtilsParamSize:unknow param 0x00008824   02-10 18:53:40.759 23713-23731 / com.example.gideon.timemanagement   D / EGL_emulation:eglMakeCurrent:0xb1eabb60:ver 3 0(tinfo 0xb1eef120)

1 个答案:

答案 0 :(得分:2)

在oncreate中,删除EditText和Textview定义,因为你已经将它们声明为类中的成员变量

在editText上添加TextChangedListener

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

 a = (EditText) findViewById(R.id.exerciseHours);
 b = (EditText) findViewById(R.id.sleepHours);
 ht = (TextView) findViewById(R.id.healthTotal);
 a.addTextChangedListener(this);
 b.addTextChangedListener(this);
 }

同时从班级定义中删除abstract