0

IT is not duplicate, this same APP without ANDROIDANNOTATIONS works fine.

My app that worked has stopped working as a result to modify project with AndroidAnnotations Framework. In my opinion everything looks good, there are no warning in AndroidStudio until run on phone, App got error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.annotationstest/com.example.annotationstest.MainActivity_}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

What i should fix or add to code ?

MainActivity

 @EActivity(R.layout.activity_main)
    public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

        private static final String SHARED_PREFS_NAME = "abc";
        public static final int REQUEST_CODE = 100;
        private int number;

        @ViewById(R.id.tv2)
        TextView tv2;

        @ViewById(R.id.tv3)
        TextView tv3;

        @ViewById(R.id.switch1)
        Switch switch1;


        @Click
        void b1() {
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            startActivityForResult(intent, REQUEST_CODE);
        }


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            SharedPreferences preferences = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE);
            number = preferences.getInt("NUMBER", 0);
            tv2.setText(""+number);
            switch1.setOnCheckedChangeListener(this);

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                if (requestCode == REQUEST_CODE) {
                    number = data.getExtras().getInt("number");
                    tv2.setText("" + number);
                    saveNumberToSharedPrefs(number);

                }
            }
        }

        private void saveNumberToSharedPrefs(int num) {
            SharedPreferences preferences = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE); 
            preferences.edit().putInt("NUMBER", num).apply(); 
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            switch (buttonView.getId()) {
                case R.id.switch1:
                    if (isChecked == true) {
                        tv3.setVisibility(View.VISIBLE);
                    } else {
                        tv3.setVisibility(View.INVISIBLE);
                    }
                    break;
            }

        }
    }
philips
  • 111
  • 1
  • 8
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – bradimus Jun 30 '17 at 13:46
  • no, it is not duplicate, this same app without AndroidAnnotiations works fine, without this error – philips Jun 30 '17 at 13:47

1 Answers1

2

According to the documentation views may have not yet been injected in the onCreate() method (see here):

When onCreate() is called, @ViewById fields are not set yet. Therefore, you can use @AfterViews on methods to write code that depends on views.

That is you should move your code from onCreate() to a new method and annotate it with @AfterViews

dpr
  • 10,591
  • 3
  • 41
  • 71