0

I have a choronometer in my app. Its working fine but i am unable to save the recorded time. Every time the user clicks on the stop button i want to capture the time and save it in the database and display it to the user in a ListView with all the past recorded time. I have have tried to implement the same but theres obviously some error in my code because the app crashes when the stopButton is clicked. Pls can anyone help me out The code i have written is as follows:

StudyTimeRecord:

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

        final View rootView = inflater.inflate(R.layout.fragment_studytimerecord, container, false);
        Bundle bundle = getArguments();
        mChapterName = bundle.getString("chapter_name");
        startButton = (Button) rootView.findViewById(R.id.start_button);
        stopButton = (Button) rootView.findViewById(R.id.stop_button);
        resetButton = (Button) rootView.findViewById(R.id.reset_button);
        studyWatch = (Chronometer) rootView.findViewById(R.id.chronometer);

        // the method for when we press the 'start' button
        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                studyWatch.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
                studyWatch.start();
                stopClicked = false;
                return;
            }
        });

        // the method for when we press the 'reset' button
        resetButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                studyWatch.setBase(SystemClock.elapsedRealtime());
                timeWhenStopped = 0;
                //   TextView secondsText = (TextView)getView().findViewById(R.id.hmsTekst);
                //  secondsText.setText("0 seconds");

            }
        });

        // the method for when we press the 'stop' button
        stopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                if (!stopClicked) {
                    // TextView secondsText = (TextView)getView().findViewById(R.id.hmsTekst);
                    timeWhenStopped = studyWatch.getBase() - SystemClock.elapsedRealtime();
                    int seconds = (int) timeWhenStopped / 1000;

                    //  secondsText.setText( Math.abs(seconds) + " seconds");
                    studyWatch.stop();
                    stopClicked = true;




                        String chronoText = studyWatch.getText().toString();
                        String array[] = chronoText.split(":");
                        if (array.length == 2) {
                            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
                                    + Integer.parseInt(array[1]) * 1000;
                        } else if (array.length == 3) {
                            stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
                                    + Integer.parseInt(array[1]) * 60 * 1000
                                    + Integer.parseInt(array[2]) * 1000;
                        }





                    final Dialog dialog = new Dialog(getActivity());
                    dialog.setContentView(R.layout.alert_dialog);
                    dialog.show();
                    dialog.findViewById(R.id.yesButton).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            DatabaseStudyLogs  dbStudylog = new DatabaseStudyLogs(getContext(),null,null,1);
                            StudyLog log = new StudyLog();
                            log.chaptername = mChapterName;
                            log.datetime =stoppedMilliseconds;
                            Log.d("yesbutton", mChapterName);

                            dbStudylog.addStudyLog(log);
                            dialog.dismiss();
                            Intent intent = new Intent(getActivity(),StudyTimeLogsActivity.class);
                            intent.putExtra("chapter_name", mChapterName);
                            intent.putExtra("date_time",mDateTime);
                            getActivity().startActivity(intent);
                            getActivity().finish();
                        }
                    });
                    dialog.findViewById(R.id.noButton).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();

                        }
                    });

                }
            }
        });

        setTitle();


        return rootView;

    }

StudyTimeLogsActivity

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_study_time_logs);
    mChapterName = getIntent().getStringExtra("chapter_name");
    mStudyTime=getIntent().getLongExtra("study_time",mStudyTime);
    DatabaseStudyLogs dbHandler = new DatabaseStudyLogs(getApplicationContext(), null, null, 1);
    Cursor cursor = dbHandler.getStudyLogCursor(mChapterName);
    Cursor cursor1 = dbHandler.getStudyLogCursorTime(mStudyTime);

    studylogChapter = (TextView)findViewById(R.id.studylogChapter);
    studylogTime =(TextView)findViewById(R.id.studylogTime);
    // TODO set description here
    mLogListView = (ListView)findViewById(R.id.logList);
    if (cursor != null && cursor.getCount() > 0)
    mLogListView.setAdapter(new LogAdapter(getApplicationContext(),cursor,0));

  //  mLogListView.setAdapter(new LogAdapter1(getApplicationContext(),cursor,0));
}

/**
 * Class to render logs
 */
class LogAdapter extends CursorAdapter {
    private TextView mLogDate;
    private TextView mLogChapter;

    public LogAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
    }

    public LogAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_log_list_item, parent, false);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        mLogDate = (TextView)view.findViewById(R.id.studylogTime);
        mLogDate.setText(cursor.getInt(cursor.getColumnIndex(DatabaseStudyLogs.COLUMN_DATE_TIME))+"");
        mLogChapter =(TextView)view.findViewById(R.id.studylogChapter);
        mLogChapter.setText(cursor.getString(cursor.getColumnIndex(DatabaseStudyLogs.COLUMN_CHAPTER_NAME)));
    }
}
class LogAdapter1 extends CursorAdapter {
    private TextView mLogDate;
    private TextView mLogChapter;

    public LogAdapter1(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
    }

    public LogAdapter1(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_log_list_item2, parent, false);

        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
      //  mLogDate = (TextView)view.findViewById(R.id.studylogDate);
        //mLogDate.setText(cursor.getInt(cursor.getColumnIndex(DatabaseStudyLogs.COLUMN_DATE_TIME))+"");
        mLogChapter =(TextView)view.findViewById(R.id.studylogChapter);
        mLogChapter.setText(cursor.getString(cursor.getColumnIndex(DatabaseStudyLogs.COLUMN_CHAPTER_NAME)));
    }
}

Error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.studypal.khadija.studypal/com.studypal..studypal.StudyTime.StudyTimeLogsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                                                                               at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:135)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
                                                                               at com.studypal.khadija.studypal.StudyTime.StudyTimeLogsActivity.onCreate(StudyTimeLogsActivity.java:30)
                                                                               at android.app.Activity.performCreate(Activity.java:5990)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                                               at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:135) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
Khadija Daruwala
  • 1,185
  • 3
  • 25
  • 54

0 Answers0