簡體   English   中英

發送值從onPostExecute()到Activity始終為NULL

[英]Send Value From onPostExecute() to Activity is always NULL

我創建了這個類:

public class GetAddressPositionTask extends
        AsyncTask<String, Integer, LatLng> {
    //...
}

它具有以下功能:

@Override
    public void onPostExecute(LatLng result) {
        Log.i("GEOCODE", result.toString());

        super.onPostExecute(result);

        Intent i = new Intent(this.mainContxt , MapsActivity.class);

        i.putExtra("latlng" , result);

        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        this.mainContxt.startActivity(i);
    }

我正在嘗試從onPostExecute方法將數據發送到名為MapsActivity的Activity。


在MapsActivity中,我之前在onCreate這個:

LatLng position = new LatLng(34.6767, 33.04455);

我的onCreate:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

            if (getIntent() != null) {
                Intent intent = getIntent();

                if (getIntent().getExtras().getParcelable("latlng")!= null) {
                    position = getIntent().getExtras().getParcelable("latlng");
                }
                else {
                    Log.d("NULL?", "position is empty!");
                }
            } else {
            }

            MapFragment mapFragment = (MapFragment) getFragmentManager()
                    .findFragmentById(R.id.
                            map);
            mapFragment.getMapAsync(this);
    }

這是我的onMapReady,它創建了我初始化的位置的引腳,當你輸入一個地址並按下搜索按鈕時,它調用上面的具有onPost函數的類,如果位置不是,則嘗試在地圖中固定一個位置空值。

@Override
    public void onMapReady(final GoogleMap map) {
        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 13));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
        map.animateCamera(zoom);

        map.addMarker(new MarkerOptions()
                .title("Shop")
                .snippet("Is this the right location?")
                .position(position))
                .setDraggable(true);

        // map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
        map.setOnInfoWindowClickListener(this);
        map.setOnMarkerDragListener(this);

        ImageButton search = (ImageButton) findViewById(R.id.search);

        final EditText searchaddress = (EditText) findViewById(R.id.locationsearch);

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //FIND LOCATION BY ADDRESS

                if (searchaddress.getText().toString() != null && !searchaddress.getText().toString().isEmpty()) {

                    new GetAddressPositionTask(getApplicationContext()).execute(searchaddress.getText().toString());

                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 13));
                    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
                    map.animateCamera(zoom);

                    //Marker marker = null;
                    map.clear();
                    //marker.setPosition(position);
                    map.addMarker(new MarkerOptions()
                            .title("Shop")
                            .snippet("Is this the right location?")
                            .position(position))
                            .setDraggable(true);

                    // map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
                    map.setOnInfoWindowClickListener(MapsActivity.this);
                    map.setOnMarkerDragListener(MapsActivity.this);
                } else {
                    Toast.makeText(getApplicationContext(), "Please enter an address!", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

我所做的過程是打開MapsActivity,然后輸入正確的地址並嘗試顯示它。 結果是位置沒有被改變但是我沒有在logcat中得到消息NULL?﹕ position is empty! 點擊按鈕后。


這是我第一次導航到MapsActivity然后單擊調用該類的按鈕時的logcat:

02-24 20:55:35.133    5907-5907/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 20:55:35.143    5907-5907/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 20:55:35.143    5907-5907/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 20:55:35.223    5907-5907/guide_me_for_all.guide_me_for_all I/Choreographer﹕ Skipped 47 frames!  The application may be doing too much work on its main thread.
02-24 20:55:36.775    5907-5907/guide_me_for_all.guide_me_for_all I/Timeline﹕ Timeline: Activity_launch_request id:guide_me_for_all.guide_me_for_all time:42579264
02-24 20:55:36.865    5907-5907/guide_me_for_all.guide_me_for_all I/x﹕ Making Creator dynamically
02-24 20:55:37.265    5907-5907/guide_me_for_all.guide_me_for_all I/Google Maps Android API﹕ Google Play services client version: 6587000
02-24 20:55:37.285    5907-5907/guide_me_for_all.guide_me_for_all I/Google Maps Android API﹕ Google Play services package version: 6776034
02-24 20:55:38.406    5907-5907/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.ew.c
02-24 20:55:38.406    5907-5907/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 441: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
02-24 20:55:38.406    5907-5907/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000f
02-24 20:55:39.117    5907-5907/guide_me_for_all.guide_me_for_all D/NULL?﹕ position is empty!
02-24 20:55:39.377    5907-5907/guide_me_for_all.guide_me_for_all I/Choreographer﹕ Skipped 57 frames!  The application may be doing too much work on its main thread.
02-24 20:55:39.517    5907-5907/guide_me_for_all.guide_me_for_all I/libblt_hw﹕ Library opened (handle = 0, fd = 100)
02-24 20:55:39.788    5907-5907/guide_me_for_all.guide_me_for_all I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@424fec18 time:42582274
02-24 20:55:41.970    5907-5912/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Jit: resizing JitTable from 4096 to 8192
02-24 20:55:47.946    5907-6133/guide_me_for_all.guide_me_for_all I/GEOCODE_background﹕ lat/lng: (64.963051,-19.020835)
02-24 20:55:47.946    5907-5907/guide_me_for_all.guide_me_for_all I/GEOCODE﹕ lat/lng: (64.963051,-19.020835)
02-24 20:55:47.946    5907-5907/guide_me_for_all.guide_me_for_all I/Timeline﹕ Timeline: Activity_launch_request id:guide_me_for_all.guide_me_for_all time:42590434

有點令人困惑的代碼和解釋,

讓我們一步一步走。

第1步:更新您的MapsActivity的onCreate()

喜歡,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

由於這里不需要意圖,你只叫MapsActivity相同的內MapsActivity 我們將在Activity的onPostExecute()中更新map,然后不需要再次啟動Activity。

第2步:創建構造GetAddressPositionTaskGoogleMap map參數更新您的地圖位置onPostExecute()GetAddressPositionTask onPostExecute()

喜歡,

public class GetAddressPositionTask extends
        AsyncTask<String, Integer, LatLng> {

  GoogleMap googleMap;
  LatLng mapPosition;

  GetAddressPositionTask(GoogleMap map, LatLng position)
  {
   googleMap = map;
   mapPosition = position;
  }
    //...
@Override
    public void onPostExecute(LatLng result) {

     if(result != null)
     {
       Log.i("GEOCODE", result.toString());
       mapPosition =  result;

        googleMap.clear();
        googleMap.addMarker(new MarkerOptions()
                            .title("Shop")
                            .snippet("Is this the right location?")
                            .position(mapPosition))
                            .setDraggable(true);
      }
   }
}

第3步:搜索按鈕的onClick()如何顯示,不需要額外的代碼,

public void onClick(View v) {
   //FIND LOCATION BY ADDRESS

   if (searchaddress.getText().toString() != null && !searchaddress.getText().toString().isEmpty()) {

          GetAddressPositionTask addressTask = new GetAddressPositionTask(map, position);
          addressTask.execute(searchaddress.getText().toString());

  } else {
          Toast.makeText(getApplicationContext(), "Please enter an address!", Toast.LENGTH_LONG).show();
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM