Android - NullPointerEx。由 Android 库 Class 中的 getPackageName() 引起
Android - NullPointerEx. caused by getPackageName() from Android Library Class
我正在做一个项目,我必须在 Android 库中从普通 'app' 模块中的 MainActivity 开始一个 Activity。
主要Activity 看起来像这样:
public class MainActivity extends ActionBarActivity implements BackendResponse {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Here I start an AsyncTask
public void tellJoke(View view) {
BackendConnector backendConnector = new BackendConnector();
backendConnector.execute();
}
// And here is the Callback
@Override
public void response(String joke) {
/*
Down here is the problematic line (PresenterActivity is imported)
*/
Intent jokeIntent = new Intent(this, PresenterActivity.class); // <--- line 18
if (joke == null) joke = getString(R.string.emergency_joke);
jokeIntent.putExtra(getString(R.string.intent_key), joke);
startActivity(jokeIntent);
}
在 'app' 模块的 build.gradle 文件中设置了依赖项
android {
// compileSdkVersion etc
}
dependency {
compile project(':jokepresenter') // <---
}
并且在整个项目的settings.gradle文件中包含了库
include ':app', ':jokepresenter'
Logcat表示如下:
FATAL EXCEPTION: main
Process: com.udacity.gradle.builditbigger, PID: 2723
java.lang.NullPointerException:
Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
// ...
at com.udacity.gradle.builditbigger.MainActivity.response(MainActivity.java:18)
at com.udacity.gradle.builditbigger.BackendConnector.onPostExecute(BackendConnector.java:53)
at com.udacity.gradle.builditbigger.BackendConnector.onPostExecute(BackendConnector.java:17)
// ...
好的,我知道了。
问题出在回调方法上,或者更具体地说是在 AsyncTask 内部。
@Override
protected void onPostExecute(String joke) {
response = new MainActivity(); // <--- ¯\_(ツ)_/¯
response.response(joke);
}
因为我创建了一个新的 MainActivity,所以 no Context 由 this:
表示
Intent jokeIntent = new Intent(this, PresenterActivity.class);
这导致 NullPointerException 在 ContextWrapper 中,当他试图调用
return mBase.getPackageName();
mBase 被设置为 Activity 上下文,这当然是 null
我正在做一个项目,我必须在 Android 库中从普通 'app' 模块中的 MainActivity 开始一个 Activity。
主要Activity 看起来像这样:
public class MainActivity extends ActionBarActivity implements BackendResponse {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Here I start an AsyncTask
public void tellJoke(View view) {
BackendConnector backendConnector = new BackendConnector();
backendConnector.execute();
}
// And here is the Callback
@Override
public void response(String joke) {
/*
Down here is the problematic line (PresenterActivity is imported)
*/
Intent jokeIntent = new Intent(this, PresenterActivity.class); // <--- line 18
if (joke == null) joke = getString(R.string.emergency_joke);
jokeIntent.putExtra(getString(R.string.intent_key), joke);
startActivity(jokeIntent);
}
在 'app' 模块的 build.gradle 文件中设置了依赖项
android {
// compileSdkVersion etc
}
dependency {
compile project(':jokepresenter') // <---
}
并且在整个项目的settings.gradle文件中包含了库
include ':app', ':jokepresenter'
Logcat表示如下:
FATAL EXCEPTION: main
Process: com.udacity.gradle.builditbigger, PID: 2723
java.lang.NullPointerException:
Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
// ...
at com.udacity.gradle.builditbigger.MainActivity.response(MainActivity.java:18)
at com.udacity.gradle.builditbigger.BackendConnector.onPostExecute(BackendConnector.java:53)
at com.udacity.gradle.builditbigger.BackendConnector.onPostExecute(BackendConnector.java:17)
// ...
好的,我知道了。
问题出在回调方法上,或者更具体地说是在 AsyncTask 内部。
@Override
protected void onPostExecute(String joke) {
response = new MainActivity(); // <--- ¯\_(ツ)_/¯
response.response(joke);
}
因为我创建了一个新的 MainActivity,所以 no Context 由 this:
表示Intent jokeIntent = new Intent(this, PresenterActivity.class);
这导致 NullPointerException 在 ContextWrapper 中,当他试图调用
return mBase.getPackageName();
mBase 被设置为 Activity 上下文,这当然是 null