我正在使用一个简单的代码并不断获得
java.lang.NullPointerException:侦听器不能为空
我仅使用非常简短的代码来尝试获取当前位置,但无法正常工作。它可能必须与@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={rtyuuu.class})
public class UserControllerIntegrationTest {
private MockMvc mockMvc;
@Mock
private SignOnService signOnService;
@Mock
public PersonService personService;
@Mock
public CustomerService customerService;
@Mock
private AgreementService agreementService;
@Mock
private DisbursementConfigService disbursementConfigService;
@Mock
private SignOnRqValidator signOnRqValidator;
@Mock
private ValidateTokenRqValidator validateTokenRqValidator;
@Mock
private ValidateTokenService validateTokenService;
@InjectMocks
private UserController userController;
private static final String BASE_URI = "http://fhrfbg78:7001/disbursementServices/v1/users/signOn";
private WebApplicationContext wac;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders
.standaloneSetup(userController)
.build();
}
@Test
public void testPost() throws Exception {
SignOnRq signOnRq = new SignOnRq();
signOnRq.setPassword("abc");
signOnRq.setUserName("abc");
SignOnRq signOnRq1 = new SignOnRq();
signOnRq.setPassword("abc");
signOnRq.setUserName("abc");
signOnRq.setCorrelationId("665356");
signOnRq.setResponseId("79368700");
Gson gson = new Gson();
String jsonString = gson.toJson(signOnRq);
BindingResult result = mock(BindingResult.class);
mockMvc.perform(post(BASE_URI)
.header("homeId", "88851153")
.header("ipAddress", "10.10.19.10")
.header("correlationId", "665656")
.header("X-Fiserv-UserAgent", "DISB.API.1.0")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(jsonString))
.andExpect(status().isOk());
}
}
回调中循环程序位置的“ null”有关。但我不确定。
我已经整天都在尝试。
这是短代码:
requestLocationUpdates
我只想简单地获取当前位置,那为什么给我这么难过呢?是什么导致此错误?
谢谢
答案 0 :(得分:0)
您正在尝试使用mLocationCallback进行初始化。只需移动此代码即可:
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
// Update UI with location data
TextView tv = (TextView) findViewById(R.id.tv);
Double latDouble = location.getLatitude();
String latString = latDouble.toString();
tv.setText(latString);
}
};
};
在此之前:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//
} else {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
null /* Looper */);
}
Looper线程中的null与它无关。通过将其传递为null,可以将回调保留在调用线程上。