java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
这是在MainActivity中:
ContextWrapper bandContextWrapper = new ContextWrapper(getApplicationContext()) {
@Override public boolean bindService(Intent service, ServiceConnection conn, int flags) {
final String action = service.getAction();
if (!TextUtils.isEmpty(action) && action.startsWith("com.microsoft.kapp")) {
//service.setPackage("com.microsoft.band");
service.setPackage("com.microsoft.kapp");
}
return super.bindService(service, conn, flags);
}
};
这是在Button的OnClickListener中:
bandClient = BandClientManager.getInstance().create(bandContextWrapper, pairedBands[0]);
这是初始化的地方:
私有View.OnClickListener btnSyncClickListener = new View.OnClickListener(){
@Override
public void onClick(View button) {
try {
pairedBands = BandClientManager.getInstance().getPairedBands();
bandClient = BandClientManager.getInstance().create(bandContextWrapper, pairedBands[0]);
new ConnectTask().execute();
}
catch(Exception ex)
{
tvBandStatusMessage.setText("Could not connect");
}
}
};
按钮在OnCreate中初始化
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView b = (ImageView)findViewById(R.id.btnSync);
b.setOnClickListener(btnSyncClickListener);
}
启动模拟器时看到此错误。我正在使用Android Studio!