-1

ビューページにGoogleマップv2を実装するために2、3日以来試みています。NullPointerException エラーのみが発生します。私は何が間違っているのか分かりません。私が欲しいのは、2 つのフラグメント間で 2 回スワイプすることです。そのうちの 1 つがマップです。別のフラグメントは単なるテキストビューです (まだ重要ではありません)。

ここに私のコードがあります:

public class MainActivity extends FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

@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;
}}

FragmentPagerAdapter:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {

    if (position < 1) {
        Test_Fragment f = new Test_Fragment(position);
        return f;
    }
    else {
        Fragment_Map f = new Fragment_Map(position);
        return f;
    }
}

@Override
public int getCount() {
    // Show 2 total pages.
    return 2;
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
    case 0:
        return "Test Fragment";
    case 1:
        return "Map Fragment";
    }
    return null;
}
      }

MapFragment:

public class Fragment_Map extends Fragment {

GoogleMap mMap;

private int fragmentNR;

public Fragment_Map(int nr) {
    this.fragmentNR = nr;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

        if (mMap != null) {
            setUpMap();
        }
    }
    return (RelativeLayout) inflater.inflate(R.layout.fragment_map, container, false);
}


private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
    }

main.xml は次のようになります

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<android.support.v4.view.PagerTitleStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#33b5e5"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="#fff" />

    </android.support.v4.view.ViewPager>

そして最後に fragmentmap.xml

     <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

       </RelativeLayout>

logcat からのエラー メッセージに次のように表示されます。

     01-08 15:16:11.726: E/AndroidRuntime(781): FATAL EXCEPTION: main
     01-08 15:16:11.726: E/AndroidRuntime(781): java.lang.NullPointerException
     01-08 15:16:11.726: E/AndroidRuntime(781):  atswipe.views2.Fragment_Map.onCreateView(Fragment_Map.java:39)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.view.View.measure(View.java:15172)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.view.View.measure(View.java:15172)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.View.measure(View.java:15172)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.View.measure(View.java:15172)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.Choreographer.doFrame(Choreographer.java:525)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.os.Handler.handleCallback(Handler.java:615)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.os.Handler.dispatchMessage(Handler.java:92)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.os.Looper.loop(Looper.java:137)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.app.ActivityThread.main(ActivityThread.java:4745)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at java.lang.reflect.Method.invokeNative(Native Method)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at java.lang.reflect.Method.invoke(Method.java:511)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    01-08 15:16:11.726: E/AndroidRuntime(781): at    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    01-08 15:16:11.726: E/AndroidRuntime(781): at dalvik.system.NativeStart.main(Native Method)
    01-08 15:21:11.768: I/Process(781): Sending signal. PID: 781 SIG: 9

どうすればその問題を解決できますか? 何か案は?

前もって感謝します

4

2 に答える 2

0

マップを初期化する必要があります。私はこれをSherlockFragmentで使用しましたが、おそらくうまくいくでしょう。

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    mapView.onCreate(savedInstanceState);
    try {
        MapsInitializer.initialize(getActivity());
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }
    GoogleMap map=mapView.getMap();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View myFragmentView = inflater.inflate(R.layout.fragment_mapsearch, container, false);
    mapView = (MapView) myFragmentView.findViewById(R.id.mapSearchView);

    return myFragmentView;
}
于 2013-01-08T15:37:40.960 に答える