Adrià Adrià - 3 years ago 227
Android Question

Runtime Error Android Studio

I get this output when running my app on my J5. Don't know what 2 do plz help me.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.testapp, PID: 22452
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.testapp/com.example.android.testapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2555)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2773)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1434)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5930)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2172)
at com.example.android.testapp.MainActivity.<init>(MainActivity.java:10)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1690)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2545)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2773) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1434) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5930) 
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:1405) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 


I've looked at blogs, youtube videos and here, in StackOverflow, but I don't know how to solve this.

Thanks!

Answer Source

Please post your MainActivity file as well. But looking at the error what I can assume is this

You are instantiating some view. Let's say a TextView.

And this is ur code.

public class MainActivity extends Activity {
TextView something = (TextView) findViewById(R.id.myTextView);
onCreate (Bundle savedinstancestate){
super.onCreate(.....
setContentView(R.layout.layout_main);
.......

And if this assumption was correct

Simply doing this

public class MainActivity extends Activity {
TextView something;
onCreate (Bundle savedinstancestate){
super.onCreate(.....
setContentView(R.layout.layout_main);
something = (TextView) findViewById(R.id.myTextView);
.........

Should work. Hope it helps you. :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download