好吧,我问了一个问题并得到了90%的答案,我需要解决其余的10%。
它基本上是一个事件页面,所以在从StackView点击所选项目时,它会打开另一个片段,里面有2个textViews和1个imageView, 所有这些页面(10+)都是对称的,所以我决定让它变得动态,这样它就很容易处理和处理。
EventsFragment
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));
}
}
DynamicEventPage
<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>
dynamic_events_page.xml
E/AndroidRuntime: android.content.res.Resources$NotFoundException: String resource ID #0x7f0b0001
E/AndroidRuntime: at package.DynamicEventsPage.onViewCreated(DynamicEventsPage.java:39)
错误是获取
{{1}}
那么修复是什么?
答案 0 :(得分:3)
将newInstance更改为String而不是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读取String而不是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));
}
onItemClick中的更改
Fragment eventDescFragment = new DynamicEventsPage().newInstance(R.array.event_stack,R.array.event_desc_stack,eventLogo.getResourceId(position, -1));
与
String name = (position < eventName.length) ? eventName[position] : "";
String description = (position < eventDesc.length) ? eventDesc[position] : "";
Fragment eventDescFragment = DynamicEventsPage.newInstance(eventName, description,eventLogo.getResourceId(position, -1));
答案 1 :(得分:1)
显示错误消息
Resources $ NotFoundException:字符串资源ID#0x7f0b0001
这意味着您正在尝试引用strings.xml文件中没有的字符串变量。
希望这会有所帮助。
答案 2 :(得分:0)
您正在将图像视图的文本分配给字符串数组R.array.somearray
,这是错误的。您应该将其分配给字符串资源R.string.somestring
。
Fragment初始化中的其他一些错误。它应该是
Fragment eventDescFragment = DynamicEventsPage.newInstance(R.string.event_stack,R.string.event_desc_stack,eventLogo.getResourceId(position, -1));
你拥有它的方法就是初始化一个新的Fragment并保留它然后在静态newInstance
方法上初始化一个新的。