-8

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

E/AndroidRuntime: FATAL EXCEPTION: 
Process: com.example.android.testapp, PID: 
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:
    at android.app.ActivityThread.access$900(ActivityThread.java:
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:
    at android.os.Handler.dispatchMessage(Handler.java:
    at android.os.Looper.loop(Looper.java:
    at android.app.ActivityThread.main(ActivityThread.java:
    at java.lang.reflect.Method.invoke(Native 
    at java.lang.reflect.Method.invoke(Method.java:
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object 
    at android.app.Activity.findViewById(Activity.java:
    at com.example.android.testapp.MainActivity.<init>(MainActivity.java:
    at java.lang.reflect.Constructor.newInstance(Native 
    at java.lang.Class.newInstance(Class.java:
    at android.app.Instrumentation.newActivity(Instrumentation.java:
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
    at android.app.ActivityThread.access$900(ActivityThread.java:
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:
    at android.os.Handler.dispatchMessage(Handler.java:
    at android.os.Looper.loop(Looper.java:
    at android.app.ActivityThread.main(ActivityThread.java:
    at java.lang.reflect.Method.invoke(Native 
    at java.lang.reflect.Method.invoke(Method.java:
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    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.

pirho
  • 11,565
  • 12
  • 43
  • 70
Adrià
  • 207
  • 1
  • 4
  • 12

2 Answers2

1

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. :)

KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26
0

EditText , number must be defined in oncreate method..

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText editText = (EditText) findViewById(R.id.editText);
    }
}
Rishav
  • 3,818
  • 1
  • 31
  • 49
Abhishek Shukla
  • 27
  • 1
  • 1
  • 10