Weird exception in calling a method in Android - java

I'm getting an exception with this trace:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.access$700(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.something.activity.SomeActivity.onCreate(SomeActivity.java:53)
at android.app.Activity.performCreate(Activity.java:5250)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
... 11 more
And in line 53 of the given activity, I'm just calling a method of SomeActivity, which is something like this:
50 #Override
51 protected void onCreate(Bundle savedInstanceState){
52 ...
53 populateItems();
54 ...
55 }
How can I get a NullPointerException on calling line of a method? Is my log reporter drunk or what? BTW I'm pretty sure about the line of the code and whether the version who got the exception and the given code is exactly the same.

You can't, a method is not a field. Most likely it seems like you compiled your code, ran it and then afterwards changed some code. Then the stacktracke will of cause lead you wrong.

put your full onCreate method
check these:
-call setContentView(layout) before populateItems()
-in findViewById(item) , item must be in it's container layout

Make sure the main_layout that you are using in setContentView(R.layout.main_layout) is correct layout that should include all the items/views you are using in populateItems(), then you can get each item in populateItems() by findViewById(R.id.item_id), otherwise you will get NullPointerException

Related

Android java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1

i've just recived this crash log but can't understand what its caused from. Basically my app is a text editor. I have a TextWatcher on the TextView but i'm not sure if its that the problem, since the logcat dosent contain any line of my app.
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at android.widget.TextView.sendOnTextChanged(TextView.java:7231)
at android.widget.TextView.handleTextChanged(TextView.java:7290)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8880)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:672)
at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:435)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:333)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
I'm not sure if this can help someone else, but to fix this bug I've done this:
I have an EditText, with a TextWatcher.
The error is raised by this set:
editText.setText("");
To solve this I had to first remove the listener, set the text to the empty string, and then re-add the listener.
Hope it can help someone else as well.
seems this happens if you edit the attached state of a text watcher in the text watcher itself. e.g., i see this happen if i remove the textwatcher in a watcher method itself.
to verify, move the contents of onTextChanged to afterTextChanged, and see if the new stacktrace is triggered from TextView$ChangeWatcher.afterTextChanged instead.
to work around it, move that work to a handler posted from the method, or similar.
So late, but for anyone can be useful:
Check if your EditText has the textAllCaps="true" attribute and remove it if needed.

Am I reading this stacktrace right?

Am i reading this stacktrace right:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at xxx.MainController.gruppeSortieren(MainController.java:68)
at xxx.MainActivity$1.onItemSelected(MainActivity.java:48)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:895)
at android.widget.AdapterView.access$200(AdapterView.java:50)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:863)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
My understanding:
I get a IndexOutOfBoundsException
this was caused while calling the get function of a ArrayList
this get was in the method xxx.MainController.gruppeSortieren
is this right? I need to find the bug and I'm not so familar with a stacktrace.
Your error is here: xxx.MainController.gruppeSortieren(MainController.java:68)
Line 68 in MainController.java
The problem is that the Arraylist you created only has one object in it.
To access first index use: .get(0)
For all collection indexing starts from 0 till length-1
You must have tried to access index 1 using: .get(1)
And hence IndexOutOfBoundException.

getResources returns null

We register a broadcast receiver to receive package installment or uninstallment event.
But some users report crash reports like this:
java.lang.RuntimeException: Unable to create application com.kc.security.MoSecurityApplication: java.lang.RuntimeException: getResources is null: dir - /data/app/com.cm.mg-1.apk, srcVal-1, srcVal-2
at android.app.LoadedApk.makeApplication(LoadedApk.java:495)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2249)
at android.app.ActivityThread.access$1600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: getResources is null: dir - /data/app/com.cm.mg-1.apk, srcVal-1, srcVal-2
at com.kc.security.b.ab.b(UpdateManager.java:69)
at com.kc.security.b.ab.a(UpdateManager.java:112)
at com.kc.security.MoSecurityApplication.onCreate(MoSecurityApplication.java:66)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
at android.app.LoadedApk.makeApplication(LoadedApk.java:492)
... 11 more
It is not easy to reappear this crash. We think a lot about this problem. We receive a
uninstall event ,then the Application context Class called it's onCreate method, we do
some initial works there , when we call getResouce(), it returns null . When look deep
inside the framework codes, we find that getReource locate resources file by sourceDir
String in ApplicationInfo, and the String is "/data/app/com.cm.mg-1.apk" > it's the original apk , and it is not exists anymore.
It maybe happens when user update our application. In some way "/data/app/com.cm.mg-1.apk" is allready deleted and replace by "/data/app/com.cm.mg-2.apk" any way, but the "sourceDir" String in ApplicationInfo is not update.
I want to know what android does when doing a updating.
Encountered a similar problem, check this out.
RuntimeException: Unable to instantiate application
I think this should answer this question, maybe because the Resources has been destroyed during this uninstall event.

How do I find this RuntimeException in Android (stack trace)?

I have this strack trace from the Developer Console on Google Play so I have no way to reproduce this exception cause I don't know what triggers it. The user also didn't leave any message, so...
java.lang.RuntimeException: Binary XML file line #17: You must supply a layout_height attribute.
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5319)
at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:5271)
at android.widget.AbsListView$LayoutParams.<init>(AbsListView.java:6398)
at android.widget.AbsListView.generateLayoutParams(AbsListView.java:6035)
at android.widget.AbsListView.generateLayoutParams(AbsListView.java:96)
at android.view.LayoutInflater.inflate(LayoutInflater.java:477)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.android.internal.view.menu.ListMenuPresenter$MenuAdapter.getView(ListMenuPresenter.java:253)
at android.widget.AbsListView.obtainView(AbsListView.java:2212)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1155)
at android.view.View.measure(View.java:12863)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2256)
at android.view.View.measure(View.java:12863)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2552)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
How do I pinpoint the problem?
This is Android's internal error, nothing in your code. You don't even see your app's code in stack trace, and your app works on many other devices.
I'm getting similar occasional crash reports, and also similar crashes on some Android versions (using ACRA). I've seen "You must supply a layout_height attribute" unexpected crash a few times.
If you use ACRA or similar, sometimes you just get reports for which your app is not responsible at all.
Message says - "You must supply a layout_height attribute."
So, double check that all your components have layout_height attribute.
I also had mention "menu" in this stack. It is generated by system. Double check that your all menus have valid items (at least not zero items) all the time.

can anyone give any insight into this issue on my android app - java.lang.NumberFormatException

Here is the stack traces from android crash report. I have 6 crash reports with this stack trace provided by google. Individuals comment on what happened but they do not tell any details on the screen what they were doing etc. can any one decipher this and possibly give me a general course of action to eliminate this issue. My app is a calculator using trig functions.
java.lang.NumberFormatException: unable to parse '2.625' as integer
at java.lang.Integer.parse(Integer.java:383)
at java.lang.Integer.parseInt(Integer.java:372)
at java.lang.Integer.parseInt(Integer.java:332)
at com.pipe.fittings.kevin.All_angle_pipe_all_angle$10.onClick(All_angle_pipe_all_angle.java:236)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9089)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)`enter code here`
In your code you are trying to parse double value using Integer.parseInt which is wrong. You need to use double temp = Double.parseDouble(s); if you want double.
If you want int, int temp2 = Double.intValue(temp);
use double i=Double.parseDouble(str); instead of Integer.parseInt.

Categories