ListView in Fragment with adapter - can't add new objects - FATAL EXCEPTION

0

I'm creating an app with a tab layout consisting of three fragments. In one of these 3 Fragments I would like to have a ListView. And I would like to add ToSave objects to the ListView. I tried to add objects to the ListView from another Fragment using interface and following lines:

ToSave toSave = new ToSave();
HistoryFragment.adapter.add(toSave);
HistoryFragment.adapter.notifyDataSetChanged();

but I am still getting FATAL EXCEPTION: main when swiping to this tab. Even if I put:

ToSave toSave = new ToSave();
adapter.add(toSave);

inside onCreateView in HistoryFragment.java I'm getting the same error.

My code is below. I've already implemented adapter (don't know if correctly):

HistoryFragment.java

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;

    }
}

ToSave.java

public class ToSave {

    public int numberBooks;
    public int numberPages;


    public ToSave() {

        numberBooks = 500;
        numberPages = 10000;
    }
}

HistoryAdapter.java

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;

    }
}

fragment_history.xml

<?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>

history_item.xml

<?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>

I've tried to solve the problem by myself for many hours, but didn't find any solution.

Many thanks for your help!!!

EDIT

logcat:

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)
android
listview
android-activity
android-fragments
android-listview
asked on Stack Overflow Aug 6, 2014 by fragon • edited Aug 6, 2014 by fragon

1 Answer

1

change these lines

    tvBooks.setText(record.numberBooks);
    tvPages.setText(record.numberPages);

to

    tvBooks.setText(Integer.valueOf(record.numberBooks).toString());
    tvPages.setText(Integer.valueOf(record.numberPages).toString());
answered on Stack Overflow Aug 6, 2014 by mmlooloo • edited Aug 6, 2014 by mmlooloo

User contributions licensed under CC BY-SA 3.0