gpt4 book ai didi

java - NullPointerException:FragmentManagerImpl.execPendingActions(FragmentManager.java:1410)

转载 作者:行者123 更新时间:2023-11-30 09:06:53 27 4
gpt4 key购买 nike

所以,我有一个使用 Maps fragment 的 fragment 。哪个工作正常,但是当我按回或退出应用程序时,有一个 NullPointerException 并且我已经搜索和搜索但我不明白它是什么?!

以下是我的日志:

06-17 12:03:59.105: E/AndroidRuntime(32422): FATAL EXCEPTION: main
06-17 12:03:59.105: E/AndroidRuntime(32422): java.lang.NullPointerException
06-17 12:03:59.105: E/AndroidRuntime(32422): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1410)
06-17 12:03:59.105: E/AndroidRuntime(32422): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
06-17 12:03:59.105: E/AndroidRuntime(32422): at android.os.Handler.handleCallback(Handler.java:725)
06-17 12:03:59.105: E/AndroidRuntime(32422): at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 12:03:59.105: E/AndroidRuntime(32422): at android.os.Looper.loop(Looper.java:153)
06-17 12:03:59.105: E/AndroidRuntime(32422): at android.app.ActivityThread.main(ActivityThread.java:5297)
06-17 12:03:59.105: E/AndroidRuntime(32422): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 12:03:59.105: E/AndroidRuntime(32422): at java.lang.reflect.Method.invoke(Method.java:511)
06-17 12:03:59.105: E/AndroidRuntime(32422): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
06-17 12:03:59.105: E/AndroidRuntime(32422): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-17 12:03:59.105: E/AndroidRuntime(32422): at dalvik.system.NativeStart.main(Native Method)

Here是我认为正确但无法实现的答案。

谢谢你的帮助。

Java 代码:

public class NearMeFragment extends Fragment {

private GoogleMap googleMap;
double latitude = 19.18482;
double longitude = 72.83410;
Context context;
ListFragment lf;
ListView lv;
RestaurantListAdapter adapter;

GPSTracker gps;

public NearMeFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_near_me, container,
false);


getActivity().getActionBar().show();

createMap

return rootView;
}

public void createMap() {
try {
initializeMap();

for (int i = 0; i < bean.size(); i++) {
try {
latitude = Double.valueOf(bean.get(i).getLatitude());
longitude = Double.valueOf(bean.get(i).getLongitude());
} catch (NumberFormatException e) {
latitude = 19.18482;
longitude = 72.83410;
}

LatLng pos = new LatLng(latitude, longitude);

MarkerOptions marker = new MarkerOptions()
.position(pos)
.title(bean.get(i).getName())
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker));

CircleOptions circle = new CircleOptions().center(pos)
.radius(Double.valueOf(bean.get(i).getDistance()) * 10)
.strokeColor(Color.parseColor("#FFFFFF"))
.strokeWidth(5)
.fillColor(Color.parseColor("#25f67f21"));

googleMap.addMarker(marker);
googleMap.addCircle(circle);
}

// CameraPosition cp = new CameraPosition.Builder()
// .target(new LatLng(latitude, longitude)).zoom(12).build();

CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(
latitude, longitude));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(12);

googleMap.moveCamera(center);
googleMap.animateCamera(zoom);

googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setAllGesturesEnabled(true);

} catch (Exception e) {
Log.e("TAG", "MSG: " + e);
}

}

private void initializeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();

if (googleMap == null) {
Toast.makeText(NearMeFragment.this.getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_LONG)
.show();
}
}
}

@Override
public void onDestroyView() {
super.onDestroyView();
Fragment fragment = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map));
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();

}

}

XML代码:

    <fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />



</FrameLayout>

主要 Activity :

import com.porcupyne.mumbaifoodlovers.R;

import net.simonvt.menudrawer.MenuDrawer;
import android.support.v4.app.Fragment;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import static com.porcupyne.mumbaifoodlovers.LoginActivity.flag;

public class MainActivity extends Activity {

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

NearMeFragment nmf = new NearMeFragment();
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.frame_container, nmf,null);
ft.commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_search:
return true;

case android.R.id.home:
MainFragment mf = new MainFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container, mf);
ft.commit();
return true;

default:
return super.onOptionsItemSelected(item);
}
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}

}

最佳答案

试试这样:onDestroyView()

@Override
public void onDestroyView ()
{
try{
MapFragment fragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}catch(Exception e){
}
super.onDestroyView();

并且您的 Activity 必须 Extends Activity

关于java - NullPointerException:FragmentManagerImpl.execPendingActions(FragmentManager.java:1410),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24257252/

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