[英]App crashes while changing the orientation from landscape to portrait
ERROR **2020-03-01 17:36:58.959 6589-6589/com.studenthelper.bscit E/AndroidRuntime: FATAL EXCEPTION: main Process: com.studenthelper.bscit, PID: 6589 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.studenthelper.bscit/com.studenthelper.bscit.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.performLaunchActivity(ActivityThread.java:2914) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049) at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4785) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4694) at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.ex错误 **2020-03-01 17:36:58.959 6589-6589/com.studenthelper.bscit E/AndroidRuntime:致命异常:主进程:com.studenthelper.bscit,PID:6589 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.studenthelper.bscit/com.studenthelper.bscit.MainActivity}:java.lang.NullPointerException:尝试在空对象上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”参考 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049) at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4785) at android.app .ActivityThread.handleRelaunchActivity(ActivityThread.java:4694) at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app。 servertransaction.TransactionExecutor.ex ecute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 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.studenthelper.bscit.MainActivity.onCreate(MainActivity.java:21) at android.app.Activity.performCreate(Activity.java:7140) at android.app.Activity.performCreate(Activity.java:7131) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894) at android.app.ActivityT
ecute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper .java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6692) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit .java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 引起:java.lang.NullPointerException:尝试调用虚拟方法 'void android.widget.TextView.setText(java. lang.CharSequence)' 在 com.studenthelper.bscit.MainActivity.onCreate(MainActivity.java:21) at android.app.Activity.performCreate(Activity.java:7140) 在 android.app.Activity.performCreate 的空对象引用(Activity.java:7131) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894) at android.app.ActivityT hread.handleLaunchActivity(ActivityThread.java:3049) at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4785) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4694) at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) **
hread.handleLaunchActivity(ActivityThread.java:3049) at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4785) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4694) at android.app.servertransaction.ActivityRelaunchItem.execute (ActivityRelaunchItem.java:69) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H。 handleMessage(ActivityThread.java:1809) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java) :6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858) **
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
TextView textView=findViewById(R.id.logotext);
int unicode=0x1F4A1;
String emoji=getEmoji(unicode);
String Text="Bsc"+emoji+"T";
textView.setText(Text);
}
public String getEmoji(int uni)
{
return new String(Character.toChars(uni));
}
As per your crash logs:根据您的崩溃日志:
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 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.studenthelper.bscit.MainActivity.onCreate(MainActivity.java:20)
It means your textView这意味着你的textView
TextView textView=findViewById(R.id.logotext);
Is not found from the layout R.layout.activity_main due to which when you try to textView.setText(Text);
未从布局R.layout.activity_main 中找到,因此当您尝试
textView.setText(Text);
it's actually null and exception occurs.它实际上是null并且发生异常。
So, I'll suggest to所以,我会建议
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
before the setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView textView=findViewById(R.id.logotext);
int unicode=0x1F4A1;
String emoji=getEmoji(unicode);
String Text="Bsc"+emoji+"T";
textView.setText(Text);
}
To summarise the problem: You have defined two layouts, which one of them does not have a TextView
.总结一下问题:您定义了两个布局,其中一个没有
TextView
。 From your code obviously the crash is caused by calling .setText()
on a null object.从您的代码中,显然崩溃是由在空对象上调用
.setText()
引起的。
The best way is always to add back the missing TextView
back to your another layout, so all your layouts contains the same set of IDs.最好的方法是始终将缺少的
TextView
添加回另一个布局,因此所有布局都包含相同的 ID 集。 This can avoid bugs caused by findViewById()
.这可以避免由
findViewById()
引起的错误。 In the layout which you do not need the TextView
, you can simply set its android:visibility="gone"
to hide it.在不需要
TextView
的布局中,您可以简单地设置它的android:visibility="gone"
来隐藏它。
Alternatively back to your code, if you know the TextView
is not always there, you can simply check if your textView
variable is null
or not, before calling .setText()
.或者回到您的代码,如果您知道
TextView
并不总是存在,您可以在调用.setText()
之前简单地检查您的textView
变量是否为null
。 This can also avoid the NullPointerException
.这也可以避免
NullPointerException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.