[英]Attempt to invoke virtual method on a null object reference but the object is not null
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: 我什至在这里到处都搜索了此错误,但没有解决方法..因此,我希望我的情况像看起来一样独特...这是我的应用程序的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: 这是MainActivity.java类的第一部分:
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!!!! 我在MainActivity之前还有其他2个活动,并且使用声明的任何视图都不会引发null异常! even if I declare this way: 即使我这样声明:
class variable: 类变量:
EditText storyTag;
in onCreate()
: 在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 !!!! 所有xml布局都具有相同的ID名称,并且R.java包含相同的ID!
I don't know how to solve this.. please Help! 我不知道该如何解决..请帮助!
the @Bind
doesn't happen automatically, you have to call it: @Bind
不会自动发生,您必须调用它:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this); <<< THIS LINE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.