DrawerLayout を使用していますが、Google マップ フラグメントを 2 回目にロードすると、期待どおりに例外がキャッチされます。ただし、さらに同じ方法で試してみると
Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);
ヌルポインター例外が発生します。ui
値が割り当てられていないためだと思います。
どうすればこれに対処できますか?
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" />
ログ:
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)
66 行目は次のとおりです。
Button btnShowList = (Button) ui.findViewById(R.id.btnShowList);