我一直试图让我的漂亮的基本地图应用程序与jfeinstein10的SlidingMenu一起使用,并且每次尝试获取地图时都会收到NullPointerException。在我尝试实现SlidingMenu之前它工作得很好,所以当我将MainActivity从Activity切换到来自jfeinstein10的SlidingMenuExample的BaseActivity时,我必须搞砸了。我的MainActivity&#39; onCreate看起来像这样< / p>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
if (mContent == null)
mContent = new MainView();
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, mContent).commit();
setBehindContentView(R.layout.menu_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame, new RandomList()).commit();
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setSlidingActionBarEnabled(true);
setUpMapIfNeeded();
//testMap();
}
private void setUpMapIfNeeded(){
if (map == null) {
FragmentManager fmanager = getSupportFragmentManager();
Fragment fragment = fmanager.findFragmentById(R.id.map);
SupportMapFragment supportmapfragment = (SupportMapFragment) fragment;
map = supportmapfragment.getMap();
if (map != null) {
map.setMapType(MapOptions.getMapType());
map.getUiSettings().setAllGesturesEnabled(true);
map.setMyLocationEnabled(true);
if(MapOptions.getCenterPoint() != null) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(MapOptions.getCenterPoint().getPosition(), 14));
}
}
}
Thiis调用MainView类,如下所示,
public class MainView extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main, container, false);
}
}
我的MainActivity扩展了我在这里粘贴的BaseActivity,
public class BaseActivity extends SlidingFragmentActivity {
private int mTitleRes;
protected ListFragment mFrag;
public BaseActivity(int titleRes){
mTitleRes = titleRes;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(mTitleRes);
setBehindContentView(R.layout.menu_frame);
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
mFrag = new RandomList();
ft.replace(R.id.menu_frame, mFrag);
ft.commit();
final SlidingMenu sm = getSlidingMenu();
sm.setShadowWidth(15);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffset(60);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
sm.setBehindCanvasTransformer(new CanvasTransformer(){
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
public void transformCanvas(Canvas canvas, float percentOpen) {
// TODO Auto-generated method stub
boolean layer = percentOpen > 0.0f && percentOpen < 1.0f;
int layerType = layer ? View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE;
if (layerType != sm.getContent().getLayerType()){
sm.getContent().setLayerType(layerType, null);
sm.getMenu().setLayerType(layerType, null);
}
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
toggle();
return true;
}
return onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class BasePagerAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragments = new ArrayList<Fragment>();
private ViewPager mPager;
public BasePagerAdapter(FragmentManager fm, ViewPager vp){
super(fm);
mPager = vp;
mPager.setAdapter(this);
for (int i = 0; i < 3; i++){
addTab(new RandomList());
}
}
public void addTab(Fragment frag){
mFragments.add(frag);
}
@Override
public Fragment getItem(int position){
return mFragments.get(position);
}
@Override
public int getCount(){
return mFragments.size();
}
}
}
我的activity_main的xml文件是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
最后,这是我在LogCat中遇到的错误。关于如何解决这个问题的任何想法?
03-26 16:13:54.427: E/AndroidRuntime(18720): FATAL EXCEPTION: main
03-26 16:13:54.427: E/AndroidRuntime(18720): Process: com.example.kennai, PID: 18720
03-26 16:13:54.427: E/AndroidRuntime(18720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kennai/com.example.kennai.MainActivity}: java.lang.NullPointerException
03-26 16:13:54.427: E/AndroidRuntime(18720): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-26 16:13:54.427: E/AndroidRuntime(18720): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-26 16:13:54.427: E/AndroidRuntime(18720): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-26 16:13:54.427: E/AndroidRuntime(18720): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-26 16:13:54.427: E/AndroidRuntime(18720): at android.os.Handler.dispatchMessage(Handler.java:102)
03-26 16:13:54.427: E/AndroidRuntime(18720): at android.os.Looper.loop(Looper.java:136)
03-26 16:13:54.427: E/AndroidRuntime(18720): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-26 16:13:54.427: E/AndroidRuntime(18720): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 16:13:54.427: E/AndroidRuntime(18720): at java.lang.reflect.Method.invoke(Method.java:515)
03-26 16:13:54.427: E/AndroidRuntime(18720): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-26 16:13:54.427: E/AndroidRuntime(18720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-26 16:13:54.427: E/AndroidRuntime(18720): at dalvik.system.NativeStart.main(Native Method)
03-26 16:13:54.427: E/AndroidRuntime(18720): Caused by: java.lang.NullPointerException
03-26 16:13:54.427: E/AndroidRuntime(18720): at com.example.kennai.MainActivity.setUpMapIfNeeded(MainActivity.java:197)
03-26 16:13:54.427: E/AndroidRuntime(18720): at com.example.kennai.MainActivity.onCreate(MainActivity.java:74)
03-26 16:13:54.427: E/AndroidRuntime(18720): at android.app.Activity.performCreate(Activity.java:5231)
03-26 16:13:54.427: E/AndroidRuntime(18720): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-26 16:13:54.427: E/AndroidRuntime(18720): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-26 16:13:54.427: E/AndroidRuntime(18720): ... 11 more
谢谢, 尼克