2016-06-13 5 views
0

このエラーについてはどこでもこのエラーについて検索しましたが、何も解決しません。 は、ここに私のアプリのAndroidManifest.xmlにある:nullオブジェクト参照で仮想メソッドを呼び出そうとしましたが、オブジェクトがnullではありません

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' 
} 

これは最初のMainActivity.javaクラスの一部である:MainActivityなしヌル例外がで投げた前に、私は2つの他の活動を持って

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 
} 

いずれかのビューを使用して宣言!!!!

クラス変数:onCreate()

EditText storyTag; 

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

私は、例えば、言うならば:

storyTag.setText("Hello"); 

それが発生します、私はこのように宣言しても 次の同じ例外があります。

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 

すべてのxmlレイアウトは同じID名で存在し、R.javaには同じIDが含まれています。

私はこれを解決する方法を知りません..助けてください!

+0

? –

+0

@MobileDeveloper '@Bind(R.id.drawer_layout)' < - バターナイフの注釈 –

+0

ああ。下の@Budiusからの提案を試しましたか? –

答えて

1

@Bindは自動的には行われません、あなたはそれを呼び出す必要があります:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ButterKnife.bind(this); <<< THIS LINE 
あなたは「引き出し」を初期化します
+0

私はそれを試しました。そして、私はButterKnifeの全てのものを乗り越えました。そして、findViewByIdがNULLを返すので、TextViewはsetTextしません! –

+0

これは、activity_mainレイアウトにそのビューが含まれていないことを意味します – Budius

関連する問題