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");