[英]Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
My app is crashing even though I instantiated my buttons to my activity.即使我将按钮实例化为我的活动,我的应用程序还是崩溃了。 What am I doing wrong?
我究竟做错了什么? Here is my xml and java code.
这是我的 xml 和 java 代码。 Please help me I really need help right now.
请帮助我我现在真的需要帮助。 I don't know what to do.
我不知道该怎么办。 Here is my error stack:
这是我的错误堆栈:
Process: com.example.dith.finalproject3, PID: 9301
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dith.finalproject3/com.example.dith.finalproject3.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
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:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
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.view.View.setVisibility(int)' on a null object reference
at com.example.dith.finalproject3.MainActivity.expansions(MainActivity.java:193)
at com.example.dith.finalproject3.MainActivity.onCreate(MainActivity.java:63)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
Here is my java code:这是我的Java代码:
public void expansions()
{
btnCalendar = (ImageButton) findViewById(R.id.btnCalendar);
btnCalendarHide = (Button) findViewById(R.id.btnCalendarHide);
expCalendar = (View) findViewById(R.id.expCalendar);
expCalendar.setVisibility(View.INVISIBLE);
}
Written below is my XML file:下面写的是我的XML文件:
<LinearLayout
android:id="@+id/expCalendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txtCalendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_large"
android:text="Calendar Placeholder" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<Button
android:id="@+id/btnCalendarHide"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="HIDE" />
</LinearLayout>
Thank you very much enjoy your day!非常感谢您度过愉快的一天!
Your problem is here:你的问题在这里:
expCalendar = (View) findViewById(R.id.expCalendar);
expCalendar.setVisibility(View.INVISIBLE);
The findViewById
method is returning null
. findViewById
方法返回null
。 This means expCalendar
is null
, which means you're trying to call the setVisibility
method on an object that doesn't exist.这意味着
expCalendar
为null
,这意味着您正在尝试对不存在的对象调用setVisibility
方法。 The straightforward answer is to check expCalendar
for nullity before you use it:直接的答案是在使用之前检查
expCalendar
是否为空:
expCalendar = (View) findViewById(R.id.expCalendar);
if (expCalendar != null){
expCalendar.setVisibility(View.INVISIBLE);
}
However, that doesn't address the implied question as to whether you care about expCalendar
not being found at all.但是,这并没有解决隐含的问题,即您是否关心
expCalendar
根本没有被发现。 Unfortunately I don't know enough about Android to help you with that one.不幸的是,我对 Android 的了解不够,无法帮助您解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.