设备是否获取位置。问题是当我需要访问位置时,public class ListAdapter extends ArrayAdapter<datamodel> implements RecognitionListener {
public static class ViewHolderItems {
AtomPayment datamodel;
TextView total_hrs;
TextView date;
TextView Hour;
TextView value;
ToggleButton mic_for_hrs;
ToggleButton mic_for_desc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
row = convertView;
ViewHolderItems holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.list_row_addtime, parent, false);
holder = new ViewHolderItems();
holder.atomPayment = items.get(position);
holder.mic_for_hrs = (ToggleButton) row.findViewById(R.id.MIC_hours);
holder.mic_for_hrs.setTag(holder.atomPayment);
speak_hours(holder, position);
holder.mic_for_desc = (ToggleButton) row.findViewById(R.id.MIC_desc);
holder.mic_for_desc.setTag(holder.atomPayment);
holder.date = (TextView) row.findViewById(R.id.textview_date);
setDateTextChangeListener(holder);
holder.Hour = (EditText) row.findViewById(R.id.edit_hours_data);
setHourTextChangeListener(holder);
holder.value = (EditText) row.findViewById(R.id.edit_desc_data);
setValueTextListeners(holder);
} else {
holder = (ViewHolderItems) row.getTag();
}
row.setTag(holder);
return row;
}
@Override
public void onResults(Bundle results) {
Log.i(LOG_TAG, "onResults 1");
String text = "";
Float valueof_text;
Integer compare = 24;
try {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
text += matches.get(0);
System.out.println("MAIN-----------------> " + matches);
System.out.println("TAG-----------------> " + matches.get(0));
valueof_text = Float.parseFloat(text);
if (valueof_text.intValue() > compare.intValue()) {
Toast.makeText(context, "Speak Again!!!", Toast.LENGTH_LONG).show();
}
else {
holder.Hour.setText(text);
}
} catch (NumberFormatException number){
number.getMessage(); number.printStackTrace();
} catch (NullPointerException number){
number.getMessage(); number.printStackTrace();
}
}
会一直返回java.lang.NullPointerException at com.example.testing.ListAdapter.onResults(ListAdapter.java:429)
at android.speech.SpeechRecognizer$InternalListener$1.handleMessage(SpeechRecognizer.java:448)
at android.os.Handler.dispatchMessage(Handler.java:110)
值。
我的DeviceLocation.class
班级
null
在我的DeviceLocation
中,我声明了一个新的public class DeviceLocation{
private static final String TAG = "DeviceLocation";
private FusedLocationProviderClient mFusedLocationProviderClient;
private static final int ERROR_DIALOG_REQUEST = 200;
private static Location mLocation;
private Context context;
public DeviceLocation(Context context) {
this.context = context;
}
private void updateDeviceLocation() {
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET}, 123);
}
return;
}
mFusedLocationProviderClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location != null){
mLocation = location;
Log.d(TAG, "Device Location is: " + mLocation.getLatitude() + ", " + mLocation.getLongitude()); //// ---> THIS WORKS
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "Error trying to get last Location");
}
});
}
public void getDeviceLocation(){
updateDeviceLocation();
Log.d(TAG, "getDeviceLocation() --> " + mLocation); // WHY DOES NOT THIS WORK!!! ---> WHY DOES THIS PRINT 'null'!!!!!!!!!!
}
}
对象,并在MainActivity
方法中对其进行了初始化。然后我调用DeviceLocation
类的onCreate()
方法。
MainActivity.class:
getDeviceLocation()
onCreate()方法:
DeviceLocatio