当我尝试获取getApplicationContext()
时,我收到了java.lang.NullPointerException。我从用户那里收到此错误,但无法在手机上重现此错误。
可能是因为我在加载完整的片段/活动之前调用了getActivity().getApplicationContext()
吗?如果这是问题,片段中的哪个函数可以最早调用getActivity().getApplicationContext()
?
我应该将getActivity().getApplicationContext()
置于片段生命周期中最后一个onResume()
变为活跃状态吗?
基本上,我需要在片段加载后的某个时候调用它。
我的片段中的onCreateView :
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ctxt = getActivity().getApplicationContext();
v = inflater.inflate(R.layout.fragment_test,
container, false);
//MODE CODE HERE THAT I AM NOT SHOWING
//THIS LINE BELOW IS GIVING THE NULL POINTER EXCEPTION - Am I calling getActivity().getApplicationContext() too early in the Fragment's lifecycle?
_adapter_lv_blockedApps2 = new Adapter_BlockedApps(
getActivity().getApplicationContext(), holder);
return v;
}
以下是我从用户那里得到的堆栈跟踪'电话。
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.ContextWrapper.getApplicationContext()' on a null object reference
at com.mavdev.focusoutfacebook.fragments.scheduledblocks.Fragment_scheduled_newdetail$populateLV_blockedAppsFromSystem.doInBackground(Fragment_scheduled_newdetail.java:2597)
at com.mavdev.focusoutfacebook.fragments.scheduledblocks.Fragment_scheduled_newdetail$populateLV_blockedAppsFromSystem.doInBackground(Fragment_scheduled_newdetail.java:2486)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
... 4 more
完整的功能: 私有类populateLV_AppsFromSystem扩展 AsyncTask {
@Override
protected Void doInBackground(Void... params) {
if (!isCancelled()) {
ArrayList<Datamodel> holder = new ArrayList<Datamodel>();
Map<String, Drawable> map_Apps = new HashMap<String, Drawable>();
List<String> Apps_List = Arrays.asList(TextUtils.split(
ToDisplay.getApps(), ","));
if (Apps_List != null) {
for (String s : Apps_List) {
String s_appName = "";
try {
s_appName = mAppInfo.getAppNamefromPackage(s,
getActivity().getApplicationContext());
} catch (Exception e) {
s_appName = "Unknown";
}
Drawable d = mAppInfo.getIconDrawablefromAppName(
s_appName, getActivity());
map_Apps.put(s_appName, d);
}
}
/****************************************************
* GETTING THE ADAPTER
****************************************************/
for (Map.Entry<String, Drawable> entry : map_Apps
.entrySet()) {
if (isCancelled()) {
return null;
}
}
**//THIS IS WHERE THE APP IS CRASHING WITH NULL POINTER**
_adapter_lv_Apps2 = new Adapter_Apps(
getActivity().getApplicationContext(), holder);
} else {
_adapter_lv_Apps2 = null;
}
/****************************************************/
return null;
}