我有一个带有登录页面的应用程序。
首先,我有我的 MainActivity
:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// here i launch LoginActivity
startActivityForResult(new Intent(this, LoginActivity.class), REQUEST_CODE);
emailText = findViewById(R.id.nav_text_email);
usernameText = findViewById(R.id.nav_text_username);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_CODE){
if(resultCode != Activity.RESULT_OK) {
finish();
}
// if login succesful, i attach to variable instance of
// logged user class.
// that part works correct, in object vars exist
loggedUser = loggedUser.getInstance();
// i want to attach strings after succesful login
emailText.setText(loggedUser.getEmail());
usernameText.setText(loggedUser.getUsername());
}
}
使用上述代码运行应用程序后,成功的 LoginActivity
应用程序因以下异常而崩溃:
2019-01-26 20:46:48.015 13071-13071/com.example.admin.keystroke_dynamics E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.keystroke_dynamics, PID: 13071
java.lang.RuntimeException: Unable to resume activity {com.example.admin.keystroke_dynamics/com.example.admin.keystroke_dynamics.Activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3586)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3626)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2876)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.admin.keystroke_dynamics.Activities.MainActivity.onResume(MainActivity.java:89)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1279)
at android.app.Activity.performResume(Activity.java:7022)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3561)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3626)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2876)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
当我从onActivityResult()中删除setText()
方法时,没有得到该异常。当我单击登录按钮时,该错误就会出现,并且应用程序崩溃。
在调试时,编译器甚至不符合setText()
我的问题是,在我上面编码的情况下,如何在textviews
中设置文本?
更新
即使我将构造函数中loggedUser
中的字符串弹出为空,将loggedUser = loggedUser.getInstance()
移至setText()
并将onCreat
移至onResume
,我仍然会得到该NPE。
编辑:
我添加了@Override
protected void onResume()
{
if(isLogged) {
emailText.setText(loggedUser.getEmail());
usernameText.setText(loggedUser.getUsername());
}
super.onResume();
}
方法:
onActivityResult()
并编辑了@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_CODE){
if(resultCode != Activity.RESULT_OK) {
finish();
}
isLogged = true; //var for if statement in onResume()
loggedUser = loggedUser.getInstance();
}
}
:
onResume()
设置TextViews
时,我在2019-01-26 21:21:57.752 18262-18262/com.example.admin.keystroke_dynamics E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.keystroke_dynamics, PID: 18262
java.lang.RuntimeException: Unable to resume activity {com.example.admin.keystroke_dynamics/com.example.admin.keystroke_dynamics.Activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3586)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3626)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1618)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.admin.keystroke_dynamics.Activities.MainActivity.onResume(MainActivity.java:90)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1279)
at android.app.Activity.performResume(Activity.java:7022)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3561)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3626)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1618)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
方法上仍然遇到相同的错误。
def highlight_max(s):
'''
highlight the maximum in a Series yellow.
'''
is_max = s == s.max()
return ['background-color: yellow' if v else '' for v in is_max]
s=df.style.apply(highlight_max)
答案 0 :(得分:1)
空视图通常是由于布局不包含具有在findViewById()中指定的ID的视图而导致的。因此,当调用onResume()时,findViewById()返回null,并且变量保持为null。在main_activity.xml中仔细检查您的视图ID。
如果使用更复杂的布局,则onCreate()中的完整内容视图也可能不可用。如果在onCreate()中发现视图为空,请尝试将findViewById()移至生命周期中的更高位置。
答案 1 :(得分:0)
尝试像这样重新排序代码
emailText = findViewById(R.id.nav_text_email);
usernameText = findViewById(R.id.nav_text_username);
startActivityForResult(new Intent(this, LoginActivity.class), REQUEST_CODE);
答案 2 :(得分:0)
尝试在访问变量之前对其进行初始化。
public class MainActivity extends AppCompatActivity {
TextView emailText;
TextView usernameText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// here i launch LoginActivity
startActivityForResult(new Intent(this, LoginActivity.class), REQUEST_CODE);
emailText = findViewById(R.id.nav_text_email);
usernameText = findViewById(R.id.nav_text_username);
}
可以加入评论
您需要从活动中访问NavigationView
更新
NavigationView navigationView = findViewById(R.id.nav_id)
View header = navigationView.getHeaderView(0)
TextView usernameText = header.findViewById(R.id.username_text_view_id);