2015-10-14 49 views
0

嗯,我問了一個問題,得到了90%的答案,我需要解決剩餘的10%。動態片段問題

這基本上是一個活動頁面,以便在點擊了從StackView選擇的項目,它打開其中有2個textViews和1個ImageView的, 所有這些頁面(10+)是對稱的,所以我決定到另一個片段使其變得動態,這樣就很容易處理和處理。

EventsFragment

public class EventsFragment extends android.support.v4.app.Fragment { 
    private StackView stackView; 
    private ArrayList<Stack_Items> list; 
    TypedArray eventLogo; 
    String eventName[],eventDesc[]; 

    @Nullable 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     eventLogo = getResources().obtainTypedArray(R.array.event_stack_icon); 
     eventName = getResources().getStringArray(R.array.event_stack); 
     eventDesc = getResources().getStringArray(R.array.event_desc_stack); 
     View view = inflater.inflate(R.layout.events_layout, null); 
     stackView = (StackView) view.findViewById(R.id.stackView1); 
     list = new ArrayList<Stack_Items>(); 

     //Adding items to the list 
     for (int i = 0; i < eventLogo.length(); i++) { 
      list.add(new Stack_Items(eventName[i], eventLogo.getResourceId(i, -1))); 
     } 
     //Calling adapter and setting it over stackView 
     Stack_Adapter adapter = new Stack_Adapter(getActivity().getApplicationContext(), list); 
     stackView.setAdapter(adapter); 
     stackView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> a, View 
        view, int position, long id) { 
       MainActivity mainActivity = (MainActivity) getActivity(); 
       FragmentTransaction fragmentTransaction = mainActivity.getSupportFragmentManager().beginTransaction(); 
       Fragment eventDescFragment = new DynamicEventsPage().newInstance(R.array.event_stack,R.array.event_desc_stack,eventLogo.getResourceId(position, -1)); 
       fragmentTransaction.replace(R.id.containerView, eventDescFragment); 
       fragmentTransaction.commit(); 
      } 
     }); 
     adapter.notifyDataSetChanged(); 
     return view; 
    } 
} 

DynamicEventPage

public class DynamicEventsPage extends Fragment{ 
    public static final String BUNDLE_STRING_KEY = "BUNDLE_STRING_KEY"; 
    public static final String BUNDLE_DRAWABLE_KEY = "BUNDLE_DRAWABLE_KEY"; 
    public static final String BUNDLE_STRING_DESC_KEY = "BUNDLE_STRING_DESC_KEY"; 

    public static Fragment newInstance(final int stringId,final int descStringId, final int drawableId) { 
     Bundle bundle = new Bundle(); 
     bundle.putInt(BUNDLE_STRING_KEY, stringId); 
     bundle.putInt(BUNDLE_DRAWABLE_KEY, drawableId); 
     bundle.putInt(BUNDLE_STRING_DESC_KEY,descStringId); 
     Fragment fragment = new DynamicEventsPage(); 
     fragment.setArguments(bundle); 
     return fragment; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.dynamic_events_page, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     Bundle bundle = getArguments(); 
     ((ImageView)view.findViewById(R.id.imageView)).setImageResource(bundle.getInt(BUNDLE_DRAWABLE_KEY)); 
     ((TextView)view.findViewById(R.id.textView)).setText(bundle.getInt(BUNDLE_STRING_KEY)); 
     ((TextView)view.findViewById(R.id.textViewdesc)).setText(bundle.getInt(BUNDLE_STRING_DESC_KEY)); 
    } 

} 

dynamic_events_page.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="1"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/textView" 
     android:layout_weight="0.40" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:id="@+id/textViewdesc" 
     android:layout_weight="0.40" /> 

    <ImageView 
     android:layout_width="228dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView" 
     android:layout_weight="0.40" /> 

</LinearLayout> 

得到的錯誤是

E/AndroidRuntime: android.content.res.Resources$NotFoundException: String resource ID #0x7f0b0001 

E/AndroidRuntime:  at package.DynamicEventsPage.onViewCreated(DynamicEventsPage.java:39) 

那麼是什麼修復?

+1

什麼是DynamicEventsPage.java:39? –

回答

3

變化的newInstance採取字符串,而不是INT

public static Fragment newInstance(final String eventName,final String eventDescr, final int drawableId) { 
     Bundle bundle = new Bundle(); 
     bundle.putString(BUNDLE_STRING_KEY, stringId); 
     bundle.putInt(BUNDLE_DRAWABLE_KEY, drawableId); 
     bundle.putString(BUNDLE_STRING_DESC_KEY,descStringId); 
     Fragment fragment = new DynamicEventsPage(); 
     fragment.setArguments(bundle); 
     return fragment; 
} 

和onViewCreate在onItemClick變化

Fragment eventDescFragment = new DynamicEventsPage().newInstance(R.array.event_stack,R.array.event_desc_stack,eventLogo.getResourceId(position, -1)); 

讀的不是int

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    Bundle bundle = getArguments(); 
    ((ImageView)view.findViewById(R.id.imageView)).setImageResource(bundle.getInt(BUNDLE_DRAWABLE_KEY)); 
    ((TextView)view.findViewById(R.id.textView)).setText(bundle.getString(BUNDLE_STRING_KEY)); 
    ((TextView)view.findViewById(R.id.textViewdesc)).setText(bundle.getString(BUNDLE_STRING_DESC_KEY)); 
} 

字符串

+0

你就像個天才男人。:3 ..我可以收到你的電子郵件嗎?請 。 –

+0

@AmanArora他是。 –

1

該錯誤消息顯示

資源$ NotFoundException:字符串資源ID#0x7f0b0001

這意味着您要參考一些字符串變量是不是有在strings.xml檔案。

希望這會有所幫助。

0

您正在將圖像視圖的文本分配給字符串數組R.array.somearray,這是錯誤的。您應該將其分配給字符串資源R.string.somestring

其他一些你的片段初始化錯誤。它應該是

Fragment eventDescFragment = DynamicEventsPage.newInstance(R.string.event_stack,R.string.event_desc_stack,eventLogo.getResourceId(position, -1)); 

你擁有它只是初始化一個新片段,離開它,然後初始化的靜態newInstance方法一個新的方式。