gpt4 book ai didi

android - 每当我从一个选项卡切换到另一个选项卡时,都会再次调用 onCreate 方法

转载 作者:太空狗 更新时间:2023-10-29 14:20:41 26 4
gpt4 key购买 nike

我正在制作一个使用 TabHost 的 Android 应用程序。在第一个选项卡(名为“MAP VIEW”)中,我在其中显示 map ,第二个选项卡用于 ListView (名为“DETAILS VIEW”)。我的问题是,当我从 MAP VIEW 选项卡切换到 DETAILS VIEW 选项卡,然后返回到 MAP VIEW 选项卡时,再次调用 onCreate 方法并再次开始加载 map 。我想要的是,当我返回到 MAP VIEW 选项卡时,它将保存我第一次离开时的行为。

这是我的显示选项卡的 Java 类。

private static final String MAP_TAB_TAG = "MAP VIEW";
private static final String DETAILS_TAB_TAG = "DETAILS VIEW";
private TabHost tHost;
private ListView mList;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);

tHost = getTabHost();

Intent intent;

intent = new Intent(Second.this, MapsView.class);

tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("MAP VIEW").setContent(intent));

intent= new Intent(Second.this, DetailsView.class);

tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("DETAILS VIEW").setContent(intent));

这是我的 MAP VIEW Java 代码(在这个类中,每次回到这个类时都会调用 onCreate)

public class MapsView extends FragmentActivity implements OnMapClickListener,
OnMapLongClickListener, OnCameraChangeListener,
OnInfoWindowClickListener , OnMarkerClickListener, LocationListener, ConnectionCallbacks, OnConnectionFailedListener{

private GoogleMap mMap;
private static final String LAT = "lat";
private static final String LONG = "lng";
Geocoder coder;
String address;
private LocationClient mLocationClient;

private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000) // 5 seconds
.setFastestInterval(16) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.maps_view);
setUpMapIfNeeded();
setUpLocationClientIfNeeded();
mLocationClient.connect();

System.out.println("Oncreate");

coder = new Geocoder(this);
}

@Override
protected void onResume() {
super.onResume();


}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mLocationClient != null) {
mLocationClient.disconnect();
}
}

private void setUpLocationClientIfNeeded() {
if (mLocationClient == null) {
mLocationClient = new LocationClient(
getApplicationContext(),
this, // ConnectionCallbacks
this); // OnConnectionFailedListener
}
}

public void showMyLocation(View view) {
if (mLocationClient != null && mLocationClient.isConnected()) {
String msg = "Location = " + mLocationClient.getLastLocation();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}

@Override
public void onConnected(Bundle connectionHint) {

Location location = mLocationClient.getLastLocation();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 14);
mMap.animateCamera(cameraUpdate);
mLocationClient.requestLocationUpdates(
REQUEST,
this); // LocationListener
}

private void setUpMapIfNeeded() {

if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
if (mMap != null) {
setUpMap();
}
}

private void setUpMap() {
// mMap.addMarker(new MarkerOptions().position(new LatLng(0,
// 0)).title("Marker"));
mMap.setMyLocationEnabled(true);
mMap.setOnMapClickListener(this);

mMap.setOnMapLongClickListener(this);
mMap.setOnCameraChangeListener(this);
mMap.setOnInfoWindowClickListener(this);
mMap.setOnMarkerClickListener(this);
}

@Override
public void onMapClick(LatLng point) {

}

@Override
public void onMapLongClick(LatLng point) {

try {
List<Address> list = coder.getFromLocation(point.latitude,
point.longitude, 1);
if (list.size() > 0) {
mMap.clear();
address = "";
if (list.get(0).getAddressLine(0) != null) {

address += list.get(0).getAddressLine(0) + " ";
}

if (list.get(0).getAddressLine(1) != null) {

address += list.get(0).getAddressLine(1) + " ";
}

if (list.get(0).getAddressLine(2) != null) {

address += list.get(0).getAddressLine(2) + " ";

}



mMap.addMarker(new MarkerOptions()
.position(point)
.title(address)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.traffic_pin)));

}
} catch (Exception ex) {
ex.printStackTrace();
}

}

@Override
public void onCameraChange(final CameraPosition position) {

}

@Override
public void onInfoWindowClick(Marker marker) {
// TODO Auto-generated method stub
String lat = "" + marker.getPosition().latitude;
String lng = "" + marker.getPosition().longitude;

startActivity(new Intent(MapsView.this, Tab.class).putExtra(LAT, lat)
.putExtra(LONG, lng).putExtra("title", marker.getTitle()).putExtra("address", address));
}

这是 LogCat 显示的结果

06-20 17:04:42.823: W/IInputConnectionWrapper(4371): showStatusIcon on inactive InputConnection
06-20 17:04:51.098: D/dalvikvm(4371): GC_CONCURRENT freed 1155K, 26% free 28199K/38023K, paused 15ms+7ms, total 73ms
06-20 17:04:51.098: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 48ms
06-20 17:04:51.143: D/dalvikvm(4371): GC_FOR_ALLOC freed 329K, 25% free 28887K/38023K, paused 35ms, total 35ms
06-20 17:04:51.263: D/SensorManager(4371): registerListener :: handle = 0 name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)b)a-,(
06-20 17:04:52.933: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 4 mFalseSizeCnt:0
06-20 17:04:53.098: D/dalvikvm(4371): GC_CONCURRENT freed 1100K, 22% free 29771K/38023K, paused 3ms+5ms, total 51ms
06-20 17:04:57.068: D/Result(4371): s
06-20 17:04:57.073: D/Result:(4371): s
06-20 17:04:57.263: D/SensorManager(4371): unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)b)a-,(
06-20 17:04:57.263: D/Sensors(4371): Remain listener = Sending .. normal delay 200ms
06-20 17:04:57.263: I/Sensors(4371): sendDelay --- 200000000
06-20 17:04:57.263: D/SensorManager(4371): JNI - sendDelay
06-20 17:04:57.263: I/SensorManager(4371): Set normal delay = true
06-20 17:04:57.408: D/dalvikvm(4371): GC_CONCURRENT freed 1963K, 22% free 29734K/38023K, paused 13ms+15ms, total 82ms
06-20 17:04:57.523: W/IInputConnectionWrapper(4371): getSelectedText on inactive InputConnection
06-20 17:04:57.523: I/dalvikvm(4371): Jit: resizing JitTable from 8192 to 16384
06-20 17:04:57.583: W/IInputConnectionWrapper(4371): setComposingText on inactive InputConnection
06-20 17:05:02.578: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
06-20 17:05:02.698: D/AbsListView(4371): Get MotionRecognitionManager
06-20 17:05:02.708: D/ProgressBar(4371): setProgress = 0
06-20 17:05:02.708: D/ProgressBar(4371): setProgress = 0, fromUser = false
06-20 17:05:02.708: D/ProgressBar(4371): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
06-20 17:05:02.818: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:02.818: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:04.203: D/response(4371): {"data":[{"id":"1","device_id":"123","lat":"33.6605","lng":"73.0779","address":"Street 95 Islamabad Pakistan ","traffic_level":"3","details":"ghsjxjdd","date":"2013-06-18","time":"07:34 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/c48f2201f748550f9442b326c9a00d60.jpg"},{"id":"2","device_id":"123","lat":"33.6606","lng":"73.0769","address":"Street 96 Islamabad Pakistan ","traffic_level":"1","details":"zuaiffhfyd","date":"2013-06-18","time":"07:44 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/5be821ec69ea233c11be8c7448646613.jpg"},{"id":"3","device_id":"123","lat":"33.6602","lng":"73.0772","address":"Street 95 Islamabad Pakistan ","traffic_level":"4","details":"yfgghtcggcfg","date":"2013-06-18","time":"07:59 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/fb801c3f5872049029bd9e54598d4dcb.jpg"},{"id":"4","device_id":"123","lat":"33.6621","lng":"73.078","address":"Street 98 Islamabad Pakistan ","traffic_level":"1","details":"new testing","date":"2013-06-20","time":"06:37 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/e091977ec224a60e6eb160d74799d591.jpg"},{"id":"5","device_id":"123","lat":"33.6686","lng":"73.0695","address":"Street 30 Islamabad Pakistan ","traffic_level":"1","details":"fydjgjyfmfhx","date":"2013-06-20","time":"08:03 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/5a909c814bf9e89352073a3d76138b4d.jpg"},{"id":"6","device_id":"123","lat":"33.6707","lng":"73.0409","address":"Kirthar Rd Islamabad Pakistan ","traffic_level":"1","details":"xfhgdjc","date":"2013-06-20","time":"08:04 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/d5e52d50ec1d78a664b6d4aeb039b080.jpg"}]}
06-20 17:05:04.453: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:04.453: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:06.038: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 21 mFalseSizeCnt:0
06-20 17:05:09.058: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 10 mFalseSizeCnt:0
06-20 17:05:09.788: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
06-20 17:05:10.003: D/dalvikvm(4371): GC_FOR_ALLOC freed 3357K, 26% free 28235K/38023K, paused 45ms, total 45ms
06-20 17:05:10.003: I/dalvikvm-heap(4371): Grow heap (frag case) to 30.033MB for 1836252-byte allocation
06-20 17:05:10.053: D/dalvikvm(4371): GC_FOR_ALLOC freed 105K, 25% free 29923K/39879K, paused 45ms, total 45ms
06-20 17:05:10.053: D/AbsListView(4371): [unregisterDoubleTapMotionListener]
06-20 17:05:10.053: I/MotionRecognitionManager(4371): .unregisterListener : / listener count = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,)b/fa*(
06-20 17:05:10.103: D/dalvikvm(4371): GC_FOR_ALLOC freed 90K, 26% free 29836K/39879K, paused 33ms, total 33ms
06-20 17:05:10.108: I/dalvikvm-heap(4371): Grow heap (frag case) to 32.958MB for 3264524-byte allocation
06-20 17:05:10.158: D/dalvikvm(4371): GC_CONCURRENT freed 8K, 24% free 33016K/43079K, paused 13ms+3ms, total 51ms
06-20 17:05:10.333: W/SurfaceView(4371): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false
06-20 17:05:10.368: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:10.368: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:11.578: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:11.578: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:12.813: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
06-20 17:05:12.853: D/AbsListView(4371): Get MotionRecognitionManager
06-20 17:05:12.863: I/System.out(4371): Oncreate
06-20 17:05:12.878: W/SurfaceView(4371): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false
06-20 17:05:12.953: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:12.953: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:13.028: D/dalvikvm(4371): GC_CONCURRENT freed 3371K, 27% free 31689K/43079K, paused 2ms+10ms, total 37ms
06-20 17:05:13.028: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 30ms
06-20 17:05:13.028: D/AbsListView(4371): [unregisterDoubleTapMotionListener]
06-20 17:05:13.033: I/MotionRecognitionManager(4371): .unregisterListener : / listener count = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,))e+aa8
06-20 17:05:13.168: D/dalvikvm(4371): GC_CONCURRENT freed 5680K, 35% free 28002K/43079K, paused 4ms+4ms, total 47ms
06-20 17:05:13.168: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 15ms
06-20 17:05:14.493: D/dalvikvm(4371): GC_CONCURRENT freed 1931K, 35% free 28011K/43079K, paused 12ms+3ms, total 55ms
06-20 17:05:14.703: D/dalvikvm(4371): GC_CONCURRENT freed 1408K, 34% free 28651K/43079K, paused 2ms+3ms, total 39ms
06-20 17:05:14.703: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 30ms
06-20 17:05:14.703: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 32ms
06-20 17:05:14.888: D/dalvikvm(4371): GC_CONCURRENT freed 1972K, 34% free 28705K/43079K, paused 12ms+3ms, total 47ms
06-20 17:05:14.888: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 17ms
06-20 17:05:15.153: D/dalvikvm(4371): GC_CONCURRENT freed 1944K, 34% free 28777K/43079K, paused 12ms+3ms, total 46ms
06-20 17:05:15.153: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 27ms
06-20 17:05:42.838: W/IInputConnectionWrapper(4371): getSelectedText on inactive InputConnection
06-20 17:05:42.843: W/IInputConnectionWrapper(4371): setComposingText on inactive InputConnection

任何帮助将不胜感激提前致谢

最佳答案

最后我找到了问题的解决方案 :-) 我在我的 TabHost 类中犯了逻辑错误看看代码

private static final String MAP_TAB_TAG = "MAP VIEW";
private static final String DETAILS_TAB_TAG = "DETAILS VIEW";
private TabHost tHost;
private ListView mList;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);

tHost = getTabHost();

Intent intent;


intent = new Intent(Second.this, MapsView.class);

tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("MAP VIEW").setContent(intent));

intent= new Intent(Second.this, DetailsView.class);

tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("DETAILS VIEW").setContent(intent));

在“DETAILS VIEW”部分,我给出了 tHost.newTabSpec(MAP_TAB_TAG)MAP_TAB_TAG 是用于 MAP VIEW 的静态变量选项卡和 DETAILS_TAB_TAG 用于 DETAILS VIEW 选项卡。所以,我的逻辑错误是我给了

tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("MAP VIEW").setContent(intent));
tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("详情 View ").setContent(intent));

如您所见,tHost.newTabSpec(MaP_TAG_TAG)用于两个选项卡,这是我的错误,现在我已经解决了这个问题,感谢您的评论并以任何方式回复

关于android - 每当我从一个选项卡切换到另一个选项卡时,都会再次调用 onCreate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17213544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com