发送值从onPostExecute()到Activity始终为NULL(Send Value From onPostExecute() to Activity is always NULL)

编程入门 行业动态 更新时间:2024-10-27 20:29:55
发送值从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

I created this class:

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

It has the below function in it:

@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); }

I am trying to send data to the Activity called MapsActivity from the onPostExecute method.


In MapsActivity I have before onCreate this:

LatLng position = new LatLng(34.6767, 33.04455);

My 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); }

This is my onMapReady that created the pin with the position that I initialized and when you type an address and press the search button, it calls the above class that has the onPost function and trying to pin a location in the map if the position is not null.

@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(); } } }); }

The process I do is open MapsActivity, then type a correct address and trying to display it. The result is that the position doesn't being changed BUT i don't get in logcat the message NULL?﹕ position is empty! after clicking the button.


This is the logcat from the first time I navigate to MapsActivity and then click a button that calls the class:

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); }

此处不需要Intent,因为您只在同一MapsActivity调用MapsActivity 。 我们将在Activity的onPostExecute()中更新map,然后无需再次启动Activity。

步骤2:使用GoogleMap map参数为GetAddressPositionTask创建构造函数,以更新onPostExecute()的地图位置。 和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(); } }

some bit confusing code and explanation,

Lets go with step wise.

Step 1: Update your MapsActivity's onCreate()

Like,

@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); }

As here Intent is not required as you are only calling MapsActivity within same MapsActivity. We will update map in onPostExecute() of Activity then no need to start Activity again.

Step 2: Create Constructor for GetAddressPositionTask with GoogleMap map parameter to update your map position in onPostExecute() of GetAddressPositionTask. And onPostExecute()

Like,

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); } } }

Step 3: How the search Button's onClick() look like, No extra code required,

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(); } }

更多推荐

本文发布于:2023-07-26 02:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269890.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Activity   onPostExecute   Send   NULL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!