1

I searched everywhere even here about this error but nothing solves it .. So I'm hoping my case is just as unique as it seems... Here is my app's AndroidManifest.xml:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "dell.example.com.myapp"
    minSdkVersion 15
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.android.support:design:23.1.0'
}

this is the first part of the MainActivity.java class:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


@Bind(R.id.tagEditText)
EditText storyTag;

@Bind(R.id.user_name)
TextView username;

@Bind(R.id.drawer_layout)
DrawerLayout drawer;

@Bind(R.id.nav_view)
NavigationView navigationView;

@Bind(R.id.storyTableLayout)
TableLayout storyTableLayout; 

@Bind(R.id.toolbar)
Toolbar toolbar;

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

    setContentView(R.layout.activity_main);

    setSupportActionBar(toolbar);
    clearTags(); // Clear all tags to load an updated copy from the database
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle); **// THIS IS WHERE IT CRASHES**
    toggle.syncState();
    username.setText(user.getName());
    navigationView.setNavigationItemSelectedListener(this);
    refreshStories(); // add previously saved story tags to GUI
}

I have 2 other activities before the MainActivity and no null exceptions were thrown by using any of the views declared!!!! even if I declare this way:

class variable:

EditText storyTag;

in onCreate():

storyTag = (EditText) findViewById(R.id.tagEditText) 

if I, for example, say:

storyTag.setText("Hello");

it will raise the same exception which is the following:

java.lang.RuntimeException: Unable to start activity ComponentInfo{dell.example.com.myapp/dell.example.com.myapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.addDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference
.
.
.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.addDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference

all xml layouts exist with the same id names and the R.java contains the same ids !!!!

I don't know how to solve this.. please Help!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Mido_007
  • 19
  • 2

1 Answers1

1

the @Bind doesn't happen automatically, you have to call it:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);  <<< THIS LINE
Budius
  • 39,391
  • 16
  • 102
  • 144
  • I did try that .. and I got ride of the whole ButterKnife thing .. and still TextView won't setText because the findViewById returns NULL !! – Mido_007 Jun 13 '16 at 23:47
  • So that means that the layout activity_main doesn't contain that view – Budius Jun 14 '16 at 06:14