My android application was running smoothly before i install some Android Studio updates, and asked me to upgrade gradle etc after the update been installed.
Later on, the Android Studio asked me to update my Android Emulator (Emulator Nexus 5X API 24 Android v7.0), since i've made all these changes, my application has been unable to open when i click run. But remember it has no codding errors when i debug etc.
The error message from Android Monitor is:
08-13 14:07:30.735 3889-3889/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smartlegal.law.freeman.smartlegal, PID: 3889
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartlegal.law.freeman.smartlegal/com.smartlegal.law.freeman.smartlegal.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.smartlegal.law.freeman.smartlegal.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
08-13 14:07:30.744 3889-3896/? W/art: Suspending all threads took: 5.874ms
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
As the error states, you're trying to set an onClickListener on a Button that does not exist (returns null). To simplify, when you're defining your Button, you call findViewById to associate the Button in Java with an XML element, but the ID that you are passing is not valid - either you're trying to call the setOnClickListener before you call setContentView, or the ID you are passing to findViewById has a typo or simply does not exist in the XML layout file.
Some more information, such as the code to your MainActivity and the layout file, would be useful to determine the exact cause of your problem.
Yes you're right, i forgot to specify the ID for the editText and Button, that is why it wasnt possible to find them ID on XML. So i've set it up and reloaded the project and it is running smoothly now. Thanks guys for the support.
"As the error states, you're trying to set an onClickListener on a Button that does not exist (returns null). To simplify, when you're defining your Button, you call findViewById to associate the Button in Java with an XML element, but the ID that you are passing is not valid. by: Timothy Langer".
Related
I want to get the primary text on my clipboard and would like to know how to do that in newer android versions.
This code works perfectly when I tested it in an emulator running Android Pie, but the app crashes when I use it on my Phone running android 12.1.
please help me with this.
clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard.hasPrimaryClip())
clipItem = clipboard.getPrimaryClip().getItemAt(0);
clipText = clipItem.getText().toString();
Details.setText(clipItem.getText().toString());
Here's the Error I got from Logcat
2022-10-01 19:23:27.595 1349-5125 ClipboardService pid-1349 E Denying clipboard access to com.jery.tachisyrpc, the application is not in focus nor is it a system service for user 0
2022-10-01 19:23:27.595 8222-8222 AndroidRuntime com.jery.tachisyrpc D Shutting down VM
2022-10-01 19:23:27.596 8222-8222 AndroidRuntime com.jery.tachisyrpc E FATAL EXCEPTION: main
Process: com.jery.tachisyrpc, PID: 8222
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jery.tachisyrpc/com.jery.tachisyrpc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.content.ClipData$Item.getText()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4111) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4277) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8751)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.content.ClipData$Item.getText()' on a null object reference
at com.jery.tachisyrpc.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:8290)
at android.app.Activity.performCreate(Activity.java:8270)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4085)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4277)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8751)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed last year.
I am developing an android application which requires me the user to sign up and then set profile picture and user name. the registration is working properly and sending the data to Firebase but when it comes to uploading the profile photo, the application reading the gallery and picks the image but does not send the image to the Firebase neither does it set it in the image view. below is the error i get upon examination since the application terminates at that point.
E**/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chatme, PID: 23039
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=123, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/content://media/external/images/media/211575/ORIGINAL/NONE/image/jpeg/79264842 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F211575/ORIGINAL/NONE/image%2Fjpeg/79264842} }} to activity {com.example.chatme/com.example.chatme.profile}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4309)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4353)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1678)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:451)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
at com.example.chatme.profile.onActivityResult(profile.java:108)
at android.app.Activity.dispatchActivityResult(Activity.java:7311)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4305)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4353)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1678)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:451)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)**
The error is occurring because the image view in which you setting the image, is null. Did you initialise it? If it is not initialised, it will crash because java will take it as null.setImageUri() but you cannot do anything on null. So initialise the image view in order to get safe from the error
When the orientation of app change this error is occur kindly help me
W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.explore_kpk, PID: 24892
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.explore_kpk/com.example.explore_kpk.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5389)
at android.app.ActivityThread.access$1200(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
***Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference***
***at com.example.explore_kpk.MainActivity.onCreate(MainActivity.java:121)
at android.app.Activity.performCreate(Activity.java:6877)***
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5389)
at android.app.ActivityThread.access$1200(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1803)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
I/Process: Sending signal. PID: 24892 SIG: 9
This is code in which error pointing**
at com.example.explore_kpk.MainActivity.onCreate(MainActivity.java:121)
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recyler_view);
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this, recyclerList);
long seed = System.nanoTime();
Collections.shuffle(recyclerList, new Random(seed));
GridLayoutManager gridLayoutManager=new GridLayoutManager(this, mNoOfColumns);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setAdapter(recyclerViewAdapter);
Your exception is saying that you are trying to use setLayoutManager on null object reference. This means that your recylcerView is null at the moment. I don't see your whole code so I can't give you an instant solution. What I can say for you is that each time the screen orientation is changed your current activity will be destroyed and called again. So, onCreate will be called again and everything else like you just started new Activity. Sometimes issues happen here so there is few options on how to handle these events.
Retaining an object during a configuration change
What this means is for you to keep your data while screen rotation is happening. This can be done with Bundle but if you have a large set of data then it is better to use the ViewModel object. How to do this check here:
Retaining an object during a configuration change | Android Developers
ViewModel Guide
Helpful answer on SO
Handling the configuration change yourself
This other option comes with big CAUTION. Consider this as a last resort when you really need to avoid restarts because of a configuration change. What this does is preventing your activity from the restart. If you can't save your instance then I would recommend doing this but first, try to play with ViewModel and Bundles to fix this issue.
Handling the configuration change yourself | Documentation
How to do this?
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|screenLayout" />
add android:configChanges line to your AndroidManifest.xml inside your <activity/> tag.
First Activity of my Application starts a STICKY sevice that I'm using for creating notifications. Recently I've noticed a bug on Android O devices: when user starts an Application and then after a few seconds application was in background state the user gets this excpetion:
FATAL EXCEPTION: main
Process: com.callcatcher.callcatcher, PID: 27566
java.lang.RuntimeException: Unable to stop service callcatcher.call.com.callcatcher.NotificationService#e5b03d1: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Looper.quit()' on a null object reference
at android.app.ActivityThread.handleStopService(ActivityThread.java:3563)
at android.app.ActivityThread.-wrap26(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:6633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Looper.quit()' on a null object reference
at android.app.IntentService.onDestroy(IntentService.java:147)
at callcatcher.call.com.callcatcher.NotificationService.onDestroy(NotificationService.java:94)
at android.app.ActivityThread.handleStopService(ActivityThread.java:3541)
at android.app.ActivityThread.-wrap26(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:6633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Still not sure about the reason of exception, but I just switched from IntentService to Service and now it works fine.
Hello everyone i've got this error a nullPointerException error after i tried to integrate the android-page-transition and i couldn't find it's cause
this is the code where the error is shown PS: it's the same exact caude in the library i just integrated the code into mywork
/**
* Réglage Menu immersive title
*/
private void dealStatusBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int statusBarHeight = getStatusBarHeight();
ViewGroup.LayoutParams lp = positionView.getLayoutParams();
lp.height = statusBarHeight;
positionView.setLayoutParams(lp);
}
}
this is the error i get when i run
11-19 12:11:30.781 16089-16089/mobile.esprit.tn.mobileteam E/AndroidRuntime: FATAL EXCEPTION: main
Process: mobile.esprit.tn.mobileteam, PID: 16089
java.lang.RuntimeException: Unable to start activity ComponentInfo{mobile.esprit.tn.mobileteam/mobile.esprit.tn.mobileteam.Activities.Project.PrjectsCardViews}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at mobile.esprit.tn.mobileteam.Activities.Project.PrjectsCardViews.dealStatusBar(PrjectsCardViews.java:129)
at mobile.esprit.tn.mobileteam.Activities.Project.PrjectsCardViews.onCreate(PrjectsCardViews.java:54)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
the PrjectsCardViews.java:129 is the code i posted above
ok i found the problem it was a dump fault caused by the copy and paste , i dident refere the oncreate to the necessary activity layout
You might have copied the projet but you may forgotten to change
<com.stone.transition.DragLayout
android:id="#+id/drag_layout" when you merged the code in your project check this out it will work