cannot resolve method 'putExtra(android.os.bundle)' error

本文介绍了一段使用Bundle传递对象时出现的cannotresolvemethod‘putExtra(android.os.bundle)’错误及其解决方法。正确的调用应为intent.putExtras(bundle),而非intent.putExtra(bundle)。

cannot resolve method ‘putExtra(android.os.bundle)’ error

问题比较坑

Bundle bundle = new Bundle();
        bundle.putSerializable("cell",cell);
        intent.putExtra(bundle);

这段代码报错的原因是

方法为putExtras,少了个s

package com.videogo.ui.login; import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.DatePicker; import android.widget.ImageButton; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.videogo.openapi.EZOpenSDK; import com.videogo.widget.TitleBar; import ezviz.ezopensdk.R; import java.util.Calendar; import java.util.Locale; public class FanHui extends AppCompatActivity { private static final String TAG = "EZPreview"; private String mAppKey; private String mDeviceSerial; private String mVerifyCode; private String mAccessToken; private int mCameraNo; private TextView mDateTextView; private int mSelectedYear, mSelectedMonth, mSelectedDay; private static final String KEY_APPKEY = "appkey"; private static final String KEY_SERIAL = "serial"; private static final String KEY_VERIFYCODE = "VerifyCode"; private static final String KEY_ACCESSTOKEN = "accessToken"; private static final String KEY_CAMERANO = "cameraNo"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ez_playback_list_page); extractParametersFromIntent(); final Calendar calendar = Calendar.getInstance(); mSelectedYear = calendar.get(Calendar.YEAR); mSelectedMonth = calendar.get(Calendar.MONTH); mSelectedDay = calendar.get(Calendar.DAY_OF_MONTH); // 设置日期显示模块 setupDatePicker(); View fanHui = findViewById(R.id.fanhui); fanHui.setOnClickListener(v -> finish()); Button huifangBtn = findViewById(R.id.fanhui); huifangBtn.setOnClickListener(v -> { Intent intent = new Intent(FanHui.this, MainActivity.class); intent.putExtra("deviceSerial", mDeviceSerial); intent.putExtra("cameraNo", mCameraNo); intent.putExtra("accessToken", mAccessToken); intent.putExtra("appkey", mAppKey); intent.putExtra("verifyCode", mVerifyCode); startActivity(intent); }); } private void setupDatePicker() { mDateTextView = findViewById(R.id.date_text); ImageButton datePickerButton = findViewById(R.id.date_picker_button); updateDateDisplay(); datePickerButton.setOnClickListener(v -> showDatePickerDialog()); } private void updateDateDisplay() { String formattedDate = String.format(Locale.getDefault(), "%d年%02d月%02d日", mSelectedYear, mSelectedMonth + 1, // 月份需要+1 mSelectedDay); mDateTextView.setText(formattedDate); } private void showDatePickerDialog() { // 使用与 WeatherInfoActivity 完全一致的创建方式 final AlertDialog dlg = new AlertDialog.Builder(this, R.style.Theme_AppCompat_Dialog).create(); dlg.show(); Window window = dlg.getWindow(); window.setContentView(R.layout.datepicker_layout); // 设置对话框宽度(与 WeatherInfoActivity 一致) WindowManager.LayoutParams lp = window.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; window.setAttributes(lp); // 获取并初始化 DatePicker(关键修复) DatePicker dpPicker = window.findViewById(R.id.dpPicker); // 隐藏不需要的视图(与 WeatherInfoActivity 一致) ViewGroup rootView = (ViewGroup) dpPicker.getChildAt(0); if (rootView != null) { ViewGroup childView = (ViewGroup) rootView.getChildAt(0); if (childView != null) { // 隐藏日选择器 childView.getChildAt(2).setVisibility(View.GONE); // 确保月选择器可见 childView.getChildAt(1).setVisibility(View.VISIBLE); } } dpPicker.init(mSelectedYear, mSelectedMonth, mSelectedDay, null); // 设置按钮事件 RelativeLayout yesButton = window.findViewById(R.id.YES); RelativeLayout noButton = window.findViewById(R.id.NO); yesButton.setOnClickListener(v -> { mSelectedYear = dpPicker.getYear(); mSelectedMonth = dpPicker.getMonth(); mSelectedDay = dpPicker.getDayOfMonth(); updateDateDisplay(); dlg.dismiss(); }); noButton.setOnClickListener(v -> dlg.dismiss()); } private void extractParametersFromIntent() { Bundle extras = getIntent().getExtras(); if (extras != null) { mAppKey = extras.getString(KEY_APPKEY, ""); mDeviceSerial = extras.getString(KEY_SERIAL, ""); mVerifyCode = extras.getString(KEY_VERIFYCODE, ""); mAccessToken = extras.getString(KEY_ACCESSTOKEN, ""); mCameraNo = extras.getInt(KEY_CAMERANO, 0); Log.d(TAG, "Received parameters:"); Log.d(TAG, "AppKey: " + mAppKey); Log.d(TAG, "DeviceSerial: " + mDeviceSerial); Log.d(TAG, "VerifyCode: " + mVerifyCode); Log.d(TAG, "AccessToken: " + mAccessToken); Log.d(TAG, "CameraNo: " + mCameraNo); } else { Log.e(TAG, "No parameters received from intent"); } } } 依据上述代码解决报错:Cannot resolve symbol 'ViewGroup'(报错4次) Cannot resolve method 'getChildAt(int)'(报错3次)。
06-26
目前代码如下,package com.example.bus.ui.map; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import com.amap.api.maps.MapView; import com.amap.api.maps.AMap; import com.amap.api.maps.model.LatLng; //import com.example.bus.R; //import com.example.bus.databinding.FragmentMapBinding; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.UiSettings; import com.amap.api.services.poisearch.PoiResult; import com.amap.api.services.poisearch.PoiSearch; import com.example.bus.R; import com.example.bus.RoutePlanActivity; import com.example.bus.databinding.FragmentMapBinding; import com.amap.api.services.core.PoiItem; import com.amap.api.services.core.LatLonPoint; // MapView 已在 XML 中声明,无需额外 import(会自动识别) public class MapFragment extends Fragment { private FragmentMapBinding binding; private MapView mapView; // 高德地图视图 private AMap aMap; // 地图控制器 private boolean isFirstLocationSet = false; // 防止反复跳转 @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 初始化 ViewModel 和 ViewBinding binding = FragmentMapBinding.inflate(inflater, container, false); View root = binding.getRoot(); //绑定 MapView mapView = root.findViewById(R.id.map_view); mapView.onCreate(savedInstanceState); // 必须调用生命周期方法 // 初始化地图 initMap(); if (binding.mapSearch != null) { binding.mapSearch.setOnClickListener(v -> { String keyword = binding.mapInput2.getText().toString().trim(); if (keyword.isEmpty()) { Toast.makeText(requireContext(), "请输入终点", Toast.LENGTH_SHORT).show(); return; } PoiSearch.Query query = new PoiSearch.Query(keyword, "", "全国"); query.setPageSize(10); // 可选:设置每页数量 query.setPageNum(0); // 可选:第一页 try { PoiSearch search = new PoiSearch(requireContext(), query); search.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() { @Override public void onPoiSearched(PoiResult result, int rCode) { requireActivity().runOnUiThread(() -> { if (rCode == 1000 && result != null && !result.getPois().isEmpty()) { PoiItem item = result.getPois().get(0); LatLonPoint point = item.getLatLonPoint(); Intent intent = new Intent(requireContext(), RoutePlanActivity.class); intent.putExtra("start_mode", "my_location"); intent.putExtra("target_lat", point.getLatitude()); intent.putExtra("target_lng", point.getLongitude()); intent.putExtra("target_title", item.getTitle()); startActivity(intent); } else { handlePoiSearchError(rCode); } }); } @Override public void onPoiItemSearched(PoiItem item, int rCode) { // 不使用单个 POI 搜索 } }); // 发起异步请求(这个方法不会再抛异常了) search.searchPOIAsyn(); } catch (AMapException e) { e.printStackTrace(); Toast.makeText(requireContext(), "POI搜索初始化失败:" + e.getMessage(), Toast.LENGTH_LONG).show(); } }); } return root; } private void handlePoiSearchError(int rCode) { String errorMsg; switch (rCode) { case 1101: errorMsg = "网络连接异常"; break; case 1102: errorMsg = "网络响应超时"; break; case 1103: errorMsg = "解析异常"; break; case 1104: errorMsg = "请求参数非法"; break; case 1200: errorMsg = "API Key 不存在"; break; case 1202: errorMsg = "签名验证失败(SHA1/包名不匹配)"; break; case 1206: errorMsg = "API Key 权限不足(未开通 POI 搜索服务)"; break; default: errorMsg = "未知错误: " + rCode; break; } Toast.makeText(requireContext(), "搜索失败: " + errorMsg, Toast.LENGTH_LONG).show(); } /** * 初始化地图 */ private void initMap() { if (aMap == null) { aMap = mapView.getMap(); UiSettings uiSettings = aMap.getUiSettings(); uiSettings.setZoomControlsEnabled(true); // 显示缩放按钮 uiSettings.setCompassEnabled(true); // 显示指南针 uiSettings.setMyLocationButtonEnabled(false); // 我们自己控制定位行为 } } @Override public void onResume() { super.onResume(); //每次恢复可见时都检查权限状态 mapView.onResume(); // ✅ 直接开启定位图层(信任 MainActivity 的判断) if (aMap != null) { aMap.setMyLocationEnabled(true); // 只第一次进入时移动相机 if (!isFirstLocationSet) { LatLng defaultLoc = new LatLng(39.909186, 116.397411); aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLoc, 12f)); isFirstLocationSet = true; } } } @Override public void onPause() { super.onPause(); mapView.onPause(); } @Override public void onDestroyView() { super.onDestroyView(); if (mapView != null) { mapView.onDestroy(); } binding = null; } @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } } 依旧报错Unhandled exception: com.amap.api.services.core.AMapException、Cannot resolve symbol 'AMapException'、Cannot resolve method 'printStackTrace()'、Cannot resolve method 'getMessage()'
11-09
目前HomeFragment代码如下:package com.example.bus.ui.home; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import com.amap.api.services.core.AMapException; import com.amap.api.services.core.LatLonPoint; import com.amap.api.services.geocoder.GeocodeSearch; import com.amap.api.services.geocoder.RegeocodeQuery; import com.amap.api.services.geocoder.RegeocodeResult; import com.amap.api.services.help.Inputtips; import com.amap.api.services.help.Tip; import com.example.bus.MainActivity; import com.example.bus.SearchResultActivity; import com.example.bus.databinding.FragmentHomeBinding; import java.util.List; import android.widget.ArrayAdapter; import android.text.TextWatcher; import android.text.Editable; public class HomeFragment extends Fragment implements GeocodeSearch.OnGeocodeSearchListener { private FragmentHomeBinding binding; private Inputtips inputTips; private GeocodeSearch geocodeSearch; // ✅ 当前城市,默认全国 private String currentCity = "全国"; private static final int LOCATION_PERMISSION_REQUEST_CODE = 1002; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { binding = FragmentHomeBinding.inflate(inflater, container, false); View root = binding.getRoot(); setupInputTips(); setupSearchSuggestion(); binding.homeSearch.setOnClickListener(v -> { String keyword = binding.homeInput.getText().toString().trim(); if (keyword.isEmpty()) { Toast.makeText(requireContext(), "请输入关键词", Toast.LENGTH_SHORT).show(); return; } MainActivity activity = (MainActivity) requireActivity(); activity.ensureFineLocationPermission(() -> { Intent intent = new Intent(requireContext(), SearchResultActivity.class); intent.putExtra("keyword", keyword); startActivity(intent); }); }); // ✅ 初始化 GeocodeSearch try { geocodeSearch = new GeocodeSearch(requireContext()); geocodeSearch.setOnGeocodeSearchListener(this); } catch (Exception e) { e.printStackTrace(); } return root; } @Override public void onResume() { super.onResume(); // ✅ 尝试获取位置并更新 currentCity requestLocationAndDetectCity(); } /** * 请求位置权限并尝试获取当前城市 */ private void requestLocationAndDetectCity() { if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { enableMyLocationOnce(); } else { ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE); } } private void enableMyLocationOnce() { try { com.amap.api.location.AMapLocationClient locationClient = new com.amap.api.location.AMapLocationClient(requireContext()); com.amap.api.location.AMapLocationClientOption option = new com.amap.api.location.AMapLocationClientOption(); option.setLocationMode(com.amap.api.location.AMapLocationClientOption.AMapLocationMode.Battery_Saving); option.setOnceLocation(true); locationClient.setLocationOption(option); locationClient.setLocationListener(location -> { if (location != null && location.getErrorCode() == 0) { double lat = location.getLatitude(); double lon = location.getLongitude(); LatLonPoint point = new LatLonPoint(lat, lon); RegeocodeQuery query = new RegeocodeQuery(point, 200, GeocodeSearch.AMAP); try { geocodeSearch.getFromLocationAsyn(query); } catch (Exception e) { e.printStackTrace(); } } else { // 定位失败也保留 fallback currentCity = "全国"; } }); locationClient.startLocation(); } catch (SecurityException e) { currentCity = "全国"; } catch (Exception e) { e.printStackTrace(); currentCity = "全国"; } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { requestLocationAndDetectCity(); } else { currentCity = "全国"; } } } // ✅ 接收反地理编码结果,提取城市 @Override public void onRegeocodeSearched(RegeocodeResult result, int rCode) { if (rCode == 1000 && result != null && result.getRegeocodeAddress() != null) { String city = result.getRegeocodeAddress().getCity(); if (city != null && !city.isEmpty()) { currentCity = city; } else { currentCity = result.getRegeocodeAddress().getProvince(); } } else { currentCity = "全国"; } } @Override public void onGeocodeSearched(com.amap.api.services.geocoder.GeocodeResult geocodeResult, int i) { // 不处理正向编码 } private void setupInputTips() { try { inputTips = new Inputtips(requireContext(), new Inputtips.InputtipsListener() { @Override public void onGetInputtips(List<Tip> tipList, int rCode) { if (rCode == 1000 && tipList != null && !tipList.isEmpty()) { String[] arr = new String[tipList.size()]; for (int i = 0; i < tipList.size(); i++) { arr[i] = tipList.get(i).getName(); } ArrayAdapter<String> adapter = new ArrayAdapter<>( requireContext(), android.R.layout.simple_dropdown_item_1line, arr ); binding.homeInput.setAdapter(adapter); } else { binding.homeInput.setAdapter(null); handleSearchError(rCode); } } }); } catch (AMapException e) { e.printStackTrace(); Toast.makeText(requireContext(), "输入提示初始化失败", Toast.LENGTH_SHORT).show(); } } private void setupSearchSuggestion() { Handler handler = new Handler(Looper.getMainLooper()); Runnable[] pendingRunnable = {null}; binding.homeInput.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (pendingRunnable[0] != null) { handler.removeCallbacks(pendingRunnable[0]); } if (s.length() == 0) { binding.homeInput.setAdapter(null); return; } pendingRunnable[0] = () -> { try { // ✅ 使用动态获取的城市 inputTips.requestInputtips(s.toString(), currentCity); } catch (AMapException e) { e.printStackTrace(); Toast.makeText(requireContext(), "输入提示请求失败", Toast.LENGTH_SHORT).show(); } }; handler.postDelayed(pendingRunnable[0], 600); } @Override public void afterTextChanged(Editable s) {} }); } private void handleSearchError(int rCode) { String msg; switch (rCode) { case 12: msg = "API Key 错误,请检查 AndroidManifest.xml"; break; case 27: msg = "网络连接失败"; break; case 30: msg = "SHA1 或包名未正确配置"; break; case 33: msg = "请求过于频繁,请稍后再试"; break; default: msg = "搜索失败,错误码: " + rCode; break; } Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show(); } @Override public void onDestroyView() { super.onDestroyView(); inputTips = null; geocodeSearch = null; binding = null; } } MapFragment代码如下:package com.example.bus.ui.map; import android.Manifest; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.text.Editable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; // 👉 新增导入:用于动态修改约束 import androidx.constraintlayout.widget.ConstraintSet; import androidx.constraintlayout.widget.ConstraintLayout; // 🔺 新增导入:用于设置 Marker 颜色 import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.AMap; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.MapView; import com.amap.api.maps.UiSettings; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.MyLocationStyle; import com.amap.api.services.core.LatLonPoint; import com.amap.api.services.core.PoiItem; import com.amap.api.services.poisearch.PoiResult; import com.amap.api.services.poisearch.PoiSearch; import com.amap.api.services.help.Inputtips; import com.amap.api.services.help.Tip; import com.amap.api.services.geocoder.GeocodeSearch; import com.amap.api.services.geocoder.RegeocodeQuery; import com.amap.api.services.geocoder.RegeocodeResult; import com.example.bus.R; import com.example.bus.RoutePlanActivity; import com.example.bus.ResultAdapter; import com.example.bus.databinding.FragmentMapBinding; import java.util.ArrayList; import java.util.List; public class MapFragment extends Fragment implements PoiSearch.OnPoiSearchListener, GeocodeSearch.OnGeocodeSearchListener { private FragmentMapBinding binding; private MapView mapView; private AMap aMap; private Inputtips inputTips; // 数据 private List<PoiItem> poiList = new ArrayList<>(); private ResultAdapter adapter; private PoiSearch poiSearch; // 当前阶段:1=选择起点, 2=选择终点 private int selectionStage = 0; // 缓存已选 POI private PoiItem selectedStartPoi = null; private PoiItem selectedEndPoi = null; private Marker startMarker = null; private Marker endMarker = null; // 缓存关键词 private String lastStartKeyword = ""; private String lastEndKeyword = ""; // ✅ 当前城市 private String currentCity = "全国"; private static final int LOCATION_PERMISSION_REQUEST_CODE = 1001; // ✅ 标记是否已居中我的位置 private boolean userHasInteracted = false; // ✅ 反地理编码 private GeocodeSearch geocodeSearch; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { binding = FragmentMapBinding.inflate(inflater, container, false); View root = binding.getRoot(); mapView = binding.mapView; mapView.onCreate(savedInstanceState); initViews(); setupMap(savedInstanceState); setupSearchSuggestion(); return root; } private void initViews() { adapter = new ResultAdapter(poiList, this::onPoiItemSelected); binding.resultList.setLayoutManager(new LinearLayoutManager(requireContext())); binding.resultList.setAdapter(adapter); binding.mapSearch.setOnClickListener(v -> performSearch()); binding.btnSwitchTarget.setOnClickListener(v -> { if (selectionStage == 1) { showEndpointSelection(binding.mapInput2.getText().toString().trim()); } else if (selectionStage == 2) { showStartpointSelection(binding.mapInput1.getText().toString().trim()); } }); binding.btnGoTo.setOnClickListener(v -> { if (selectedStartPoi != null && selectedEndPoi != null) { Intent intent = new Intent(requireContext(), RoutePlanActivity.class); intent.putExtra(RoutePlanActivity.EXTRA_SOURCE, RoutePlanActivity.SOURCE_FROM_MAP_DIRECT); intent.putExtra("start_lat", selectedStartPoi.getLatLonPoint().getLatitude()); intent.putExtra("start_lng", selectedStartPoi.getLatLonPoint().getLongitude()); intent.putExtra("target_lat", selectedEndPoi.getLatLonPoint().getLatitude()); intent.putExtra("target_lng", selectedEndPoi.getLatLonPoint().getLongitude()); intent.putExtra("target_title", selectedEndPoi.getTitle()); startActivity(intent); } else { Toast.makeText(requireContext(), "请完成起点和终点的选择", Toast.LENGTH_SHORT).show(); } }); } private void performSearch() { String startKeyword = binding.mapInput1.getText().toString().trim(); String endKeyword = binding.mapInput2.getText().toString().trim(); if (startKeyword.isEmpty()) { Toast.makeText(requireContext(), "请输入起点", Toast.LENGTH_SHORT).show(); return; } if (endKeyword.isEmpty()) { Toast.makeText(requireContext(), "请输入终点", Toast.LENGTH_SHORT).show(); return; } // 智能判断跳过搜索 if (startKeyword.equals(lastStartKeyword) && endKeyword.equals(lastEndKeyword) && selectedStartPoi != null && selectedEndPoi != null) { binding.btnGoTo.performClick(); return; } // 展示 UI binding.containerResultList.setVisibility(View.VISIBLE); binding.buttonGroup.setVisibility(View.VISIBLE); // 动态压缩地图区域 ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone((ConstraintLayout) binding.getRoot()); constraintSet.connect( R.id.map_view, ConstraintSet.BOTTOM, R.id.container_result_list, ConstraintSet.TOP, 0 ); constraintSet.applyTo((ConstraintLayout) binding.getRoot()); userHasInteracted = true; // 隐藏软键盘 View currentFocus = requireActivity().getCurrentFocus(); if (currentFocus != null) { currentFocus.clearFocus(); InputMethodManager imm = (InputMethodManager) requireContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0); } // 执行搜索 if (!startKeyword.equals(lastStartKeyword)) { lastStartKeyword = startKeyword; lastEndKeyword = endKeyword; showStartpointSelection(startKeyword); } else if (!endKeyword.equals(lastEndKeyword)) { lastEndKeyword = endKeyword; showEndpointSelection(endKeyword); } else if (selectedStartPoi == null) { showStartpointSelection(startKeyword); } else { showEndpointSelection(endKeyword); } } // ✅ 修改:显示“搜索起点中...” private void showStartpointSelection(String keyword) { selectionStage = 1; binding.btnSwitchTarget.setText("前往选择终点"); binding.btnGoTo.setEnabled(false); binding.emptyView.setText("🔍 搜索起点中..."); binding.emptyView.setVisibility(View.VISIBLE); binding.resultList.setVisibility(View.GONE); doSearch(keyword); } // ✅ 修改:显示“搜索终点中...” private void showEndpointSelection(String keyword) { selectionStage = 2; binding.btnSwitchTarget.setText("回到选择起点"); binding.btnGoTo.setEnabled(false); binding.emptyView.setText("🔍 搜索终点中..."); binding.emptyView.setVisibility(View.VISIBLE); binding.resultList.setVisibility(View.GONE); doSearch(keyword); } private void doSearch(String keyword) { if (keyword.isEmpty()) return; PoiSearch.Query query = new PoiSearch.Query(keyword, "", currentCity); query.setPageSize(20); query.setPageNum(0); try { poiSearch = new PoiSearch(requireContext(), query); poiSearch.setOnPoiSearchListener(this); poiSearch.searchPOIAsyn(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(requireContext(), "搜索失败", Toast.LENGTH_SHORT).show(); } } private void onPoiItemSelected(PoiItem item) { LatLng latLng = new LatLng(item.getLatLonPoint().getLatitude(), item.getLatLonPoint().getLongitude()); if (selectionStage == 1) { if (startMarker != null) { startMarker.remove(); startMarker = null; } startMarker = aMap.addMarker(new MarkerOptions() .position(latLng) .title("起点:" + item.getTitle()) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))); selectedStartPoi = item; aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14f)); updateGoToButtonState(); } else if (selectionStage == 2) { if (endMarker != null) { endMarker.remove(); endMarker = null; } endMarker = aMap.addMarker(new MarkerOptions() .position(latLng) .title("终点:" + item.getTitle()) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); selectedEndPoi = item; aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14f)); updateGoToButtonState(); } userHasInteracted = true; } private void updateGoToButtonState() { binding.btnGoTo.setEnabled(selectedStartPoi != null && selectedEndPoi != null); } @Override public void onPoiSearched(PoiResult result, int rCode) { if (rCode == 1000 && result != null && result.getPois() != null && !result.getPois().isEmpty()) { List<PoiItem> newPoiList = result.getPois(); poiList.clear(); poiList.addAll(newPoiList); if (adapter == null) { adapter = new ResultAdapter(poiList, this::onPoiItemSelected); binding.resultList.setLayoutManager(new LinearLayoutManager(requireContext())); binding.resultList.setAdapter(adapter); } else { adapter.notifyDataSetChanged(); binding.resultList.scrollToPosition(0); } binding.emptyView.setVisibility(View.GONE); binding.resultList.setVisibility(View.VISIBLE); // 自动选中第一个 onPoiItemSelected(poiList.get(0)); } else { // ✅ 根据当前阶段显示不同错误信息 if (selectionStage == 1) { binding.emptyView.setText("⚠️ 未找到相关起点"); } else if (selectionStage == 2) { binding.emptyView.setText("⚠️ 未找到相关终点"); } binding.emptyView.setVisibility(View.VISIBLE); binding.resultList.setVisibility(View.GONE); } } @Override public void onPoiItemSearched(PoiItem item, int rCode) { // 接口必须实现 } private void setupMap(Bundle savedInstanceState) { mapView.onCreate(savedInstanceState); aMap = mapView.getMap(); if (aMap != null) { initMapSettings(); } else { new Handler(Looper.getMainLooper()).post(() -> { aMap = mapView.getMap(); if (aMap != null) { initMapSettings(); } else { waitAMapReady(); } }); } try { geocodeSearch = new GeocodeSearch(requireContext()); geocodeSearch.setOnGeocodeSearchListener(this); } catch (Exception e) { e.printStackTrace(); } } private void waitAMapReady() { new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { int retry = 0; @Override public void run() { if (mapView == null) return; aMap = mapView.getMap(); if (aMap != null) { initMapSettings(); } else if (retry++ < 30) { new Handler(Looper.getMainLooper()).postDelayed(this, 100); } } }, 100); } private void initMapSettings() { UiSettings uiSettings = aMap.getUiSettings(); uiSettings.setZoomControlsEnabled(true); uiSettings.setCompassEnabled(true); uiSettings.setScrollGesturesEnabled(true); uiSettings.setMyLocationButtonEnabled(true); new Handler(Looper.getMainLooper()).post(() -> aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(39.909186, 116.397411), 10f)) ); } private void setupSearchSuggestion() { try { inputTips = new Inputtips(requireContext(), new Inputtips.InputtipsListener() { @Override public void onGetInputtips(List<Tip> tipList, int rCode) { if (rCode == 1000 && tipList != null && !tipList.isEmpty()) { String[] arr = new String[tipList.size()]; for (int i = 0; i < tipList.size(); i++) { arr[i] = tipList.get(i).getName(); } ArrayAdapter<String> adapter = new ArrayAdapter<>( requireContext(), android.R.layout.simple_dropdown_item_1line, arr ); new Handler(Looper.getMainLooper()).post(() -> { if (requireActivity().getCurrentFocus() == binding.mapInput1) { binding.mapInput1.setAdapter(adapter); } else if (requireActivity().getCurrentFocus() == binding.mapInput2) { binding.mapInput2.setAdapter(adapter); } }); } else { new Handler(Looper.getMainLooper()).post(() -> { binding.mapInput1.setAdapter(null); binding.mapInput2.setAdapter(null); }); handleSearchError(rCode); } } }); } catch (Exception e) { e.printStackTrace(); Toast.makeText(requireContext(), "智能提示初始化失败", Toast.LENGTH_SHORT).show(); } Handler handler = new Handler(Looper.getMainLooper()); Runnable[] pending1 = {null}, pending2 = {null}; binding.mapInput1.addTextChangedListener(new SimpleTextWatcher(s -> { if (pending1[0] != null) handler.removeCallbacks(pending1[0]); if (s.length() == 0) { binding.mapInput1.setAdapter(null); } else { pending1[0] = () -> { new Handler(Looper.getMainLooper()).post(() -> { try { inputTips.requestInputtips(s.toString(), currentCity); } catch (Exception e) { e.printStackTrace(); } }); }; handler.postDelayed(pending1[0], 600); } })); binding.mapInput2.addTextChangedListener(new SimpleTextWatcher(s -> { if (pending2[0] != null) handler.removeCallbacks(pending2[0]); if (s.length() == 0) { binding.mapInput2.setAdapter(null); } else { pending2[0] = () -> { new Handler(Looper.getMainLooper()).post(() -> { try { inputTips.requestInputtips(s.toString(), currentCity); } catch (Exception e) { e.printStackTrace(); } }); }; handler.postDelayed(pending2[0], 600); } })); } private void handleSearchError(int rCode) { String msg; switch (rCode) { case 12: msg = "API Key 错误"; break; case 27: msg = "网络连接失败"; break; case 30: msg = "SHA1 或包名错误"; break; case 33: msg = "请求频繁"; break; default: msg = "搜索失败: " + rCode; break; } Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show(); } @Override public void onResume() { super.onResume(); mapView.onResume(); if (!userHasInteracted) { enableMyLocationLayer(); } } @Override public void onPause() { super.onPause(); mapView.onPause(); } @Override public void onDestroyView() { super.onDestroyView(); mapView.onDestroy(); inputTips = null; geocodeSearch = null; binding = null; } @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } private void enableMyLocationLayer() { if (aMap == null) return; if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER); aMap.setMyLocationStyle(myLocationStyle); aMap.setMyLocationEnabled(true); AMap.OnMyLocationChangeListener listener = location -> { if (location != null && !userHasInteracted) { LatLng curLatlng = new LatLng(location.getLatitude(), location.getLongitude()); aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(curLatlng, 16f)); userHasInteracted = true; LatLonPoint point = new LatLonPoint(location.getLatitude(), location.getLongitude()); RegeocodeQuery query = new RegeocodeQuery(point, 200, GeocodeSearch.AMAP); try { geocodeSearch.getFromLocationAsyn(query); } catch (Exception e) { e.printStackTrace(); } aMap.setOnMyLocationChangeListener(null); } }; aMap.setOnMyLocationChangeListener(listener); } else { ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (aMap != null) { MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER); aMap.setMyLocationStyle(myLocationStyle); aMap.setMyLocationEnabled(true); AMap.OnMyLocationChangeListener listener = location -> { if (location != null && !userHasInteracted) { LatLng curLatlng = new LatLng(location.getLatitude(), location.getLongitude()); aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(curLatlng, 16f)); userHasInteracted = true; LatLonPoint point = new LatLonPoint(location.getLatitude(), location.getLongitude()); RegeocodeQuery query = new RegeocodeQuery(point, 200, GeocodeSearch.AMAP); try { geocodeSearch.getFromLocationAsyn(query); } catch (Exception e) { e.printStackTrace(); } aMap.setOnMyLocationChangeListener(null); } }; aMap.setOnMyLocationChangeListener(listener); } } } } @Override public void onRegeocodeSearched(RegeocodeResult result, int rCode) { if (rCode == 1000 && result != null && result.getRegeocodeAddress() != null) { String city = result.getRegeocodeAddress().getCity(); currentCity = (city != null && !city.isEmpty()) ? city : result.getRegeocodeAddress().getProvince(); } else { currentCity = "全国"; } } @Override public void onGeocodeSearched(com.amap.api.services.geocoder.GeocodeResult geocodeResult, int i) { // 忽略 } private static class SimpleTextWatcher implements android.text.TextWatcher { private final java.util.function.Consumer<CharSequence> onTextChanged; public SimpleTextWatcher(java.util.function.Consumer<CharSequence> onTextChanged) { this.onTextChanged = onTextChanged; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void afterTextChanged(Editable s) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { onTextChanged.accept(s); } } } SearchResultActivity代码如下:package com.example.bus; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.amap.api.maps.AMap; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.MapView; import com.amap.api.maps.UiSettings; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.MyLocationStyle; import com.amap.api.services.core.LatLonPoint; import com.amap.api.services.core.PoiItem; import com.amap.api.services.poisearch.PoiResult; import com.amap.api.services.poisearch.PoiSearch; import com.amap.api.services.geocoder.GeocodeSearch; import com.amap.api.services.geocoder.RegeocodeQuery; import com.amap.api.services.geocoder.RegeocodeResult; import com.amap.api.services.help.Inputtips; import com.amap.api.services.help.Tip; import android.text.Editable; import android.text.TextWatcher; import android.widget.ArrayAdapter; import java.util.ArrayList; import java.util.List; public class SearchResultActivity extends AppCompatActivity implements PoiSearch.OnPoiSearchListener, GeocodeSearch.OnGeocodeSearchListener { private Button searchBtn, goToBtn; private RecyclerView resultListView; private List<PoiItem> poiList = new ArrayList<>(); private ResultAdapter adapter; private PoiSearch poiSearch; // 地图相关 private MapView mapView; private AMap aMap; private Marker selectedMarker; // 终点标记 // 输入提示 private Inputtips inputTips; private GeocodeSearch geocodeSearch; // 当前城市 private String currentCity = "全国"; // 是否已与地图交互(防止重复居中) private boolean userHasInteracted = false; private static final int LOCATION_PERMISSION_REQUEST_CODE = 1001; // ✅ 新增:空状态提示视图 private TextView emptyView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_result); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle("搜索地点"); } initViews(); setupMap(savedInstanceState); setupSearchSuggestion(); try { geocodeSearch = new GeocodeSearch(this); geocodeSearch.setOnGeocodeSearchListener(this); } catch (Exception e) { e.printStackTrace(); } // ✅ 新增:接收 keyword 并模拟“用户输入 + 点击搜索” new Handler(Looper.getMainLooper()).post(() -> { String keyword = getIntent().getStringExtra("keyword"); if (keyword == null || keyword.isEmpty()) return; android.widget.AutoCompleteTextView inputView = findViewById(R.id.search_input); Button searchBtn = findViewById(R.id.search_btn); inputView.setText(keyword); inputView.setSelection(keyword.length()); inputView.clearFocus(); // 防止键盘弹出 searchBtn.performClick(); // 触发搜索 }); } private void initViews() { searchBtn = findViewById(R.id.search_btn); resultListView = findViewById(R.id.result_list); goToBtn = findViewById(R.id.btn_go_to); emptyView = findViewById(R.id.empty_view); goToBtn.setEnabled(false); adapter = new ResultAdapter(poiList, this::onPoiItemSelected); resultListView.setLayoutManager(new LinearLayoutManager(this)); resultListView.setAdapter(adapter); resultListView.setVisibility(View.GONE); emptyView.setVisibility(View.GONE); } private void setupMap(Bundle savedInstanceState) { mapView = findViewById(R.id.map_view); mapView.onCreate(savedInstanceState); aMap = mapView.getMap(); if (aMap != null) { initMapSettings(); } else { new Handler(Looper.getMainLooper()).post(() -> { aMap = mapView.getMap(); if (aMap != null) { initMapSettings(); } else { waitAMapReady(); } }); } } private void waitAMapReady() { new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { int retry = 0; @Override public void run() { if (mapView == null) return; aMap = mapView.getMap(); if (aMap != null) { initMapSettings(); } else if (retry++ < 30) { new Handler(Looper.getMainLooper()).postDelayed(this, 100); } } }, 100); } private void initMapSettings() { UiSettings uiSettings = aMap.getUiSettings(); uiSettings.setZoomControlsEnabled(true); uiSettings.setCompassEnabled(true); uiSettings.setScrollGesturesEnabled(true); uiSettings.setMyLocationButtonEnabled(true); aMap.setOnMapClickListener(latLng -> { onCustomLocationSelected(latLng); }); new Handler(Looper.getMainLooper()).post(() -> aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(39.909186, 116.397411), 10f)) ); enableMyLocationLayer(); } private void enableMyLocationLayer() { if (aMap == null) return; if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER); aMap.setMyLocationStyle(myLocationStyle); aMap.setMyLocationEnabled(true); AMap.OnMyLocationChangeListener listener = location -> { if (location != null && !userHasInteracted) { LatLng curLatlng = new LatLng(location.getLatitude(), location.getLongitude()); aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(curLatlng, 16f)); userHasInteracted = true; LatLonPoint point = new LatLonPoint(location.getLatitude(), location.getLongitude()); RegeocodeQuery query = new RegeocodeQuery(point, 200, GeocodeSearch.AMAP); try { geocodeSearch.getFromLocationAsyn(query); } catch (Exception e) { e.printStackTrace(); } aMap.setOnMyLocationChangeListener(null); } }; aMap.setOnMyLocationChangeListener(listener); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { enableMyLocationLayer(); } else { Toast.makeText(this, "定位权限被拒绝,部分功能受限", Toast.LENGTH_LONG).show(); currentCity = "全国"; } } } private void setupSearchSuggestion() { try { inputTips = new Inputtips(this, new Inputtips.InputtipsListener() { @Override public void onGetInputtips(List<Tip> tipList, int rCode) { if (rCode == 1000 && tipList != null && !tipList.isEmpty()) { String[] arr = new String[tipList.size()]; for (int i = 0; i < tipList.size(); i++) { arr[i] = tipList.get(i).getName(); } ArrayAdapter<String> adapter = new ArrayAdapter<>( SearchResultActivity.this, android.R.layout.simple_dropdown_item_1line, arr ); ((android.widget.AutoCompleteTextView) findViewById(R.id.search_input)) .setAdapter(adapter); } else { ((android.widget.AutoCompleteTextView) findViewById(R.id.search_input)) .setAdapter(null); handleSearchError(rCode); } } }); } catch (Exception e) { e.printStackTrace(); } android.widget.AutoCompleteTextView inputView = findViewById(R.id.search_input); Handler handler = new Handler(Looper.getMainLooper()); Runnable[] pendingRunnable = {null}; inputView.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (pendingRunnable[0] != null) { handler.removeCallbacks(pendingRunnable[0]); } if (s.length() == 0) { inputView.setAdapter(null); return; } pendingRunnable[0] = () -> { new Handler(Looper.getMainLooper()).post(() -> { try { inputTips.requestInputtips(s.toString(), currentCity); } catch (Exception e) { e.printStackTrace(); } }); }; handler.postDelayed(pendingRunnable[0], 600); } @Override public void afterTextChanged(Editable s) {} }); searchBtn.setOnClickListener(v -> { String keyword = inputView.getText().toString().trim(); if (!keyword.isEmpty()) { performSearch(keyword); } else { Toast.makeText(this, "请输入关键词", Toast.LENGTH_SHORT).show(); } }); } private void performSearch(String keyword) { emptyView.setText("🔍 搜索中..."); emptyView.setVisibility(View.VISIBLE); resultListView.setVisibility(View.GONE); PoiSearch.Query query = new PoiSearch.Query(keyword, "", currentCity); query.setPageSize(20); query.setPageNum(0); try { poiSearch = new PoiSearch(this, query); poiSearch.setOnPoiSearchListener(this); poiSearch.searchPOIAsyn(); } catch (Exception e) { Toast.makeText(this, "搜索失败", Toast.LENGTH_SHORT).show(); emptyView.setText("⚠️ 未找到相关地点"); } } private void handleSearchError(int rCode) { String msg; switch (rCode) { case 12: msg = "API Key 错误"; break; case 27: msg = "网络失败"; break; case 30: msg = "SHA1/包名错误"; break; case 33: msg = "请求频繁"; break; default: msg = "错误码=" + rCode; break; } Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } private void onPoiItemSelected(PoiItem item) { LatLng latLng = new LatLng(item.getLatLonPoint().getLatitude(), item.getLatLonPoint().getLongitude()); if (selectedMarker != null) { selectedMarker.remove(); selectedMarker = null; } selectedMarker = aMap.addMarker(new MarkerOptions() .position(latLng) .title("终点:" + item.getTitle()) .icon(com.amap.api.maps.model.BitmapDescriptorFactory.defaultMarker( com.amap.api.maps.model.BitmapDescriptorFactory.HUE_RED))); aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14f)); goToBtn.setEnabled(true); } private void onCustomLocationSelected(LatLng latLng) { if (selectedMarker != null) { selectedMarker.remove(); } selectedMarker = aMap.addMarker(new MarkerOptions() .position(latLng) .title("终点:自定义位置") .icon(com.amap.api.maps.model.BitmapDescriptorFactory.defaultMarker( com.amap.api.maps.model.BitmapDescriptorFactory.HUE_RED))); goToBtn.setEnabled(true); } @Override public void onPoiSearched(PoiResult result, int rCode) { if (rCode == 1000 && result != null && result.getPois() != null && !result.getPois().isEmpty()) { List<PoiItem> newPoiList = result.getPois(); poiList.clear(); poiList.addAll(newPoiList); adapter.notifyDataSetChanged(); resultListView.scrollToPosition(0); resultListView.setVisibility(View.VISIBLE); emptyView.setVisibility(View.GONE); onPoiItemSelected(poiList.get(0)); } else { emptyView.setText("⚠️ 未找到相关地点"); emptyView.setVisibility(View.VISIBLE); resultListView.setVisibility(View.GONE); } } @Override public void onPoiItemSearched(PoiItem item, int rCode) { // 必须实现 } @Override public void onRegeocodeSearched(RegeocodeResult result, int rCode) { if (rCode == 1000 && result != null && result.getRegeocodeAddress() != null) { String city = result.getRegeocodeAddress().getCity(); currentCity = (city != null && !city.isEmpty()) ? city : result.getRegeocodeAddress().getProvince(); } else { currentCity = "全国"; } } @Override public void onGeocodeSearched(com.amap.api.services.geocoder.GeocodeResult geocodeResult, int i) { // 忽略 } @Override protected void onResume() { super.onResume(); mapView.onResume(); } @Override protected void onPause() { super.onPause(); mapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); inputTips = null; geocodeSearch = null; } @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } @Override public boolean onSupportNavigateUp() { onBackPressed(); return true; } } 请你为我提供修改好的完整.java代码,确保是最小量修改,不改变目前已有的其它任何功能,不删除功能和添加其它功能
最新发布
11-29
如何解决package com.example.aiencrypeddiary; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.example.aiencrypeddiary.adapter.DiaryAdapter; import com.example.aiencrypeddiary.db.DiaryDao; import com.example.aiencrypeddiary.db.DiaryDatabase; import com.example.aiencrypeddiary.model.Diary; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.util.ArrayList; import java.util.List; import cn.leancloud.LCObject; import cn.leancloud.LCQuery; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; import io.reactivex.rxjava3.disposables.CompositeDisposable; import io.reactivex.rxjava3.schedulers.Schedulers; import io.reactivex.rxjava3.core.Observer; import io.reactivex.rxjava3.disposables.Disposable; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private DiaryAdapter adapter; private DiaryDatabase db; private DiaryDao diaryDao; private static final String TAG = "MainActivity"; private CompositeDisposable disposable = new CompositeDisposable(); // 替换成你的LeanCloud用户标识(自己用固定值即可) private static final String CURRENT_USER_ID = "my_ai_diary_user"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate: 页面创建开始"); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); Log.d(TAG, "onCreate: 布局加载完成"); ViewCompat.setOnApplyWindowInsetsListener(getWindow().getDecorView(), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); // 初始化RecyclerView recyclerView = findViewById(R.id.recycler_view); Log.d(TAG, "onCreate: RecyclerView 初始化完成"); recyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter = new DiaryAdapter(this, new ArrayList<>()); // 日记点击跳转详情页 adapter.setOnDiaryClickListener(diary -> { Intent intent = new Intent(MainActivity.this, DiaryDetailActivity.class); intent.putExtra("diary", diary); startActivity(intent); }); recyclerView.setAdapter(adapter); // 初始化本地数据库 db = DiaryDatabase.getInstance(this); diaryDao = db.diaryDao(); Log.i(TAG, "onCreate: 本地数据库实例获取成功"); // 加载本地日记 + 从LeanCloud加载服务器日记 loadAllDiaries(); loadFromLeanCloud(); Log.d(TAG, "onCreate: 开始加载日记数据"); // 新增日记按钮 FloatingActionButton fabAdd = findViewById(R.id.fab_add); fabAdd.setOnClickListener(v -> { Log.d(TAG, "fabAdd onClick: 点击添加日记按钮"); startActivity(new Intent(MainActivity.this, EditDiaryActivity.class)); }); } // 原有:加载本地日记 public void loadAllDiaries() { Log.d(TAG, "loadAllDiaries: 开始异步查询本地日记数据"); disposable.add( diaryDao.getAllDiaries() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( diaries -> { Log.i(TAG, "loadAllDiaries: 本地日记加载成功,共 " + diaries.size() + " 条"); // 先显示本地日记,后续服务器日记会覆盖更新 if (adapter.getItemCount() == 0) { adapter.updateData(diaries); } }, error -> { Log.e(TAG, "loadAllDiaries: 本地日记加载失败:" + error.getMessage()); Toast.makeText(this, "加载本地日记失败:" + error.getMessage(), Toast.LENGTH_SHORT).show(); } ) ); } // 新增:从LeanCloud加载服务器日记 private void loadFromLeanCloud() { Log.d(TAG, "loadFromLeanCloud: 开始从服务器加载日记"); // 查询LeanCloud的Diary表,筛选当前用户的日记 LCQuery<LCObject> query = new LCQuery<>("Diary"); query.whereEqualTo("userId", CURRENT_USER_ID); // 只查自己的日记 query.orderByDescending("updateTime"); // 最新的日记在前 // 执行查询 query.findInBackground() .subscribe(new Observer<List<LCObject>>() { @Override public void onSubscribe(Disposable d) { disposable.add(d); // 加入CompositeDisposable管理 } @Override public void onNext(List<LCObject> lcObjects) { Log.i(TAG, "loadFromLeanCloud: 服务器日记查询成功,共 " + lcObjects.size() + " 条"); // 将LeanCloud的LCObject转成本地Diary对象 List<Diary> serverDiaries = new ArrayList<>(); for (LCObject lcObj : lcObjects) { Diary diary = new Diary(); // 手动映射字段(服务器字段名 -> 本地字段) diary.setTitle(lcObj.getString("title")); diary.setContent(lcObj.getString("encryptedContent")); // 服务器存的是加密内容 diary.setEncryptedContent(lcObj.getString("encryptedContent")); diary.setImagePath(lcObj.getString("imagePath")); diary.setCreateTime(lcObj.getLong("createTime")); diary.setUpdateTime(lcObj.getLong("updateTime")); diary.setSensitivity(lcObj.getInt("sensitivity")); diary.setUserId(CURRENT_USER_ID); diary.setObjectId(lcObj.getObjectId()); // 保存服务器唯一ID serverDiaries.add(diary); } // 主线程更新UI,显示服务器日记 runOnUiThread(() -> { adapter.updateData(serverDiaries); Toast.makeText(MainActivity.this, "已同步服务器日记(" + serverDiaries.size() + "条)", Toast.LENGTH_SHORT).show(); }); } @Override public void onError(Throwable e) { Log.e(TAG, "loadFromLeanCloud: 服务器日记加载失败:" + e.getMessage()); runOnUiThread(() -> Toast.makeText(MainActivity.this, "同步服务器失败:" + e.getMessage(), Toast.LENGTH_SHORT).show() ); } @Override public void onComplete() {} }); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy: 页面销毁,释放资源"); disposable.dispose(); // 取消所有订阅,防止内存泄露 } }里Cannot resolve method 'subscribe(Observer<List<LCObject>>)'的问题
10-29
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值