在我的片段中,名为locate_map
我声明了一个字符串数组并将其转换为String ArrayList。我希望ArrayList传递给我的另一个名为locate_updatedstatus
的片段。我有我的locate_map
片段的按钮具有ID to_update_status
会从locate_map
切换到locate_updatedstatus
同时会通过我的ArrayList和显示它作为我的locate_updatedstatus
android - 在片段之间传递数组无效
一个ListView有在BULDING没有错误。然而,每当我导航到我的locate_updatedstatus
应用程序崩溃。
主要活动。这是我如何在片段之间切换。我使用的导览活动与容器片段
public void onSelectFragment(View v){
Fragment newFragment;
if(v == findViewById(R.id.to_update_status)){
newFragment = new locate_updatedstatus();
}
else{
newFragment = new locate_login();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
locate_map片段
public class locate_map extends Fragment implements Serializable{
public locate_map() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_locate_map, container, false);
String[] updatedArr = {"display this", "also this"};
List<String> stringList = new ArrayList<String>(Arrays.asList(updatedArr));
Bundle bundle = new Bundle();
bundle.putSerializable("key", (Serializable) stringList);
return rootView;
}
}
locate_map XML按钮
<Button
android:id="@+id/to_update_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/check_images"
android:layout_marginTop="15dp"
android:text="Update Status"
android:width="340dp"
android:height="70dp"
android:onClick="onSelectFragment"
/>
locate_updatedstatus片段
public class locate_updatedstatus extends Fragment implements Serializable {
public locate_updatedstatus() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_locate_updatedstatus, container, false);
ArrayList<String> stringList = (ArrayList<String>)getArguments().getSerializable("key");
ListView listSource = (ListView) rootView.findViewById(R.id.updates);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
stringList
);
listSource.setAdapter(listViewAdapter);
return rootView; // Inflate the layout for this fragment
}
}
LOCA te_updatedstatus XML列表视图
<ListView
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_marginTop="60dp"
android:id="@+id/updates"
android:listSelector="@android:color/transparent"
/>
如果答案是详细我将不胜感激。我是Android编程的初学者,请耐心等待。
logcat的错误
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference at com.example.makati.sidebar.locate_updatedstatus.onCreateView(locate_updatedstatus.java:31)
什么是logcat的错误获取数据? – chirag90
我现在用logcat中的erorr更新了这个问题 –