-2

Hey guys so when I hit this particular button it is supposed to take me to the another layout. However everytime i hit it, the entire app crashes. Here is the onCreate method for that page im trying to get to:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bench_data_entry);
        //opens the database
        openDB();
        //Initializes and declares the sensors, and accelerometer
        mInitialized = false;
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
      //  tv.setText("Aasfdaf");

  }

Here is the onClick for the button I am pressing:

 public void onClick_Bench(View v){
        //Intent to create a new activity
        Intent intent = new Intent(MainMenuActivity.this, BenchDataEntry.class);
        //Goes to the Data Entry Screen
        startActivity(intent);
    }

and here is the error log I am getting when I press the button:

08-08 17:30:30.785  31113-31113/com.example.jacob.repcapture E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.jacob.repcapture, PID: 31113
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jacob.repcapture/com.example.jacob.repcapture.BenchDataEntry}: 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:2327)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5422)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     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:2096)
            at com.example.jacob.repcapture.BenchDataEntry.<init>(BenchDataEntry.java:49)
            at java.lang.Class.newInstance(Native Method)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5422)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
jvangore31
  • 31
  • 5
  • `findViewById` must be inside the `onCreate` method. Not outside of it. Please refer to line 49 of BenchDataEntry.java – OneCricketeer Aug 08 '16 at 22:43
  • Actually correction, findViewById must be called on the UI Thread simply by doing _act.runOnUiThread _act being you Activity variable or this. Also if you end up running and initializing ui on two separate threads you must use thread concurrency and make sure that you are using Locks and Conditions to sync the functional code running and the UI code. I second the answer below make sure to do a rebuild. – JQluv Aug 09 '16 at 16:30

1 Answers1

0

I think error not in these code blocks logcat about android.view.View android.view.Window.findViewById(int)' on a null object reference

First - do clean project under build-menu, and restart the emulator. After that the exception went away in my situation. Second - check FindviewById items You need to initialize them in the onCreate too. 

Ozan Manav
  • 429
  • 4
  • 18