按下按钮时出现错误

Posted

技术标签:

【中文标题】按下按钮时出现错误【英文标题】:There is an error when I press the button 【发布时间】:2021-09-14 11:47:38 【问题描述】:

如果我按下呼叫按钮,我会得到一个错误,即出租车没有呼叫,而是去另一个窗口。

   protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customers_map);

    customerLogoutButton = (Button) findViewById(R.id.cuslomer_logout_button);
    settingsButton = (Button)findViewById(R.id.cuslomer_settings_button);
    callTaxiButton = (Button)findViewById(R.id.cuslomer_order_button);
    callDriver = findViewById(R.id.call_to_driver);

    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();
    customerID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    CustomerDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Customers Requests");
    DriversAvailableRef = FirebaseDatabase.getInstance().getReference().child("Driver Available");
    DriversLocationRef = FirebaseDatabase.getInstance().getReference().child("Driver Working");

    txtName = (TextView)findViewById(R.id.driver_name);
    txtPhone = (TextView)findViewById(R.id.driver_phone_number);
    txtCarName = (TextView)findViewById(R.id.driver_car);
    driverPhoto = (CircleImageView)findViewById(R.id.driver_photo);
    relativeLayout = findViewById(R.id.rel1);

    relativeLayout.setVisibility(View.INVISIBLE);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    settingsButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            Intent intent = new Intent(CustomersMapActivity.this, SettingsActivity.class);
            intent.putExtra("type", "Customers");
            startActivity(intent);
        
    );

    customerLogoutButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            mAuth.signOut();
            LogoutCustomer();
        
    );

    callTaxiButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v)
        
            if (requestType)
            
                requestType = false;
                GeoFire geofire = new GeoFire(CustomerDatabaseRef);
                geofire.removeLocation(customerID);

                if(PickUpMarker !=null)
                
                    PickUpMarker.remove();
                
                if(driverMarker !=null)
                
                    driverMarker.remove();
                

                callTaxiButton.setText("Вызвать такси");
                if (driverFound!=null)
                
                    DriversRef = FirebaseDatabase.getInstance().getReference()
                            .child("Users").child("Drivers").child(driverFoundID).child("CustomerRideID");

                    DriversRef.removeValue();

                    driverFoundID = null;
                

                driverFound = false;
                radius = 1;


            
            else 
                requestType = true;

                GeoFire geofire = new GeoFire(CustomerDatabaseRef);
                geofire.setLocation(customerID, new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()));

                CustomerPosition = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
                PickUpMarker = mMap.addMarker(new MarkerOptions().position(CustomerPosition).title("Я здесь").icon(BitmapDescriptorFactory.fromResource(R.drawable.user)));

                callTaxiButton.setText("Поиск водителя...");
                getNearbyDrivers();
            


        
    );

我认为这个错误来自实时数据库。如果您有不同的意见,请写下您的答案。

错误: E/androidRuntime: 致命异常: main 进程:com.example.drivetaxiuzbek,PID:8915 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double android.location.Location.getLatitude()” 在 com.example.drivetaxiuzbek.CustomersMapActivity$3.onClick(CustomersMapActivity.java:170) 在 android.view.View.performClick(View.java:7125) 在 android.view.View.performClickInternal(View.java:7102) 在 android.view.View.access$3500(View.java:801) 在 android.view.View$PerformClick.run(View.java:27336) 在 android.os.Handler.handleCallback(Handler.java:883) 在 android.os.Handler.dispatchMessage(Handler.java:100) 在 android.os.Looper.loop(Looper.java:214) 在 android.app.ActivityThread.main(ActivityThread.java:7356) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

enter image description here

【问题讨论】:

你确定lastLocation 不为空吗? 这能回答你的问题吗? What is a NullPointerException, and how do I fix it? 【参考方案1】:

在运行代码之前尝试检查lastLocation 是否为null。仅在lastLocation 不为空时运行:

if(lastLocation!=null)
 GeoFire geofire = new GeoFire(CustomerDatabaseRef);
                geofire.setLocation(customerID, new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()));

                CustomerPosition = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
                PickUpMarker = mMap.addMarker(new MarkerOptions().position(CustomerPosition).title("Я здесь").icon(BitmapDescriptorFactory.fromResource(R.drawable.user)));

                callTaxiButton.setText("Поиск водителя...");
                getNearbyDrivers();

【讨论】:

以上是关于按下按钮时出现错误的主要内容,如果未能解决你的问题,请参考以下文章

按下更新按钮时出现错误

按下按钮时出现无法识别的选择器错误

按下按钮时出现错误 EXC_BAD_ACCESS

按下按钮时创建多个文本框时出现错误 400

切换活动时出现Logcat错误导致应用程序在按下按钮后崩溃

尝试关闭键盘时出现“无法识别的选择器”错误