Android----java.lang.NullPointerException:---java.lang.NullPointerException:

2021-03-06 13:26:12.274 8544-8544/com.example.helloworld E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.helloworld, PID: 8544
java.lang.NullPointerException:: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3897)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4076)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
at com.example.helloworld.MainActivity.setListener(MainActivity.java:161)
at com.example.helloworld.MainActivity.onCreate(MainActivity.java:138)
at android.app.Activity.performCreate(Activity.java:8085)
at android.app.Activity.performCreate(Activity.java:8073)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3870)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4076) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:219) 
at android.app.ActivityThread.main(ActivityThread.java:8349) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055) 
2021-03-06 13:26:12.282 8544-8544/com.example.helloworld I/Process: Sending signal. PID: 8544 SIG: 9java

 

檢查一下xml文件定義的checkBox的id和在activity裏獲取的id是否一致android

問題出在運行時找不到對應的checkBoxapp

 


 

Process: com.example.helloworld, PID: 21319
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.helloworld/com.example.helloworld.MoreActivity}: java.lang.IllegalAccessException: java.lang.Class<com.example.helloworld.MoreActivity> is not accessible from java.lang.Class<android.app.AppComponentFactory>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3815)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4076)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
Caused by: java.lang.IllegalAccessException: java.lang.Class<com.example.helloworld.MoreActivity> is not accessible from java.lang.Class<android.app.AppComponentFactory>
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1264)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3800)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4076) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:219) 
at android.app.ActivityThread.main(ActivityThread.java:8349) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055) oop

 

這是在清單文件沒有註冊activity佈局

全部的activity類都必須是public 的 否則清單文件找不到,因此就沒法運行spa


 

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference;.net

這和問題一是同樣的,由於找不到對應的idorm

博主連接:https://blog.csdn.net/qq_42618969/article/details/81141895server

 

 


 

 

頁面佈局經常使用:

android:id 爲控件指定相應的ID
android:text 指定控件的文本,置儘可能使用strings.xml
android:grivity 指定控件的基本位置 ,好比舉重,居右,
android:padding 指定控件的內邊距,控件當中的內容
android:singleLine 若是設置爲真的話,則將控件的內容在同一行當中顯示xml

android:layout_above 將該空間的底部至於給定ID的空間之上
android:layout_below: 將該控件的頂部至於給定ID的控件之下
android:layout_toLeftOf: 將該控件的右邊緣和給定ID的控件的左 邊緣對其
android:layout_toRightOf 將該控件的左邊緣和給定的ID的控件的右邊緣對齊

android:layout_alignBaseLine 該控件的baseline和給定ID的控件的Baseline對齊
android:layout_alignBottom 將該控件的底部邊緣與給定ID控件的底部邊緣
android:layout_alignLeft 將該控件左邊緣與給定ID控件的左邊緣對齊
android:layout_alignRight 將該控件的右邊緣與給定ID控件的右邊緣對齊
android:layout_alignTop 將給定控件的頂部邊緣與給定ID控件的頂部對齊

android:alignParentBottom 若是該值爲true 則將該控件的底部和父控件的底部對齊
android:layout_alignParentLeft 若是該值爲true則將該控件的左邊與父控件的左邊對齊
android:layout_alignParentRight 若是該值爲true則將該控件的右邊與父控件的右邊對齊
android:layout_alignParentTop 將控件的頂部與父控件的頂部對齊

android:layout_centerHorizontal 若是值爲真,該控件將被至於水平方向的中央
android:layout_centerInParent 將被至於父控件水平方向和垂直都居中
android:layout_centerVertical 將被至於垂直方向的中央

第一類:屬性值爲true或false

android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相對於父元素徹底居中
android:layout_alignParentBottom 貼緊父元素的下邊緣
android:layout_alignParentLeft 貼緊父元素的左邊緣
android:layout_alignParentRight 貼緊父元素的右邊緣
android:layout_alignParentTop 貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing 若是對應的兄弟元素找不到的話就以父元素作參照物

第二類:屬性值必須爲id的引用名「@id/id-name」

android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左邊
android:layout_toRightOf 在某元素的右邊
android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊

第三類:屬性值爲具體的像素值,如30dip,40px

android:layout_marginBottom 離父控件底邊緣的距離
android:layout_marginLeft 離父控件左邊緣的距離
android:layout_marginRight 離父控件右邊緣的距離
android:layout_marginTop 離父控件上邊緣的距離

EditText的android:hint
設置EditText爲空時輸入框內的提示信息。

android:gravity 
android:gravity屬性是對該view 內容的限定.好比一個button 上面的text. 你能夠設置該text 在view的靠左,靠右等位置.以button爲例,android:gravity=」right」則button上面的文字靠右

android:layout_gravity
android:layout_gravity是用來設置該view相對與起父view 的位置.好比一個button 在linearlayout裏,你想把該button放在靠左、靠右等位置就能夠經過該屬性設置.以button爲例,

android:layout_gravity=」right」則button靠右

android:layout_alignParentRight
使當前控件的右端和父控件的右端對齊。這裏屬性值只能爲true或false,默認false。

padding和margin的區別:
因此padding是定義控件本身的內容和控件邊緣之間的距離,padding有填充填料的意思,而margin是在說控件的四個邊緣以外的距離,指的是和別的控件之間的距離,margin有邊緣,頁面留白的意思

原文連接:https://blog.csdn.net/qq_28296925/article/details/51115132

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息