You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:函数传参方式是否正确?功能无响应排查

问题排查:点击按钮无响应,无法跳转Activity

尝试将数据库中存储的两个字符串传递给Directionsmanager类的getTravelTime函数,预期执行完成后从当前Activity跳转到仅显示文本的Activity,但点击按钮后无任何反应。

Logcat相关日志

2023-07-11 22:25:21.896 24459-24505 DynamiteModule com.example.startuptest1 W Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
2023-07-11 22:25:21.995 24459-24505 DynamiteModule com.example.startuptest1 I Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
2023-07-11 22:25:21.998 24459-24505 ProviderInstaller com.example.startuptest1 W Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.

按钮点击事件代码

confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txt.setText("button pressed");
                Directionsmanager dm = new Directionsmanager();
                dm.getTravelTime(startLocation, endLocation, dbdisplay.this);
            }
        });

Directionsmanager类代码

package com.example.startuptest1;

import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.functions.FirebaseFunctions;
import com.google.firebase.functions.HttpsCallableResult;

public class Directionsmanager {

    private FirebaseFunctions mFunctions;
    public Directionsmanager() {
        mFunctions = FirebaseFunctions.getInstance();
    }
    public void getTravelTime(String startLocation, String endLocation, final OnTravelTimeListener listener) {

        Calendar calendar = Calendar.getInstance();
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);


        if (dayOfWeek >= Calendar.MONDAY && dayOfWeek <= Calendar.FRIDAY) {

            Map<String, Object> data = new HashMap<>();
            data.put("startLocation", startLocation);
            data.put("endLocation", endLocation);
            data.put("mode", "transit");


            calendar.set(Calendar.HOUR_OF_DAY, 9);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);


            if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }


            data.put("arriveByTime", calendar.getTimeInMillis());


            mFunctions.getHttpsCallable("getTravelTime")
                    .call(data)
                    .addOnSuccessListener(new OnSuccessListener<HttpsCallableResult>() {
                        @Override
                        public void onSuccess(HttpsCallableResult httpsCallableResult) {
                            // Handle the result
                            String travelTime = (String) httpsCallableResult.getData();
                            listener.onTravelTimeReceived(travelTime);
                        }
                    });
        } else {

            listener.onTravelTimeReceived("No action on weekends");

        }
    }

    public interface OnTravelTimeListener {
        void onTravelTimeReceived(String travelTime);
    }
}

排查方向

  1. 检查Activity是否正确实现回调接口
    确认dbdisplay类是否实现了Directionsmanager.OnTravelTimeListener接口,并且在onTravelTimeReceived方法中编写了跳转Activity的代码。如果没有实现该接口,或者方法内没有跳转逻辑,自然不会触发页面跳转。示例实现:

    public class dbdisplay extends AppCompatActivity implements Directionsmanager.OnTravelTimeListener {
        // ... 其他代码
        @Override
        public void onTravelTimeReceived(String travelTime) {
            // 这里添加跳转Activity的代码
            Intent intent = new Intent(this, TargetTextActivity.class);
            intent.putExtra("travel_time", travelTime);
            startActivity(intent);
        }
    }
    
  2. 添加Firebase函数调用的失败监听
    当前代码仅处理了函数调用成功的情况,未处理失败场景。如果Firebase函数调用出错(比如网络问题、函数不存在、参数错误),不会触发onSuccess,也就不会执行回调。需要添加addOnFailureListener来排查错误:

    mFunctions.getHttpsCallable("getTravelTime")
            .call(data)
            .addOnSuccessListener(/* 现有成功逻辑 */)
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e("Directionsmanager", "获取行程时间失败", e);
                    // 可在此处提示用户或执行 fallback 逻辑
                }
            });
    
  3. 解决Google Play服务模块问题
    Logcat中的日志显示Google Play服务的providerinstaller.dynamite模块加载失败,这可能导致Firebase Functions无法正常工作。可以尝试以下操作:

    • 确保设备上安装了最新版本的Google Play服务
    • 在项目的build.gradle中检查Google Play服务依赖版本是否正确,确保与Firebase依赖版本兼容
    • 清理项目缓存并重新构建
  4. 验证参数有效性
    确认startLocation和endLocation是否正确从数据库获取到有效值,避免传递空字符串或无效地址导致Firebase函数无法处理。可以在按钮点击事件中添加日志打印参数值:

    Log.d("dbdisplay", "startLocation: " + startLocation + ", endLocation: " + endLocation);
    

内容的提问来源于stack exchange,提问作者Goodsir 23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 09:05:37