简体   繁体   中英

Null pointer exception when adding TextView

I have a ScrollView, and I'm trying to add TextViews to the LinearLayout in it but i keep getting an error.

TextView l = new TextView(StopWatch.this);
l.setText("Didn't work");
l.setTextSize(20);
llLocations.addView(l);

I was trying to use this code to see if the ArrayList i was using was empty, but it wasnt that. It says the error is caused by a NullPointerException at...

llLocations.addView(l);

I have other code that does the same thing, and i tried comparing them but there wasn't a difference so i'm confused. How do i fix this?

07-10 21:14:42.687: E/AndroidRuntime(5241): FATAL EXCEPTION: main
07-10 21:14:42.687: E/AndroidRuntime(5241): java.lang.IllegalStateException: Could not execute method of the activity
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.view.View$1.onClick(View.java:3599)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.view.View.performClick(View.java:4204)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.view.View$PerformClick.run(View.java:17355)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.os.Handler.handleCallback(Handler.java:725)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.os.Looper.loop(Looper.java:137)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at dalvik.system.NativeStart.main(Native Method)
07-10 21:14:42.687: E/AndroidRuntime(5241): Caused by: java.lang.reflect.InvocationTargetException
07-10 21:14:42.687: E/AndroidRuntime(5241):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.view.View$1.onClick(View.java:3594)
07-10 21:14:42.687: E/AndroidRuntime(5241):     ... 11 more
07-10 21:14:42.687: E/AndroidRuntime(5241): Caused by: java.lang.NullPointerException
07-10 21:14:42.687: E/AndroidRuntime(5241):     at com.TBJsoft.runprogress.StopWatch.done(StopWatch.java:506)
07-10 21:14:42.687: E/AndroidRuntime(5241):     ... 14 more

StopWath.java:506 is the

llLocations.addView(l);

line.

I'm trying to do this in an alert dialog that has a custom view with the scroll view in it. Do you think that could be causing the problem?

llLocations is null. Read up on findViewById . The end.

llLocations is null , so I guess you did something like that:

LinearLayout llLocations = (LinearLayout)findViewById(R.id.<here_the_id_of_the_linearleayout_in_your_xml_layout>);

If you did this, and the id is the correct one, and you setContentView of your activity with this layout which contains the linearlayout, then it should work.

normally you have to bind the TextView to an id! Like that:

example = findViewById(R.id.yourid);

For your own knowledge, R is dynamically generated by android out of your values-file(IDs are mostly assigned in the layout-files)

If you have a variable which is an instance of a class, you need to initialize it. If you do not initialize it, it will have a null pointer.

llocations was not initialized properly before you try to access its method. But you cannot access the method of a null pointer, right? It would be like shaking hands with nobody. So, this is an exceptional behavior and that is why the NullPointerException is thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM