请帮我处理此代码,我收到以下错误,我是新手
--------- beginning of crash
07-08 05:12:32.223 2990-2990/com.parkaspot.najeeb.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.parkaspot.najeeb.project, PID: 2990
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Double.toString()' on a null object reference
at com.parkaspot.najeeb.project.RecentAdapter$1.onClick(RecentAdapter.java:76)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我可以发送整个项目或所需的文件,请帮助我,这是我的程序
`package com.parkaspot.najeeb.project;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.github.ivbaranov.mli.MaterialLetterIcon;
import java.util.ArrayList;
import java.util.Random;
public class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder>{
private Context context;
private ArrayList<RecentData> mArrayList;
private LayoutInflater inflater;
private int[] mMaterialColors;
Location networkLocation;
Double lati,lngi;
private static final Random RANDOM = new Random();
public RecentAdapter(Context context, ArrayList<RecentData> list) {
this.context = context;
this.mArrayList = list;
inflater = LayoutInflater.from(context);
mMaterialColors = context.getResources().getIntArray(R.array.colors);
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (networkEnabled) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (networkLocation!=null){
lati = networkLocation.getLatitude();
lngi = networkLocation.getLongitude();
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.parking_lots_list,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.name.setText(mArrayList.get(position).getName());
holder.lotTotal.setText(mArrayList.get(position).getFilled()+"/"+ mArrayList.get(position).getTotal());
holder.lotService.setText(mArrayList.get(position).getService()+""+" Wheeler");
holder.nameIcon.setShapeColor(mMaterialColors[RANDOM.nextInt(mMaterialColors.length)]);
holder.nameIcon.setLetter(mArrayList.get(position).getName());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context,MainRouteActivity.class);
i.putExtra("OriginLatt",lati.toString()); //this line shows error
i.putExtra("OriginLong",lngi.toString());
i.putExtra("destinLat",mArrayList.get(position).getLat());
i.putExtra("destinLong",mArrayList.get(position).getLng());
i.putExtra("bank",mArrayList.get(position).getName());
context.startActivity(i);
}
});
}
@Override
public int getItemCount() {
return mArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name,lotTotal,lotService;
MaterialLetterIcon nameIcon;
CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
nameIcon = itemView.findViewById(R.id.icon);
nameIcon.setLetterSize(20);
nameIcon.setShapeType(MaterialLetterIcon.Shape.CIRCLE);
nameIcon.setLettersNumber(3);
nameIcon.setInitialsNumber(3);
name = itemView.findViewById(R.id.name);
lotTotal = itemView.findViewById(R.id.lot_total);
lotService = itemView.findViewById(R.id.lot_service);
cardView = itemView.findViewById(R.id.card_view);
}
}
}`
模拟器图片应用停止空指针
兄弟们请帮助我,我花了 3 天时间来解决这个问题,但我无法 我是 android 编程的新手,正在做一个大学项目,请帮我解决它
为什么我看到的是你的GPS不会得到你的位置,并且你总是收到空指针,
这是我在想要获取地理位置时使用的类
LocationDetector.kt
class LocationDetector(val context: Context) {
val fusedLocationClient: FusedLocationProviderClient = FusedLocationProviderClient(context)
var locationListener: LocationListener? = null
interface LocationListener {
fun locationFound(location: Location)
fun locationNotFound(reason: String)
}
fun detectLocation() {
//create request
val locationRequest = LocationRequest()
locationRequest.interval = 0L
// check for permission
val permissionResult = ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION)
// if have permission, try to get location within 10 seconds
if (permissionResult == android.content.pm.PackageManager.PERMISSION_GRANTED) {
val timer = Timer()
val locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
fusedLocationClient.removeLocationUpdates(this)
timer.cancel()
// return location
locationListener?.locationFound(locationResult.locations.first())
}
}
timer.schedule(timerTask {
fusedLocationClient.removeLocationUpdates(locationCallback)
locationListener?.locationNotFound("Timeout")
}, 10 * 1000) //10 seconds
// make request
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null)
} else {
// if no permission
locationListener?.locationNotFound("No permission given")
}
}
以及你会需要
的方式class MainActivity : AppCompatActivity(), LocationDetector.LocationListener {
var isGPSRunning = false
override fun locationFound(location: Location) {
AppPreferencesSingleton(applicationContext).put(AppPreferencesSingleton.Key.latitude,location.latitude.toString())
AppPreferencesSingleton(applicationContext).put(AppPreferencesSingleton.Key.longitude,location.longitude.toString())
}
override fun locationNotFound(reason: String) {
when(isGPSEnabled()){
true -> {
println("Waiting for GPS fix")
}
false -> {
if (!isGPSRunning) {
isGPSRunning = true
startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
}
}
}
}
fun isGPSEnabled() = (getSystemService(Context.LOCATION_SERVICE) as LocationManager).isProviderEnabled(LocationManager.GPS_PROVIDER)
这是在 KOTLIN中,但适应很简单,请记住在您的清单中设置所需的权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />