我有一个导航抽屉,当我点击项目时,我打开了相应的片段..在一个项目中我创建了一个扩展FragmentActivity
而不是Fragment
的地图所以我认为这是原因崩溃..无论如何这是日志:
05-21 10:11:39.245: E/AndroidRuntime(22265): FATAL EXCEPTION: main
05-21 10:11:39.245: E/AndroidRuntime(22265): Process: com.tutecentral.navigationdrawer, PID: 22265
05-21 10:11:39.245: E/AndroidRuntime(22265): java.lang.NullPointerException
05-21 10:11:39.245: E/AndroidRuntime(22265): at com.tutecentral.navigationdrawer.MainActivity.SelectItem(MainActivity.java:264)
05-21 10:11:39.245: E/AndroidRuntime(22265): at com.tutecentral.navigationdrawer.MainActivity$DrawerItemClickListener.onItemClick(MainActivity.java:325)
05-21 10:11:39.245: E/AndroidRuntime(22265): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-21 10:11:39.245: E/AndroidRuntime(22265): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
05-21 10:11:39.245: E/AndroidRuntime(22265): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
05-21 10:11:39.245: E/AndroidRuntime(22265): at android.widget.AbsListView$3.run(AbsListView.java:3638)
05-21 10:11:39.245: E/AndroidRuntime(22265): at android.os.Handler.handleCallback(Handler.java:733)
05-21 10:11:39.245: E/AndroidRuntime(22265): at android.os.Handler.dispatchMessage(Handler.java:95)
05-21 10:11:39.245: E/AndroidRuntime(22265): at android.os.Looper.loop(Looper.java:136)
05-21 10:11:39.245: E/AndroidRuntime(22265): at android.app.ActivityThread.main(ActivityThread.java:5144)
05-21 10:11:39.245: E/AndroidRuntime(22265): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 10:11:39.245: E/AndroidRuntime(22265): at java.lang.reflect.Method.invoke(Method.java:515)
05-21 10:11:39.245: E/AndroidRuntime(22265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
这是项目部分中的导航抽屉onClick:
public void SelectItem(int possition) {
Fragment fragment = null;
FragmentActivity mapFragment = null;
Bundle args = new Bundle();
switch (possition) {
/*case 0:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
.get(possition).getImgResID());
break;*/
case 1:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
.get(possition).getImgResID());
break;
case 2:
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());
break;
case 3:
//-- Mappe e tratte
mapFragment = new mapFragment();
break;
case 4:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
.get(possition).getImgResID());
break;
case 5:
fragment = new UserProfile();
/*args.putString(UserProfile.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(UserProfile.IMAGE_RESOURCE_ID, dataList.get(possition)
.getImgResID());*/
/*Intent in = new Intent(MainActivity.this, UserProfile.class);
startActivity(in);*/
break;
case 6:
fragment = new FragmentThree();
args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
.get(possition).getImgResID());
break;
case 7:
fragment = new aboutFragment();
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(possition, true);
//mDrawerList.getChildAt(possition).setBackgroundResource(R.drawable.activated_background);
setTitle(dataList.get(possition).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);
}
在第264行中有:fragment.setArguments(args);
如果有必要,我也会发布地图片段:
public class mapFragment extends FragmentActivity {
// ...
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mapfragment, null, false);
InitialiseMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
//...
return v;
}
private void InitialiseMap() {
// TODO Auto-generated method stub
if (map == null) {
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (map != null) {
}
}
}
}
我该如何解决?