即使实例化了我的活动按钮,我的应用程序仍然崩溃。我究竟做错了什么?这是我的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>
非常感谢您的快乐!
答案 0 :(得分:0)
您的问题在这里:
@WebFilter(filterName = "Filter", urlPatterns = { "/filter/*" })
public class WebFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
.......
}
.......
}
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);
}
。不幸的是,我对Android的了解不足,无法为您提供帮助。