活动选择时运行时崩溃 [英] Runtime Crash Upon Activity Selection

查看:147
本文介绍了活动选择时运行时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先要说的是我几乎没有任何Android体验,这是我在android中的第一个项目,我的老师不是很教,所以我为任何过度的无知道歉。

I would like to begin by saying I have little to no android experience, this is my first ever project within android and my teacher isn't quite teaching, so I apologize for any excessive ignorance.

在我进一步说明之前解释一下:我的应用程序的目标实际上是能够将您花费多少小时花在某些活动上,记录时间,然后在图表中显示它们。我目前正在开发的是创建文本字段,允许用户输入他们在每个活动中花费了多少小时,然后将其添加到总花费的小时数,我希望在不使用按钮的情况下完成此操作,这让我查找textWatcher教程。

To explain before I go any further: The goal of my app is essentially the ability to bank how many hours you have spent doing certain activities, record the times, and then display them in graphs. What I'm currently working on is creating text fields that will allow the user to type in how many hours they spent in each activity and then add them up to total hours spent, and I want this to be done without the usage of a button, which led me to look up textWatcher tutorials.

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

        }
    }
};

这是我目前为此特定活动提供的代码。在我实现任何这个之前,我可以进入屏幕并在布局上输入数据,但是,一旦我开始尝试输入代码,我就只有问题。这段代码最终允许我构建gradle,但现在每当我尝试进入活动时,它都会崩溃整个应用程序。

This is the code that I currently have for this particular activity. Before I implemented any of this I could get into the screen and enter data on the layout, however, once I began attempting to input the code I've had nothing but issues. This code finally allowed to me build the gradle, but now whenever I attempt to enter the activity it crashes the entire app.

任何帮助都表示赞赏,关闭主题提示也会有所帮助!

Any help is appreciated, and off topic tips would be helpful as well!

编辑:进行更改(更新了原始帖子中的代码以便匹配,因此也应该更改并且仍然有崩溃,所以我找到了logcat并将在此段后立即发布:

Made changes(Updated Code in the original post to match, so that should be changed as well) and still have a crash, so I found the logcat and will be posting it immediately after this paragraph:

02-10 18:53:40.167 23713-23713/com.example.gideon.timemanagement I/zygote: 
Not late-enabling -Xcheck:jni (already on)
02-10 18:53:40.174 23713-23713/com.example.gideon.timemanagement W/zygote: 
Unexpected CPU variant for X86 using defaults: x86
02-10 18:53:40.393 23713-23713/com.example.gideon.timemanagement 
I/InstantRun: starting instant run server: is main process
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: 
android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColor 
ay retrieved: 0
02-10 18:53:40.662 23713-23731/com.example.gideon.timemanagement 
I/OpenGLRenderer: Initialized EGL, version 1.4
02-10 18:53:40.662 23713-23731/com.example.gideon.timemanagement 
D/OpenGLRenderer: Swap behavior 1
02-10 18:53:40.662 23713-23731/com.example.gideon.timemanagement 
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, 
retrying without...
02-10 18:53:40.662 23713-23731/com.example.gideon.timemanagement 
D/OpenGLRenderer: Swap behavior 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)


推荐答案

首先,请确保始终将您的崩溃详细信息添加到logcat中,该logcat位于您的下半部分屏幕,如下图所示> 6:LOGCAT

First of all, make sure your always add your crash details printed in logcat, which is found in lower part of your screen, as you see in the image below > 6:LOGCAT

其次,当您想要为vari分配视图时能够,您应该确保构建视图层次结构。赋值 findViewById(R.id.someID)查找视图,但是在构建父视图之前调用它时,您将获得一个空指针执行。确保在 setContentView(R.layout.activity_customize) onCreate()方法之后始终调用此方法。

Second, when you want to assign a view to variable, you should make sure the view hierarchy is built. the assignment findViewById(R.id.someID) looks for a view, but when you call it before the parent view is built you will get a null pointer exeption. make sure you call this method always after setContentView(R.layout.activity_customize) of onCreate() method.

我想到的第三点是尝试命名变量有意义。 a,b和c不会在十天左右的时间内提醒你他们在做什么。将变量命名为描述性的。问问自己他们做了什么以及为什么需要他们,你对这些问题的回答将帮助你找到合适的名字。我选择了一些例子,因为它不是a,b,c!
因此,请将您的代码设置如下;

3rd point that comes to my mind is that try to name your variable meaningful. a, b and c are NOT going to remind you what are they doing in ten days or so. Name your variables descriptive. Ask yourself what they do and why do you need them, your answer to those question will help you find a right name for them. I chose some example for just not being a, b, c! So, make your code as below;

在类中,以及 onCreate()之前:

EditText exerciseHoursEditText;
EditText sleepHoursEditText;
EditText mmHoursEditText;
EditText oaHoursEditText;
TextView healthTotalTextView;

onCreate()

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customize);
exerciseHoursEditText = (EditText) findViewById(R.id.exerciseHours);
sleepHoursEditText = (EditText) findViewById(R.id.sleepHours);
mmHoursEditText = (EditText) findViewById(R.id.mmHours);
oaHoursEditText = (EditText) findViewById(R.id.oaHours);
healthTotalTextView = (TextView) findViewById(R.id.healthTotal);

Android以这种方式工作,也许你应该阅读一些关于android活动生命周期的内容。这很简单。查看此链接

Android works in this way, perhaps you should read a little about android activity life cycle. it's pretty simple. check this link

值得一提的第四点是,在使用可能导致 NULL 的方法时必须小心谨慎。您在 a.getText()。toString()中所做的事情可能会导致 NPE ,因此请务必按以下方式检查:

4th point that is worth mentioning, is that you must be cautious when using methods which might result in NULL. What you are doing in a.getText().toString() might cause in NPE, so always check as below:

   if(a.getText() != null) {
        a.getText().toString() .... 
    }

我希望你喜欢你正在做的事情;)

I hope you enjoy what you are doing ;)

这篇关于活动选择时运行时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆