我正在创建一个带有三个片段的标签布局的应用。在这3个片段中的一个中,我想要一个ListView。我想将ToSave对象添加到ListView。我尝试使用接口和以下行从另一个Fragment向ListView添加对象:
ToSave toSave = new ToSave();
HistoryFragment.adapter.add(toSave);
HistoryFragment.adapter.notifyDataSetChanged();
但是当我滑动到此标签时,我仍然会FATAL EXCEPTION: main
。即使我把:
ToSave toSave = new ToSave();
adapter.add(toSave);
在 HistoryFragment.java 中的onCreateView
内我收到同样的错误。
我的代码如下。我已经实现了适配器(不知道是否正确):
public class HistoryFragment extends Fragment {
ListView listView;
HistoryAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View history = inflater.inflate(R.layout.fragment_history, container, false);
ArrayList<ToSave> arrayOfData = new ArrayList<ToSave>();
adapter = new HistoryAdapter(getActivity().getBaseContext(), arrayOfData);
listView = (ListView) history.findViewById (R.id.listView1);
listView.setAdapter(adapter);
ToSave toSave = new ToSave();
adapter.add(toSave);
return history;
}
}
public class ToSave {
public int numberBooks;
public int numberPages;
public ToSave() {
numberBooks = 500;
numberPages = 10000;
}
}
public class HistoryAdapter extends ArrayAdapter<ToSave>{
public HistoryAdapter(Context context, ArrayList<ToSave> records) {
super(context, R.layout.history_item, records);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ToSave record = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.history_item, parent, false);
}
TextView tvBooks = (TextView) convertView.findViewById(R.id.textViewBooks);
TextView tvPages = (TextView) convertView.findViewById(R.id.textViewPages);
// Populate the data into the template view using the data object
tvBooks.setText(record.numberBooks);
tvPages.setText(record.numberPages);
// Return the completed view to render on screen
return convertView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="#E3F6CE">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewBooks"
android:paddingLeft="2dp"
android:textSize="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date" />
<TextView
android:id="@+id/textViewPages"
android:paddingLeft="5dp"
android:textSize="17dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data" />
</LinearLayout>
我试图自己解决这个问题好几个小时,但没有找到任何解决方案。
非常感谢你的帮助!!!
08-06 19:02:44.295: D/OpenGLRenderer(26412): Enabling debug mode 0
08-06 19:02:44.355: I/ActivityManager(26412): Timeline: Activity_idle id: android.os.BinderProxy@44ff1e10 time:112732348
08-06 19:02:45.445: W/ResourceType(26412): No package identifier when getting value for resource number 0x000001f4
08-06 19:02:45.445: D/AndroidRuntime(26412): Shutting down VM
08-06 19:02:45.445: W/dalvikvm(26412): threadid=1: thread exiting with uncaught exception (group=0x41e5cd88)
08-06 19:02:45.455: E/AndroidRuntime(26412): FATAL EXCEPTION: main
08-06 19:02:45.455: E/AndroidRuntime(26412): Process: com.example.booker, PID: 26412
08-06 19:02:45.455: E/AndroidRuntime(26412): android.content.res.Resources$NotFoundException: String resource ID #0x1f4
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.content.res.Resources.getText(Resources.java:252)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.widget.TextView.setText(TextView.java:3905)
08-06 19:02:45.455: E/AndroidRuntime(26412): at com.example.booker.HistoryAdapter.getView(HistoryAdapter.java:27)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.widget.ListView.onMeasure(ListView.java:1175)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.View.measure(View.java:16628)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.View.measure(View.java:16628)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.View.measure(View.java:16628)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.View.measure(View.java:16628)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.View.measure(View.java:16628)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-06 19:02:45.455: E/AndroidRuntime(26412): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.View.measure(View.java:16628)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-06 19:02:45.455: E/AndroidRuntime(26412): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2298)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.View.measure(View.java:16628)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5622)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.Choreographer.doFrame(Choreographer.java:544)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.os.Handler.handleCallback(Handler.java:733)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.os.Handler.dispatchMessage(Handler.java:95)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.os.Looper.loop(Looper.java:212)
08-06 19:02:45.455: E/AndroidRuntime(26412): at android.app.ActivityThread.main(ActivityThread.java:5135)
08-06 19:02:45.455: E/AndroidRuntime(26412): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 19:02:45.455: E/AndroidRuntime(26412): at java.lang.reflect.Method.invoke(Method.java:515)
08-06 19:02:45.455: E/AndroidRuntime(26412): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
08-06 19:02:45.455: E/AndroidRuntime(26412): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
08-06 19:02:45.455: E/AndroidRuntime(26412): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
更改这些行
tvBooks.setText(record.numberBooks);
tvPages.setText(record.numberPages);
到
tvBooks.setText(Integer.valueOf(record.numberBooks).toString());
tvPages.setText(Integer.valueOf(record.numberPages).toString());