This question already has an answer here:
这个问题在这里已有答案:
- What is a NullPointerException, and how do I fix it? 12 answers
什么是NullPointerException,我该如何解决? 12个答案
When I run my app on Motorola Nexus 6 , I get NullPointerException.Problem is with one TextView and setting text on same.I saw some solutions online, but none of them worked for me.I guess problem is with JVM on different smartphones, or with lifecycle of my fragment which is nested inside activity.
当我在摩托罗拉Nexus 6上运行我的应用程序时,我得到NullPointerException.Problem有一个TextView并且在同一个文本上设置文本。我在网上看到了一些解决方案,但它们都没有为我工作。我想问题是在不同的智能手机上使用JVM,或者我的片段的生命周期嵌套在活动中。
Everything is working fine on smaller devices.
在小型设备上一切正常。
Log bellow:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com..data.models.entity.Email.getUsername()' on a null object reference
at com..presenter.view.fragment.ForgotPasswordFragmentComplete.onStart(ForgotPasswordFragmentComplete.java:84)
Here is my Fragment:
这是我的片段:
@Bind(R.id.button_resend_email)
AppCompatButton resendEmail;
@Bind(R.id.resend_user_email)AutoResizeTextView resendEmailTxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DaggerForgotPasswordCompletedComponent.builder()
.applicationComponent(
((AndroidApplication) getActivity().getApplication()).getApplicationComponent())
.forgotPasswordCompletedModule(new ForgotPasswordCompletedModule())
.build()
.inject(this);
}
@Override
public void onStart() {
super.onStart();
realm = getRealm();
RealmQuery<Email> query1 = realm.where(Email.class);
Email result1 = query1.findFirst();
resendEmailTxt = (AutoResizeTextView) getView().findViewById(R.id.resend_user_email) ;
this.resendEmailTxt.setText(result1.getUsername());
}
@Override
public void onDestroy() {
super.onDestroy();
realm.close();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_forgot_password_fragment_complete, container, false);
}
@Override
protected int getFragmentLayoutId() {
return R.layout.fragment_forgot_password_fragment_complete;
}
2 个解决方案
#1
1
If you get nullPointerException
it probably means your result1
variable has not been assigned a value. That implies that your query1.findFirst()
may be returning nothing at all.
如果得到nullPointerException,则可能意味着您的result1变量尚未赋值。这意味着你的query1.findFirst()可能根本没有返回任何内容。
#2
0
Try to move your code from onStart()
to onResume()
.
尝试将代码从onStart()移动到onResume()。
#1
1
If you get nullPointerException
it probably means your result1
variable has not been assigned a value. That implies that your query1.findFirst()
may be returning nothing at all.
如果得到nullPointerException,则可能意味着您的result1变量尚未赋值。这意味着你的query1.findFirst()可能根本没有返回任何内容。
#2
0
Try to move your code from onStart()
to onResume()
.
尝试将代码从onStart()移动到onResume()。