![](/img/trans.png)
[英]java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CardView.setVisibility(int)' on a null object reference
[英]Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
即使我将按钮实例化为我的活动,我的应用程序还是崩溃了。 我究竟做错了什么? 这是我的 xml 和 java 代码。 请帮助我我现在真的需要帮助。 我不知道该怎么办。 这是我的错误堆栈:
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)
这是我的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);
}
下面写的是我的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>
非常感谢您度过愉快的一天!
你的问题在这里:
expCalendar = (View) findViewById(R.id.expCalendar);
expCalendar.setVisibility(View.INVISIBLE);
findViewById
方法返回null
。 这意味着expCalendar
为null
,这意味着您正在尝试对不存在的对象调用setVisibility
方法。 直接的答案是在使用之前检查expCalendar
是否为空:
expCalendar = (View) findViewById(R.id.expCalendar);
if (expCalendar != null){
expCalendar.setVisibility(View.INVISIBLE);
}
但是,这并没有解决隐含的问题,即您是否关心expCalendar
根本没有被发现。 不幸的是,我对 Android 的了解不够,无法帮助您解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.