问题描述
我使用的是DrawerLayout,当我打开第二次一个谷歌地图片段的异常将被捕获的预期;然而,进一步在同样的方法,当我尝试
按钮btnShowList =(按钮)ui.findViewById(R.id.btnShowList);
和我得到一个空指针异常。我想这是因为用户界面
没有分配任何价值。
我该如何处理?
Page2.java:
公共类第二页扩展片段{
查看用户界面;
最终的字符串变量=波普;
最终诠释CODIGO_DE_RESULTADO_1 = 1; //居第usarlo EN onActivityResult();
私人GoogleMap的MMAP;
公共第2页(){
//埃斯特ES EL构造
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
如果(UI!= NULL){
ViewGroup中父=(ViewGroup中)ui.getParent();
如果(父!= NULL){
parent.removeView(UI);
}
}
尝试 {
UI = inflater.inflate(R.layout.activity_page2,集装箱,假);
}赶上(InflateException E){
Log.i(TAG,来这里的堆栈跟踪);
e.printStackTrace();
}
FragmentManager FM = getFragmentManager();
MMAP =((MapFragment)fm.findFragmentById(R.id.map))的GetMap()。
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(真正的);
按钮btnShowList =(按钮)ui.findViewById(R.id.btnShowList);
btnShowList.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
showFileslist();
}
});
返回UI;
}
}
activity_page2.xml:
< RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
的xmlns:机器人=http://schemas.android.com/apk/res/android>
<片段
机器人:ID =@ + ID /图
机器人:名称=com.google.android.gms.maps.MapFragment
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
<按钮
机器人:ID =@ + ID / btnShowList
风格=机器人:ATTR / buttonStyleSmall
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:文本=加载点/>
登录:
07-08 22:17:44.203:I /教皇(11859):来这里的堆栈跟踪
07-08 22:17:44.213:W / System.err的(11859):android.view.InflateException:二进制XML文件,6号线:错误充气类片段
07-08 22:17:44.213:W / System.err的(11859):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
07-08 22:17:44.213:W / System.err的(11859):在android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
07-08 22:17:44.213:W / System.err的(11859):在android.view.LayoutInflater.inflate(LayoutInflater.java:495)
07-08 22:17:44.213:W / System.err的(11859):在android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-08 22:17:44.213:W / System.err的(11859):在com.jorgeandresserrano.gpstools.Page2.onCreateView(Page2.java:52)
07-08 22:17:44.213:W / System.err的(11859):在android.app.Fragment.performCreateView(Fragment.java:1695)
07-08 22:17:44.213:W / System.err的(11859):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-08 22:17:44.213:W / System.err的(11859):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
07-08 22:17:44.213:W / System.err的(11859):在android.app.BackStackRecord.run(BackStackRecord.java:682)
07-08 22:17:44.213:W / System.err的(11859):在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
07-08 22:17:44.223:W / System.err的(11859):在android.app.FragmentManagerImpl $ 1.运行(FragmentManager.java:441)
07-08 22:17:44.223:W / System.err的(11859):在android.os.Handler.handleCallback(Handler.java:725)
07-08 22:17:44.223:W / System.err的(11859):在android.os.Handler.dispatchMessage(Handler.java:92)
07-08 22:17:44.223:W / System.err的(11859):在android.os.Looper.loop(Looper.java:137)
07-08 22:17:44.223:W / System.err的(11859):在android.app.ActivityThread.main(ActivityThread.java:5293)
07-08 22:17:44.223:W / System.err的(11859):在java.lang.reflect.Method.invokeNative(本机方法)
07-08 22:17:44.223:W / System.err的(11859):在java.lang.reflect.Method.invoke(Method.java:511)
07-08 22:17:44.223:W / System.err的(11859):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-08 22:17:44.223:W / System.err的(11859):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-08 22:17:44.223:W / System.err的(11859):在dalvik.system.NativeStart.main(本机方法)
07-08 22:17:44.223:W / System.err的(11859):java.lang.IllegalArgumentException异常:二进制XML文件,6号线:由造成重复ID 0x7f05003f,标记为空,或父ID为0xffffffff与另一个片段COM .google.android.gms.maps.MapFragment
07-08 22:17:44.223:W / System.err的(11859):在android.app.Activity.onCreateView(Activity.java:4863)
07-08 22:17:44.233:W / System.err的(11859):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
07-08 22:17:44.233:W / System.err的(11859):19 ...更多
07-08 22:17:44.233:D / AndroidRuntime(11859):关闭虚拟机
07-08 22:17:44.233:W / dalvikvm(11859):主题ID = 1:螺纹退出与未捕获的异常(组= 0x410feac8)
07-08 22:17:44.233:E / AndroidRuntime(11859):致命异常:主要
07-08 22:17:44.233:E / AndroidRuntime(11859):显示java.lang.NullPointerException
07-08 22:17:44.233:E / AndroidRuntime(11859):在com.myname.myapp.Page2.onCreateView(Page2.java:66)
07-08 22:17:44.233:E / AndroidRuntime(11859):在android.app.Fragment.performCreateView(Fragment.java:1695)
07-08 22:17:44.233:E / AndroidRuntime(11859):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-08 22:17:44.233:E / AndroidRuntime(11859):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
07-08 22:17:44.233:E / AndroidRuntime(11859):在android.app.BackStackRecord.run(BackStackRecord.java:682)
07-08 22:17:44.233:E / AndroidRuntime(11859):在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
07-08 22:17:44.233:E / AndroidRuntime(11859):在android.app.FragmentManagerImpl $ 1.运行(FragmentManager.java:441)
07-08 22:17:44.233:E / AndroidRuntime(11859):在android.os.Handler.handleCallback(Handler.java:725)
07-08 22:17:44.233:E / AndroidRuntime(11859):在android.os.Handler.dispatchMessage(Handler.java:92)
07-08 22:17:44.233:E / AndroidRuntime(11859):在android.os.Looper.loop(Looper.java:137)
07-08 22:17:44.233:E / AndroidRuntime(11859):在android.app.ActivityThread.main(ActivityThread.java:5293)
07-08 22:17:44.233:E / AndroidRuntime(11859):在java.lang.reflect.Method.invokeNative(本机方法)
07-08 22:17:44.233:E / AndroidRuntime(11859):在java.lang.reflect.Method.invoke(Method.java:511)
07-08 22:17:44.233:E / AndroidRuntime(11859):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-08 22:17:44.233:E / AndroidRuntime(11859):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-08 22:17:44.233:E / AndroidRuntime(11859):在dalvik.system.NativeStart.main(本机方法)
行66:
按钮btnShowList =(按钮)ui.findViewById(R.id.btnShowList);
这个问题可以通过临走的onDestroyView()方法去除地图片段解决。像这样的:
@覆盖
公共无效onDestroyView(){
super.onDestroyView();
MapFragment F =(MapFragment)getFragmentManager()findFragmentById(R.id.map)。
如果(F!= NULL){
。getFragmentManager()的BeginTransaction()删除(F).commit();
}
}
此文章解释这个更详细的http://stackoverflow.com/a/14484640/2422833
I'm using a DrawerLayout and when I load for the second time a google map fragment the exception will be caught as expected; however, further in the same method when I try
Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);
and I get a null pointer exception. I guess that's because ui
was not assigned any value.
How can I deal with this?
Page2.java:
public class Page2 extends Fragment {
View ui;
final String TAG = "POPE";
final int CODIGO_DE_RESULTADO_1 = 1; // Es para usarlo en onActivityResult();
private GoogleMap mMap;
public Page2(){
// Este es el constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (ui != null) {
ViewGroup parent = (ViewGroup) ui.getParent();
if (parent != null){
parent.removeView(ui);
}
}
try {
ui = inflater.inflate(R.layout.activity_page2,container,false);
} catch (InflateException e) {
Log.i(TAG,"HERE COMES THE STACK TRACE");
e.printStackTrace();
}
FragmentManager fm = getFragmentManager();
mMap = ((MapFragment) fm.findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(true);
Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);
btnShowList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileslist();
}
});
return ui;
}
}
activity_page2.xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnShowList"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Load points" />
Log:
07-08 22:17:44.203: I/POPE(11859): HERE COMES THE STACK TRACE
07-08 22:17:44.213: W/System.err(11859): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
07-08 22:17:44.213: W/System.err(11859): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
07-08 22:17:44.213: W/System.err(11859): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
07-08 22:17:44.213: W/System.err(11859): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
07-08 22:17:44.213: W/System.err(11859): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-08 22:17:44.213: W/System.err(11859): at com.jorgeandresserrano.gpstools.Page2.onCreateView(Page2.java:52)
07-08 22:17:44.213: W/System.err(11859): at android.app.Fragment.performCreateView(Fragment.java:1695)
07-08 22:17:44.213: W/System.err(11859): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-08 22:17:44.213: W/System.err(11859): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
07-08 22:17:44.213: W/System.err(11859): at android.app.BackStackRecord.run(BackStackRecord.java:682)
07-08 22:17:44.213: W/System.err(11859): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
07-08 22:17:44.223: W/System.err(11859): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
07-08 22:17:44.223: W/System.err(11859): at android.os.Handler.handleCallback(Handler.java:725)
07-08 22:17:44.223: W/System.err(11859): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 22:17:44.223: W/System.err(11859): at android.os.Looper.loop(Looper.java:137)
07-08 22:17:44.223: W/System.err(11859): at android.app.ActivityThread.main(ActivityThread.java:5293)
07-08 22:17:44.223: W/System.err(11859): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 22:17:44.223: W/System.err(11859): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 22:17:44.223: W/System.err(11859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-08 22:17:44.223: W/System.err(11859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-08 22:17:44.223: W/System.err(11859): at dalvik.system.NativeStart.main(Native Method)
07-08 22:17:44.223: W/System.err(11859): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f05003f, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
07-08 22:17:44.223: W/System.err(11859): at android.app.Activity.onCreateView(Activity.java:4863)
07-08 22:17:44.233: W/System.err(11859): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
07-08 22:17:44.233: W/System.err(11859): ... 19 more
07-08 22:17:44.233: D/AndroidRuntime(11859): Shutting down VM
07-08 22:17:44.233: W/dalvikvm(11859): threadid=1: thread exiting with uncaught exception (group=0x410feac8)
07-08 22:17:44.233: E/AndroidRuntime(11859): FATAL EXCEPTION: main
07-08 22:17:44.233: E/AndroidRuntime(11859): java.lang.NullPointerException
07-08 22:17:44.233: E/AndroidRuntime(11859): at com.myname.myapp.Page2.onCreateView(Page2.java:66)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.Fragment.performCreateView(Fragment.java:1695)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.BackStackRecord.run(BackStackRecord.java:682)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.os.Handler.handleCallback(Handler.java:725)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.os.Looper.loop(Looper.java:137)
07-08 22:17:44.233: E/AndroidRuntime(11859): at android.app.ActivityThread.main(ActivityThread.java:5293)
07-08 22:17:44.233: E/AndroidRuntime(11859): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 22:17:44.233: E/AndroidRuntime(11859): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 22:17:44.233: E/AndroidRuntime(11859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-08 22:17:44.233: E/AndroidRuntime(11859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-08 22:17:44.233: E/AndroidRuntime(11859): at dalvik.system.NativeStart.main(Native Method)
Line 66 is:
Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);
The problem can be resolved by removing the map fragment in the onDestroyView() method just before leaving. Like this:
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
if (f != null){
getFragmentManager().beginTransaction().remove(f).commit();
}
}
This post explains this more in detail http://stackoverflow.com/a/14484640/2422833
这篇关于在膨胀的看法加载第二次一个谷歌地图的片段时,空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!