I have a simple activity in android where I’m doing something like:
givepassw = (EditText) findViewById(R.id.txt_password);
login = (Button) findViewById(R.id.login_button);
sony = (ImageView) findViewById(R.id.sony1);
orange = (ImageView) findViewById(R.id.orange1);
sony.setImageResource(R.drawable.btn_sony);
orange.setImageResource(R.drawable.btn_orange);
So…simple things.The problem is that sometimes I get NullPointerException at one of these lines but most of the time this works perfect.
What could be the problem ???Cause everything is defined and simple to understand….
LOGCAT:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Xperiaproject/com.Xperiaproject.LoginPage}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3701)
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:862)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)
08-30 12:18:49.605: ERROR/AndroidRuntime(1623): Caused by: java.lang.NullPointerException
at com.Xperiaproject.LoginPage.onCreate(LoginPage.java:78)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
At this line: login = (Button) findViewById(R.id.login_button); but I also get at this line: sony.setImageResource(R.drawable.btn_sony);
IMPORTANT: Is not about setting properly the layout....cause 90% of the time this code works perfect.But sometimes the same code is not working!!!
First : i prefer that you change the method
setImageResource(int res); by the method :Second : Clean and rebuild your project.