我的 XML 中有一个SwipeView项,当我尝试从我的 java 类中访问它时,sometimes it returns null
. 在昨天的某个时候,虽然有一个断点来调试一切都工作正常,但这只是几次。我已经尝试清理我的项目。
自定义 ListView XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal">
<com.daimajia.swipe.SwipeLayout
android:id="@+id/swipeEvents" - this does not belong to activity_all_events
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
More Code...
</com.daimajia.swipe.SwipeLayout>
</LinearLayout>
Java 类- 这会调用包含 CustomAdapter 的 ListView 和自定义 xml 视图的 XML 布局
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_events);
SwipeLayout swipeLayout = (SwipeLayout)findViewById(R.id.swipeEvents); - this does not belong to activity_all_events.xml
//set show mode.
swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
More Code...
}
错误日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{cmario.com.xxxx/cmario.com.xxxx.events.AllEvents}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.daimajia.swipe.SwipeLayout.setShowMode(com.daimajia.swipe.SwipeLayout$ShowMode)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.daimajia.swipe.SwipeLayout.setShowMode(com.daimajia.swipe.SwipeLayout$ShowMode)' on a null object reference
at cmario.com.xxxx.events.AllEvents.onCreate(AllEvents.java:94)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
- 与我的构造函数有什么关系?
- 我试图访问 XML 属性的点(在 CustomAdapter 初始化之前)?在被调用后尝试过,
CustomAdapter
但输出仍然相同
提前感谢任何愿意帮助我提出建议的人