0

在我的片段中,名为locate_map我声明了一个字符串数组并将其转换为String ArrayList。我希望ArrayList传递给我的另一个名为locate_updatedstatus的片段。我有我的locate_map片段的按钮具有ID to_update_status会从locate_map切换到locate_updatedstatus同时会通过我的ArrayList和显示它作为我的locate_updatedstatusandroid - 在片段之间传递数组无效

一个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) 
+0

什么是logcat的错误获取数据? – chirag90

+0

我现在用logcat中的erorr更新了这个问题 –

回答

0

所有你需要的是bundle.putStringArray

String[] updatedArr = {"display this", "also this"}; 
bundle.putStringArray(KEY, updatedArr) 

bundle.getStringArray(KEY)

在你的代码,您不添加Bundle作为参数传递给Fragment 请使用link修复类构造或

0

下面的代码只是创建捆绑并添加数组列表。但没有具体说明它将被使用的地方。

List<String> stringList = new ArrayList<String>(Arrays.asList(updatedArr)); 
    Bundle bundle = new Bundle(); 
    bundle.putSerializable("key", (Serializable) stringList); 

解决方案:希望第一个locate_login和然后locate_updatedstatus片段将按顺序创建。

locate_login片段:试图把内容的活动意图

List<String> stringList = new ArrayList<String>(Arrays.asList(updatedArr)); 
Bundle bundle = new Bundle(); 
bundle.putSerializable("key", (Serializable) stringList); 
getActivity().getIntent().putExtras("key",bundle); 

locate_upadtestatus片段:从活动的意图

ArrayList<String> stringList = (ArrayList<String>)getActivity().getIntent().getExtras("key");