我正在开发一个地图应用程序。一个活动有两个片段,其中一个是地图,另一个是onMarkerClick()
。 Fragment
中有文本字段,我需要在点击特定标记位置和信息的标记后对其进行更改。我的标记类:
public static BlankFragment newInstance(String param1, String param2,String param3,String param4,double param5,double param6) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString("addressLine", param1);
args.putString("countryName", param2);
args.putString("imageURL", param3);
args.putString("comment", param4);
args.putDouble("lat", param5);
args.putDouble("lng", param6);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
addressFromIntent=getArguments().getString("addressLine");
countryFromIntent=getArguments().getString("countryName");
commentFromIntent=getArguments().getString("comment");
photoPath=getArguments().getString("imageURL");
latFromIntent=getArguments().getDouble("lat", 0);
lngFromIntent=getArguments().getDouble("lng", 0);
addressView=(TextView)getActivity().findViewById(R.id.textView);
countryView=(TextView)getActivity().findViewById(R.id.textView2);
latlngView=(TextView)getActivity().findViewById(R.id.textView3);
saveButton=(Button)getView().findViewById(R.id.button);
saveButton.setText("Save");
imageView=(ImageView)getActivity().findViewById(R.id.imageView);
commentView=(EditText)getActivity().findViewById(R.id.editText);
final String latlngTextField=String.valueOf(latFromIntent)+", "+String.valueOf(lngFromIntent);
latlngView.setText(latlngTextField);
if (photoPath.equals("")) {
imageView.setImageResource(R.drawable.image);
} else {
imageView.setImageURI(Uri.parse(photoPath));
}
commentView.setText(commentFromIntent);
commentView.setHint("Enter comment here...");
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
Toast.makeText(getActivity().getApplicationContext(), photoPath, Toast.LENGTH_SHORT).show();
}
});
commentView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
commentModified++;
}
});
}
}
我的主要活动onMarkerClick()
方法:
@Override
public boolean onMarkerClick(Marker marker) {
CustomMarker customMarker = list.get(new LatLng(marker.getPosition().latitude,marker.getPosition().longitude));
getFragmentManager()
.beginTransaction()
.replace(R.id.map, BlankFragment.newInstance(customMarker.getAddressLine(),customMarker.getCountryName(),customMarker.getImageURL(),customMarker.getComment(), marker.getPosition().latitude,marker.getPosition().longitude))
.addToBackStack(null) // enables back key
.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK) // if you need transition
.commit();
我在尝试设置按钮文本时获取空指针(以及其他字段)。我该如何解决这个问题?
修改:行saveButton=(Button)getView().findViewById(R.id.button);
也无法与getActivity()
一起使用。