我有MapActivity& LocationSendActivity从片段扩展而来。 将MapActivity中的双类型数据传递给LocationSendActivity时,它会在MapActivity中显示错误“java.lang.NullPointerException”.Data
LocationSendActivity frag=new LocationSendActivity();
Bundle bnd1=new Bundle();
bnd1.putDouble("lat", latitude);
bnd1.putDouble("lng", longitude);
frag.setArguments(bnd1);
LocationActivity中的数据是
Bundle bnd=this.getArguments();
latt=bnd.getDouble("lat");
lngt=bnd.getDouble("lng")
MapActivity ..........
package com.example.map;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MapActivity extends Fragment {
private GoogleMap gmap;
public SupportMapFragment smf;
public double latitude,longitude;
public static View view;
private FragmentManager fm;
public boolean wifi,gps,network,val;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try{
view = inflater.inflate(R.layout.map, container, false);
try{
initializemap();
gmap.setMyLocationEnabled(true);
gmap.getUiSettings().setZoomControlsEnabled(true);
gmap.getUiSettings().setMyLocationButtonEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
//set map type
//gmap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Get latitude of the current location
latitude = myLocation.getLatitude();
// Get longitude of the current location
longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
gmap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Enable / Disable Compass icon
gmap.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
gmap.getUiSettings().setRotateGesturesEnabled(true);
//Set zooming control on goole to view location
gmap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
//Add marker at current position
MarkerOptions marker=new MarkerOptions();
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.arr));
//marker.position(latLng).anchor(1,1).rotation(90);
gmap.addMarker(marker.position(new LatLng(latitude, longitude)).title("You are here!"));
//Remove default marker
//gmap.clear();
}catch(Exception e){
e.printStackTrace();
}
}catch (InflateException e) {
// TODO: handle exception
}
//Dialog call
showDialog();
Log.e("latlng", Double.toString(latitude));
//Set data for LocationSendActivity
LocationSendActivity frag=new LocationSendActivity();
Bundle bnd1=new Bundle();
bnd1.putDouble("lat", latitude);
bnd1.putDouble("lng", longitude);
frag.setArguments(bnd1);
return view;
}
public void showDialog() {
//Check network
LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
try{
network = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch(Exception ex){}
//Check Wifi
WifiManager wfm =(WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
wifi = wfm.isWifiEnabled();
//Check Gps
try{
gps = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch(Exception ex){}
//call dialog class
val=network && wifi && gps;
if(!val){
DialogFragment dial =new AlertDialogFragment();
Bundle bnd=new Bundle();
bnd.putBoolean("net", network);
bnd.putBoolean("wi", wifi);
bnd.putBoolean("gps",gps);
dial.setArguments(bnd);
dial.show(getFragmentManager(),"dialogShow");
}
}
public void onResume(){
super.onResume();
initializemap();
}
private void initializemap() {
// TODO Auto-generated method stub
if(gmap == null){
fm=getFragmentManager();
Fragment ft=fm.findFragmentById(R.id.map);
smf=(SupportMapFragment)ft;
gmap=smf.getMap();
}
}
}
LocationSendActivity...........
package com.example.map;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class LocationSendActivity extends Fragment {
private double latt,lngt;
private TextView tv1,tv2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bnd=this.getArguments();
latt=bnd.getDouble("lat");
lngt=bnd.getDouble("lng");
}
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
super.onCreate(savedInstanceState);
View v=inflater.inflate(R.layout.locationsend, container, false);
/* tv1=(TextView)v.findViewById(R.id.textView3);
tv1.setText(String.valueOf(latt));
tv2=(TextView)v.findViewById(R.id.textView4);
tv2.setText(String.valueOf(lngt)); */
return v;
}
}
的logcat ........
11-30 12:46:34.238: W/System.err(5880): java.io.IOException: Permission denied
11-30 12:46:34.248: E/AndroidRuntime(5880): FATAL EXCEPTION: main
11-30 12:46:34.248: E/AndroidRuntime(5880): java.lang.NullPointerException
11-30 12:46:34.248: E/AndroidRuntime(5880): at com.example.map.LocationSendActivity.onCreate(LocationSendActivity.java:18)
11-30 12:46:34.248: E/AndroidRuntime(5880): at android.support.v4.app.Fragment.performCreate(Fragment.java:1455)
11-30 12:46:34.248: E/AndroidRuntime(5880): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
11-30 12:46:34.248: E/AndroidRuntime(5880): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-30 12:46:34.248: E/AndroidRuntime(5880): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-30 12:46:34.248: E/AndroidRuntime(5880): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
11-30 12:46:34.248: E/AndroidRuntime(5880): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
11-30 12:46:34.248: E/AndroidRuntime(5880): at android.os.Handler.handleCallback(Handler.java:587)
11-30 12:46:34.248: E/AndroidRuntime(5880): at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 12:46:34.248: E/AndroidRuntime(5880): at android.os.Looper.loop(Looper.java:123)
11-30 12:46:34.248: E/AndroidRuntime(5880): at android.app.ActivityThread.main(ActivityThread.java:4635)
11-30 12:46:34.248: E/AndroidRuntime(5880): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 12:46:34.248: E/AndroidRuntime(5880): at java.lang.reflect.Method.invoke(Method.java:521)
11-30 12:46:34.248: E/AndroidRuntime(5880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:916)
11-30 12:46:34.248: E/AndroidRuntime(5880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
11-30 12:46:34.248: E/AndroidRuntime(5880): at dalvik.system.NativeStart.main(Native Method)
11-30 12:46:34.258: I/global(5880): Default buffer size used in BufferedWriter constructor. It would be better to be explicit if an 8k-char buffer is required.