Android 对应用程序类对象调用getSystemService()时出现NullPointerException
注意:应用程序扩展了应用程序类。 因此,调用getApplicationContext().getSystemService()会引发NullPointerExceptionAndroid 对应用程序类对象调用getSystemService()时出现NullPointerException,android,Android,注意:应用程序扩展了应用程序类。 因此,调用getApplicationContext().getSystemService()会引发NullPointerException @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView == null){ N
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
NotesApplication context = NotesApplication.getInstance();
Object Object;
try{
if(context != null)
Object = context.getApplicationContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
catch(Exception e){
}
}
注意:应用程序:
public class NotesApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Values passwordSaved = isPasswordSaved();
launchActivity(passwordSaved);
}
private static class AppInstanceHolder{
public static NotesApplication notesApp = new NotesApplication();
}
public static NotesApplication getInstance(){
if(AppInstanceHolder.notesApp == null)
AppInstanceHolder.notesApp = new NotesApplication();
return AppInstanceHolder.notesApp;
}
}
在代码
注释中,应用程序
对象不是当前的应用程序
对象
因此,如下面所示更改NotesApplication类
public class NotesApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
notesApp = NotesApplication.this;
Values passwordSaved = isPasswordSaved();
launchActivity(passwordSaved);
}
public static NotesApplication notesApp;
public static NotesApplication getInstance() {
return notesApp;
}
}
显示此类注释应用程序