我有一个Activity
,可在ActionBar
中加载2个标签。标签1加载Fragment
,这将使WebView
膨胀,而标签2加载带有地图的Fragment
。最初,我遇到了更改标签导致应用程序崩溃的问题。我按照说明here执行了onDestroyView()
。
现在的问题是,如果我按Tab 1(WebView
)的后退按钮,它会正确退出Activity
。但如果我在Tab 2(地图)上这样做,应用程序崩溃了。当我改变方向时,完全相同的情况适用。我确信它与onDestroyView()
有关,但我不确定它是什么。
我甚至试图Override
后退按钮,但没有任何作用。
这是我提供一些上下文的代码:
活动类:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Displaying the tabs by calling ActionBar
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Details Tab
String label1 = getResources().getString(R.string.details);
Tab tab = actionBar.newTab();
tab.setText(label1);
TabListener<DetailsFragment> t1 = new TabListener<DetailsFragment>(this, label1, DetailsFragment.class);
tab.setTabListener(t1);
actionBar.addTab(tab);
//Map Tab
String label2 = getResources().getString(R.string.map);
tab = actionBar.newTab();
tab.setText(label2);
TabListener<MapFragment> t2 = new TabListener<MapFragment>(this, label2, MapFragment.class);
tab.setTabListener(t2);
actionBar.addTab(tab);
}
private class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/**
* Constructor used each time a new tab is created.
*
* @param activity
* The host Activity, used to instantiate the fragment
* @param tag
* The identifier tag for the fragment
* @param clz
* The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
@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);
return true;
} }
DetailsFragment:
public class DetailsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.morning, container, false);
WebView webview = (WebView) v.findViewById(R.id.details);
webview.loadUrl("file:///android_asset/pools/details.html");
return v;
} }
MapFragment:
public class MapFragment extends Fragment {
static final LatLng mapLatLng = new LatLng("some numbers", "some numbers");
private GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map, container, false);
map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
Marker amksc = map.addMarker(new MarkerOptions().position(mapLatLng).title("Map"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(mapLatLng, 18));
return v;
}
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment destroyMe = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
if (destroyMe != null) {
getFragmentManager().beginTransaction().remove(destroyMe).commit();
}
} }
感谢您阅读这么长的帖子。任何帮助是极大的赞赏。没有笔记本电脑我会离开一段时间(确切地说是15天),所以我可能无法回答并测试你的答案,但请放心,我会=)
提前致谢!
已编辑以包含错误日志(不确定如何格式化)
04-25 11:27:54.333: E/AndroidRuntime(21779): FATAL EXCEPTION: main
04-25 11:27:54.333: E/AndroidRuntime(21779): java.lang.NullPointerException
04-25 11:27:54.333: E/AndroidRuntime(21779): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1380)
04-25 11:27:54.333: E/AndroidRuntime(21779): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:430)
04-25 11:27:54.333: E/AndroidRuntime(21779): at android.os.Handler.handleCallback(Handler.java:615)
04-25 11:27:54.333: E/AndroidRuntime(21779): at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 11:27:54.333: E/AndroidRuntime(21779): at android.os.Looper.loop(Looper.java:213)
04-25 11:27:54.333: E/AndroidRuntime(21779): at android.app.ActivityThread.main(ActivityThread.java:4786)
04-25 11:27:54.333: E/AndroidRuntime(21779): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 11:27:54.333: E/AndroidRuntime(21779): at java.lang.reflect.Method.invoke(Method.java:511)
04-25 11:27:54.333: E/AndroidRuntime(21779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-25 11:27:54.333: E/AndroidRuntime(21779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-25 11:27:54.333: E/AndroidRuntime(21779): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我似乎已经解决了后退按钮和方向更改问题。
这里的技巧是不使用onDestroyView()
,而是改变处理片段的方式。而不是ft.attach(mFragment)
和ft.detach(mFragment)
,我将其更改为ft.show(mFragment)
和ft.hide(mFragment)
。
至于导致应用程序崩溃的方向更改,我相信(如果我错了,请纠正我),只要您没有其他横向布局,就可以将android:configChanges="orientation|screenSize"
添加到清单中这个活动,像这样:
<activity
android:theme="@style/AppTheme"
android:name="MyFragmentActivity"
android:label="@string/fragmentActivity"
android:configChanges="orientation|screenSize">
</activity>
答案 1 :(得分:0)
GoogleMap需要一点时间加载,因此如果您想使用它,它可能会返回null。 尝试在MapFragment的onActivityCreated()方法中移动此代码
map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
Marker amksc = map.addMarker(new MarkerOptions().position(mapLatLng).title("Map"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(mapLatLng, 18));
那曾经适合我。 请务必查看Fragment Lifecycle
答案 2 :(得分:0)
这对我有用..试试吧 下面的代码在onDestroy()方法中写入
@Override
public void onDestroy() {
if (fragment != null
&& getFragmentManager().findFragmentById(
fragment.getId()) != null) {
getFragmentManager().beginTransaction().remove(fragment)
.commit();
fragment = null;
}
}