Java 无法从“活动”到“片段”获取字符串。请看详情
我已经经历了很多问题,虽然这个问题似乎是重复的,但它不是 我想将2个字符串从一个活动传递到一个片段,但未能传递到以下错误:Java 无法从“活动”到“片段”获取字符串。请看详情,java,android,android-fragments,google-maps-api-3,Java,Android,Android Fragments,Google Maps Api 3,我已经经历了很多问题,虽然这个问题似乎是重复的,但它不是 我想将2个字符串从一个活动传递到一个片段,但未能传递到以下错误:java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String android.os.Bundle.getString(java.lang.String)'在com.abc.xyz.PostARequest$2$2.run(PostARequest.java:154)上的空对象引用上 下面是PostARequest.java(
java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String android.os.Bundle.getString(java.lang.String)'在com.abc.xyz.PostARequest$2$2.run(PostARequest.java:154)上的空对象引用上
下面是PostARequest.java
(片段)文件的代码:
public class PostARequest extends Fragment {
Bitmap bitmap;
ImageView hPic;
boolean hasDrawable;
EditText hsDescription;
TextView hPicTag, hLocationTag, currentCoordinatesTag, currentLat, currentLng;
LinearLayout latContainer, lngContainer;
Bundle b;
TabHost tabHost;
String latitude, longitude;
private OnFragmentInteractionListener mListener;
public PostARequest() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment PostARequest.
*/
// TODO: Rename and change types and number of parameters
public static PostARequest newInstance(String param1, String param2) {
PostARequest fragment = new PostARequest();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_post_a_request, container, false);
// check for location here
currentCoordinatesTag = (TextView) rootView.findViewById(R.id.currentCoordinatesTag);
latContainer = (LinearLayout) rootView.findViewById(R.id.latContainer);
currentLat = (TextView) rootView.findViewById(R.id.currentLat);
lngContainer = (LinearLayout) rootView.findViewById(R.id.lngContainer);
currentLng = (TextView) rootView.findViewById(R.id.currentLng);
currentCoordinatesTag.setVisibility(View.INVISIBLE);
latContainer.setVisibility(View.INVISIBLE);
currentLat.setVisibility(View.INVISIBLE);
lngContainer.setVisibility(View.INVISIBLE);
currentLng.setVisibility(View.INVISIBLE);
tabHost = new TabHost(getContext());
b = this.getArguments();
hLocationTag = (TextView) rootView.findViewById(R.id.h_location_tag);
hLocationTag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mapsIntent = new Intent(getContext(), MapsActivity.class);
startActivity(mapsIntent);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
homelessLocationTag.setVisibility(View.INVISIBLE);
}
}, 500);
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
currentCoordinatesTag.setVisibility(View.VISIBLE);
latContainer.setVisibility(View.VISIBLE);
currentLat.setVisibility(View.VISIBLE);
lngContainer.setVisibility(View.VISIBLE);
currentLng.setVisibility(View.VISIBLE);
latitude = b.getString("latitude");
longitude = b.getString("longitude");
currentLat.setText(latitude);
currentLng.setText(longitude);
tabHost.setCurrentTab(2);
}
}, 15000);
}
});
}
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
FloatingActionButton addLocationFab;
Double latitude, longitude;
LatLng userCurrentPosition;
String coordl1, coordl2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
addLocationFab = (FloatingActionButton) findViewById(R.id.addLocationFAB);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
googleMap.setMyLocationEnabled(true);
addLocationFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
latitude = googleMap.getMyLocation().getLatitude();
longitude = googleMap.getMyLocation().getLongitude();
coordl1 = latitude.toString();
coordl2 = longitude.toString();
Log.d("latitude", coordl1);
Log.d("longitude", coordl2);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Bundle bundle = new Bundle();
bundle.putString("latitude", coordl1);
bundle.putString("longitude", coordl2);
PostARequest postARequest = new PostARequest();
postARequest.setArguments(bundle);
}
}, 5000);
}
});
}
}
下面是MapsaActivity.java
(活动)文件的代码:
public class PostARequest extends Fragment {
Bitmap bitmap;
ImageView hPic;
boolean hasDrawable;
EditText hsDescription;
TextView hPicTag, hLocationTag, currentCoordinatesTag, currentLat, currentLng;
LinearLayout latContainer, lngContainer;
Bundle b;
TabHost tabHost;
String latitude, longitude;
private OnFragmentInteractionListener mListener;
public PostARequest() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment PostARequest.
*/
// TODO: Rename and change types and number of parameters
public static PostARequest newInstance(String param1, String param2) {
PostARequest fragment = new PostARequest();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_post_a_request, container, false);
// check for location here
currentCoordinatesTag = (TextView) rootView.findViewById(R.id.currentCoordinatesTag);
latContainer = (LinearLayout) rootView.findViewById(R.id.latContainer);
currentLat = (TextView) rootView.findViewById(R.id.currentLat);
lngContainer = (LinearLayout) rootView.findViewById(R.id.lngContainer);
currentLng = (TextView) rootView.findViewById(R.id.currentLng);
currentCoordinatesTag.setVisibility(View.INVISIBLE);
latContainer.setVisibility(View.INVISIBLE);
currentLat.setVisibility(View.INVISIBLE);
lngContainer.setVisibility(View.INVISIBLE);
currentLng.setVisibility(View.INVISIBLE);
tabHost = new TabHost(getContext());
b = this.getArguments();
hLocationTag = (TextView) rootView.findViewById(R.id.h_location_tag);
hLocationTag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mapsIntent = new Intent(getContext(), MapsActivity.class);
startActivity(mapsIntent);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
homelessLocationTag.setVisibility(View.INVISIBLE);
}
}, 500);
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
currentCoordinatesTag.setVisibility(View.VISIBLE);
latContainer.setVisibility(View.VISIBLE);
currentLat.setVisibility(View.VISIBLE);
lngContainer.setVisibility(View.VISIBLE);
currentLng.setVisibility(View.VISIBLE);
latitude = b.getString("latitude");
longitude = b.getString("longitude");
currentLat.setText(latitude);
currentLng.setText(longitude);
tabHost.setCurrentTab(2);
}
}, 15000);
}
});
}
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
FloatingActionButton addLocationFab;
Double latitude, longitude;
LatLng userCurrentPosition;
String coordl1, coordl2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
addLocationFab = (FloatingActionButton) findViewById(R.id.addLocationFAB);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
googleMap.setMyLocationEnabled(true);
addLocationFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
latitude = googleMap.getMyLocation().getLatitude();
longitude = googleMap.getMyLocation().getLongitude();
coordl1 = latitude.toString();
coordl2 = longitude.toString();
Log.d("latitude", coordl1);
Log.d("longitude", coordl2);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Bundle bundle = new Bundle();
bundle.putString("latitude", coordl1);
bundle.putString("longitude", coordl2);
PostARequest postARequest = new PostARequest();
postARequest.setArguments(bundle);
}
}, 5000);
}
});
}
}
我真的不知道这里出了什么问题
请让我知道
若问题状态不好,请配合,我仍在学习发布好的问题。您并没有在片段中调用newInstance()
使用
在片段中设置这些全局变量:
this.latitude = latitude;
this.longitude = longitude;
尝试初始化字符串以将数据从活动传递到片段:将此代码放在任意位置Bundle=new Bundle();bundle.putString(“KEY_NAME1”、“Abrakadabra”);bundle.putString(“KEY_NAME2”、“Kadabra”);MyFragment MyFragment=新的MyFragment();setArguments(bundle);然后在片段的onCreateView方法中添加此代码束args=getArguments();String stringdata1=args.getString(“KEY_NAME1”);String stringdata2=args.getString(“KEY_NAME2”);我注意到的一件事是,您试图通过bundle将数据发送到再次创建的相同片段。我必须做同样的一次,因为我必须销毁/删除片段的前一个实例,然后执行您所做的过程。在调用函数发送数据之前,只需删除片段。或者,如果您不想销毁该片段,则必须使用我上面回答的界面。@suku您能简单回答一下吗?描述整个过程,整个事情。如何在没有任何错误的情况下将字符串从活动获取到片段。请问,我应该在哪里加这个?