I have been slowing learning and building my first android app. I'm VERY new to java but have already done a couple of projects in C#, VB.NET (back in the day), Objective-C (have 6 apps in the store) and Fortran (waaaaaaaaaaaaaaaaaaaay back in the day ;)
So I just received from overseas a htc legend (I'm not in the US), which i bought in order to have a decent mid-level device for development (it's running non-rooted adnroid 2.1)
The application I have been developing is target level 4 (android 1.6). It uses a 5 Mb sqlite3 database with a .mp3 extension to avoid compression within the apk and proper copying from assets to system folder.
It all works fine on the emulator, and on the device I see that the file size of the app after copying the database matches exactly what I see on the emulator.
now, on my main activity with a list view and a spinner, I bind some data through two array adapters. when running on the device all does smoothly. but when trying to run on the device this part of the code:
public class mainAct extends Activity implements OnItemSelectedListener, TextWatcher, OnItemClickListener
{
/** members */
//private EditText searchtext;
private ListView designations;
private ArrayAdapter<String> adapterShapes;
private ArrayAdapter<String> adapterTypes;
private Spinner types;
.
.
.
public void onCreate(Bundle savedInstanceState)
{
.
.
.
// DESIGNATIONS
//
adapterShapes = new ArrayAdapter<String>(this,R.layout.list_item,shapes); // custom TextView Adapter
designations=(ListView)findViewById(R.id.designations);
Log.e("MAIN.ACCT", "ok to 172");
designations.setAdapter(adapterShapes);
Log.e("MAIN.ACCT", "ok to 174");
designations.setOnItemClickListener(this);
// TYPES
//
adapterTypes=new ArrayAdapter<String>(this,R.layout.spinner_item,DT.get().typesInLibrary);
types=(Spinner)findViewById(R.id.types);
types.setAdapter(adapterTypes);
types.setOnItemSelectedListener(this);
.
.
.
}
}
Both designations.setAdapter(adapterShapes);
& types.setAdapter(adapterTypes);
give me a Null Pointer exception.
I'm using eclipse under mac, the LogCat window throws:
06-25 18:41:37.842:
ERROR/AndroidRuntime(9523): Uncaught
handler: thread main exiting due to
uncaught exception
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523):
java.lang.RuntimeException: Unable to
start activity
ComponentInfo{com.davidhomes.steel/com.davidhomes.steel.mainAct}:
java.lang.NullPointerException
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
android.app.ActivityThread.access$2200(ActivityThread.java:126)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
android.os.Handler.dispatchMessage(Handler.java:99)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
android.os.Looper.loop(Looper.java:123)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
android.app.ActivityThread.main(ActivityThread.java:4595)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
java.lang.reflect.Method.invokeNative(Native
Method)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
java.lang.reflect.Method.invoke(Method.java:521)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
dalvik.system.NativeStart.main(Native
Method)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): Caused by:
java.lang.NullPointerException
-------------------------------------------------------------- 06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
com.davidhomes.steel.mainAct.onCreate(mainAct.java:183)
--------------------------------------------------------------
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
06-25 18:41:37.891:
ERROR/AndroidRuntime(9523): ... 11
more
06-25 18:46:38.252:
ERROR/ActivityManager(99): fail to set
top app changed!
Line 183 is the first setAdapter call (designations.setAdapter(adapterShapes);), when I comment it out the second setAdapter is the one breaking code
I'm a little lost here, the adapters show the proper number of items on the log window when running from the simulator and the device.
I admit to being a noob to both java and android, so any help is highly appreciated.
regards
david
Well, as you've probably figured out yourself, some variable is null. Unfortunately, there is no obvious source of the NullPointerException in your code.
Therefore, you should first try to identify what variable is null, and hence causing the exception.
For example, findViewById returns null if it cannot find the view, so you may want to double check your ListView and Spinner are being initialised properly.
Of course, the problem may be with your ArrayAdapters, so you should also check them, but from your question it sounds as if you've already done that.
Once you (and we) know exactly where the NullPointerException is occurring, it will be easier to give more specific advice
Oops! it was my fault all along, somehow I had TWO layouts (one for different resolutions), the one being used on my device did not have the proper ListView and Spinner ID. That alternative layout was collapse into a folder and I just forgot about it (I had paused development of the app for about 2 month until I actually got a device to test on).
the one working fine was installing on the simulator but not on the device and vice-versa, weird as the good one matches the resolution of my device (I'm pretty sure I'm also missing something there but that's not important right now)
Still, thanks Chris for pointing me to looking at the Null return value on findViewById. Being new to android I was lost as to were to begin
Best regards
david
I know this is a long closed subject. However I just came across the same problem and this post did not answer it.
What i did, was very silly. I forgot to add a reference to the xml. ie. setContentView(R.layout.settings);
it actually looks like the original post might have also forgotten this so i thought I would just post it.
Cheers.
I had the same problem. I put Log.d messages in printing out every object. It turned out that my ListView object was "null". The reason for this was I had the wrong XML activity listed in my SetContentView. I think the easiest way to debug this problem is keep putting in Log.d messages until you find the object/variable that is "null".
Related
My app always shutdown in random time interval (mostly from few minutes to something like 20 mins). The "App stopped working" messagebox appears. Biggest problem is, I don't do anything in app (like interacting with it), and it just random shutdown by itself. Here's logcat print:
07-15 20:34:16.595 27361-27363/test.game D/dalvikvm: GC_CONCURRENT freed 141
9K, 44% free 13902K/24391K, paused 4ms+9ms, total 66ms
07-15 20:34:28.997 27361-27363/test.game D/dalvikvm: GC_CONCURRENT freed 1416K, 43% free 13903K/24391K, paused 3ms+7ms, total 56ms
07-15 20:34:29.418 27361-27361/test.game D/AndroidRuntime: Shutting down VM
07-15 20:34:29.418 27361-27361/test.game W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x412b52a0)
07-15 20:34:29.498 27361-27361/test.game E/AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: onMeasure() did not set the measured dimension by calling setMeasuredDimension()
at android.view.View.measure(View.java:15293)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
at android.view.View.measure(View.java:15288)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
at android.view.View.measure(View.java:15288)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15288)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15288)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
at android.view.View.measure(View.java:15288)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1974)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1217)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1390)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1113)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
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)
One would say, it's because I didn't write setMeasuredDimension() when overriding method onMeasure(). Also, docs say you have to write this line to your onMeasure, otherwise the IllegalStateException will be thrown (see it here).
But I don't overriding onMeasure anywhere in my whole project, so problem is not here (at least not on my side in this case).
Few things to note:
this is full logcat log, and any of these lines ("at android. ...") doesn't link to my code, all goes to source - that means all of these links are gray
this exception always happens only on my S3 Mini (android 4.1.2), but never happened on my old Galaxy Ace S5830i (android 2.3.6) (and yes, I tested it alot - over 20hours on old phone and app still running!).
never happened in emulator
there are no "view resizing" operations running in background, which could cause this exception
I would like to post some code, but I have absolutely no idea where this exception could be thrown.
If someone can help, I'd be really, really grateful. I'm lost at this point...
Since there is not code, this is just a random guess. You can start by looking out for activities in which you've added fragments. It might be that the activity containing a fragment has been destroyed on the device due to inactivity for a long time(and memory requirements by the system, which I think is the reason that you are finding the bug on a device and not on the other). So the case is that the fragment might be referring to a view which might not be attached to the window now. (You can also look for views that use fragment adapters like viewPager, etc if you have used them).
I'm trying to learn and add some basic Facebook functionality to my Android App (an address book). What I'm eventually going to do is import a friends profile picture. I've currently only copy/pasted what I need from the "FriendPickerSample" example project, which is just a button that brings up a friend picker fragment, and, if I remember correctly, it places the names you picked into a list. I've altered it to come up when an imageview is clicked, and it'll just Toast the single name that is chosen. When I try to bring up the friend picker, I get a null pointer exception, and I don't understand why. Here's the log:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.JDE.RAB/com.JDE.RAB.PickFriendsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.facebook.widget.PickerFragment.onCreateView(PickerFragment.java:152)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
at com.JDE.RAB.PickFriendsActivity.onStart(PickFriendsActivity.java:112)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3781)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
... 11 more
This is the code in my onclick, which is supposed to open the PickFriendsActivity:
if (Session.getActiveSession() == null || Session.getActiveSession().isClosed())
{
Session.openActiveSession(AddNewTabLayoutActivity.this, true, null);
}//endif
FriendPickerApplication application = (FriendPickerApplication) GetMyApplication();
application.setSelectedUsers(null);
Intent intent = new Intent(AddNewTabLayoutActivity.this, PickFriendsActivity.class);
// Note: The following line is optional, as multi-select behavior is the default for
// FriendPickerFragment. It is here to demonstrate how parameters could be passed to the
// friend picker if single-select functionality was desired, or if a different user ID was
// desired (for instance, to see friends of a friend).
//multi-select is OFF
PickFriendsActivity.populateParameters(intent, null, false, true);
Debugger.SendNotification(getApplicationContext(), "Alert", "Alert", "Starting Activity For Result", 3009);
startActivityForResult(intent, 2);
PickFriendsActivity, line 112 is super.onStart(); Should I post the entirety of that activities source code?
Thanks for any help!
You should copy res/layout/pick_friends_activity.xml to your own project too.
I wish I had more information about this error but I just don't. I have a tool called BugSense(Now it is Splunk MINT) that sends crashes to a remote server and this is all I'm getting:
java.lang.NullPointerException
at android.view.HardwareRenderer$GlRenderer.checkCurrent(HardwareRenderer.java:960)
at android.view.HardwareRenderer$Gl20Renderer.destroyLayers(HardwareRenderer.java:1148)
at android.view.ViewRootImpl.destroyHardwareResources(ViewRootImpl.java:576)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:973)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2448)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4482)
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:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
The interesting thing is that all of the error instances came from:
BLU DASH 4.0
They are all rooted
They all had Mobile Net turned off
They all had at least 12 running apps
So... what do you think?
has anyone had a NPE in the HardwareRenderer class?
Could it be a 'root' thing (i.e. an issue with the device being rooted)?
Could it be a 'hardware' thing (i.e. an issue with the BLUE DASH 4.0)
The problem is that the code is using an null object.
MyClass class;
class.doStuff(); // Will throw NullPointerException as class is null
MyClass newClass = new MyClass();
newClass.doStuff(); // Will work as newClass is pointing to an object.
As I do not have the offending code, I cannot help further. I would suggest you include the code at (HardwareRenderer.java:960), and the call route from the stack, as this is what is causing the exception.
I have an app on Google play. I got a few error reports from the developer page and It's quite hard to figure out the problem.
This is what I have:
java.lang.NullPointerException
at com.seb.example.free.MainActivity$ApplyFilter.onPostExecute(MainActivity.java:828)
at com.seb.example.free.MainActivity$ApplyFilter.onPostExecute(v.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:4987)
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:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
As you can see I have an asynctask innerclass and the error seems to be in on postexecute.
I have checked my project in eclipse and, as the stacktrace says, the error is on line 828:
(iV.getVisibility() == ImageView.INVISIBLE)
My imageView, iV, is declared as private in MainActivity and I initiate it in oncreate.
I can't reproduce the problem on the three of my phones as i have tested on, so it's quite hard to understand what's causing it.
Anyone who has any ideas? Thanks!
I think that imageview object is becomes null in some where in your code..check whether it initialized or not..and if initialized check where it is used in your project.
It might have been derefferenced in some other place, initialize in onresume and see whether this persists or not.
please check have you initialized iv
try:
if(iv != null)
(iV.getVisibility() == View.INVISIBLE)
else
Log.e("CHECK","iv Null");
if you have not initialized iv then do that first.
I have a crash report from a user which i couldn't recreate same error myself on my test device.
Here is the relevant code :
for(File f : sessionDirectory.listFiles()){
Date lastModDate = new Date(f.lastModified());
/*Line 53*/ Session ss = new Session(lastModDate.toLocaleString() , f.getName()) ;
sessionArrayList.add(ss ) ;
}
and here are the stack traces :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain/com.mydomain.myActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mydomain.myActivity.onCreate(myActivity.java:53)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
I have seen things like that when concerting dates to/from string representations in java (not android) and for a long time didn't have a clue why. I don't remember the exact details, but it had something to do with a conversion method not being thread safe, which was weird, because (if I remember correctly) it happened in a call to a static jdk method.
Try if it still happens when you pull lastModDate.toLocaleString() into a synchronized method and using an instance of DateFormat.
There was already a bug file against this in the jdk when I looked for it, but it was rejected.
(Perhaps toLocaleString uses a DateFormat attached to the current locale which is shared between all threads, and calls a non-threadsafe method.)
However this is just a guess, but since the symptoms are similarly weird, just give it a try.
Stack trace says:
Caused by:
java.lang.NullPointerException at
com.mydomain.myActivity.onCreate(myActivity.java:53)
What is line 53?
sessionDirectory can be null and sessionDirectory.listFiles() can return null - also got reminded of that by a user crash report :-)