findFragmentById始终返回null

时间:2014-03-23 10:03:19

标签: android google-maps android-fragments android-activity google-maps-android-api-2

最初我开始制作一张地图来显示谷歌地图,方法是fragmentRelativeLayout内使用Unable to resume activity,它在前几次工作,但由于某种原因,它& #39; s停止工作并抛出import android.app.Fragment; import android.app.FragmentManager; import android.location.Location; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; public class MapActivity extends FragmentActivity { SessionManager session; // Google Map private GoogleMap googleMap; private Location userLocation = null; @Override protected void onStart() { session = new SessionManager(getApplicationContext()); session.checkLogin(); try { // Loading map initializeMap(); } catch (Exception e) { e.printStackTrace(); } super.onStart(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { public void run() { Toast.makeText(MapActivity.this, toast, Toast.LENGTH_SHORT).show(); } }); } /** * function to load map. If map is not created it will create it for you * */ private void initializeMap() { if (googleMap == null) { FragmentManager fm = getFragmentManager(); fm.executePendingTransactions(); // ---------v Here value of mf = null always MapFragment mf = (MapFragment) fm.findFragmentById(R.id.map); googleMap = mf.getMap(); // <-- This is where the error occurs // check if map is created successfully or not if (googleMap == null) { Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT) .show(); } } } @Override protected void onResume() { super.onResume(); Log.d("Update", "Inside onResume"); initializeMap(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.map, menu); return true; } @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. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public boolean authLogout(MenuItem item) { session.logoutUser(); return true; } /** * 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_map, container, false); return rootView; } } }

的错误

这是我的活动 -

fragment_map.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> 档案 -

activity_map.xml

Eclipse在生成空白活动时也创建了一个名为<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lokay2.MapActivity" tools:ignore="MergeRootFrame" /> 的文件 -

03-23 15:29:34.353: E/AndroidRuntime(8432): FATAL EXCEPTION: main
03-23 15:29:34.353: E/AndroidRuntime(8432): java.lang.RuntimeException: Unable to resume activity {com.example.lokay2/com.example.lokay2.MapActivity}: java.lang.NullPointerException
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2870)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2899)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.app.ActivityThread.access$600(ActivityThread.java:153)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.os.Looper.loop(Looper.java:137)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.app.ActivityThread.main(ActivityThread.java:5227)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at java.lang.reflect.Method.invoke(Method.java:511)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at dalvik.system.NativeStart.main(Native Method)
03-23 15:29:34.353: E/AndroidRuntime(8432): Caused by: java.lang.NullPointerException
03-23 15:29:34.353: E/AndroidRuntime(8432):     at com.example.lokay2.MapActivity.initializeMap(MapActivity.java:60)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at com.example.lokay2.MapActivity.onResume(MapActivity.java:75)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.app.Activity.performResume(Activity.java:5182)
03-23 15:29:34.353: E/AndroidRuntime(8432):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2860)
03-23 15:29:34.353: E/AndroidRuntime(8432):     ... 12 more

Logcat -

{{1}}

我遇到了同样问题的很多的问题。他们中的大多数建议使用SupportMapFragment。但是从this tutorial -

  

仅在您定位API 12及更高版本时才使用此课程。否则,请使用SupportMapFragment。

在大多数问题中,原因与其他问题不同。如果您要将其标记为重复,请好好考虑一下。

我定位API 17.有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的MapActivity没有布局,因此无法找到具有该ID的片段。在setContentView(R.layout.fragment_map)方法中致电onCreate()。还将fragment_map布局重命名为activity_map,因为您将其用于活动,所以更有意义。