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. 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:
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:
<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!