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.
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())));
}
}
};
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!
Edit: 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)
First, when there is a crash, there is an exception stacktrace in logcat and it is very helpful in debugging problems. You should learn to use it. See: Unfortunately MyApp has stopped. How can I solve this?
Second, your activity is not yet doing really anything. One crash reason is that you're calling findViewById()
too early in the activity lifecycle, before the activity has a window and before you've set a layout with the views to be found. Move the findViewById()
calls to onCreate()
after setContentView()
.
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
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.
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;
in the class, and before onCreate()
:
EditText exerciseHoursEditText;
EditText sleepHoursEditText;
EditText mmHoursEditText;
EditText oaHoursEditText;
TextView healthTotalTextView;
and in 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 works in this way, perhaps you should read a little about android activity life cycle. it's pretty simple. check this link
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 ;)
来源:https://stackoverflow.com/questions/48718241/runtime-crash-upon-activity-selection