无法从活动到片段获取字符串。请看细节

时间:2016-01-10 19:51:02

标签: java android android-fragments google-maps-api-3

我经历了很多问题,虽然这个问题似乎重复,但事实并非如此!

我想将一个活动中的两个字符串传递给一个片段,但我没有这样做会出现以下错误:In [1]: solve(10, 100) Out[1]: 250639233987229485923025924628548154758061157 In [2]: %timeit solve(10, 100) 1000 loops, best of 3: 964 µs per loop

这是java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at 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); } }); } (活动)文件的代码:

MapsActivity.java

我真的不知道这里有什么问题。

请告诉我。

如果问题不完整,请合作,我还在学习发表好问题。

1 个答案:

答案 0 :(得分:0)

你没有在Fragment中调用newInstance()。

使用

PostFragment.newInstance(latitude, longitude);

在Fragment中设置那些全局变量:

this.latitude = latitude;
this.longitude = longitude;