Question

I am trying to run a Chronometer in a fragment and one of my textview on clicks is getting a NPE. Its probably something really easy that I am overlooking but I cant seem to figure it out.

import android.app.Fragment;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.TextView;

public class TimersCountdownFragment extends Fragment {

    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;
    TextView countdownStart;
    TextView countdownStop;
    Chronometer countdownChronometer;
    boolean isChronometerRunning;

    public TimersCountdownFragment(){}

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

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }
        View view = inflater.inflate(R.layout.fragment_timer_countdown, container, false);

        countdownStart = (TextView) getActivity().findViewById(R.id.timerStartDown);
        countdownStop = (TextView) getActivity().findViewById(R.id.timerStopDown);
        countdownChronometer = (Chronometer) getActivity().findViewById(R.id.chronometerDown);
        //countdownStop.setAlpha(50);

        countdownStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isChronometerRunning) {
                    isChronometerRunning = true;
                    countdownChronometer.start();
                    countdownStop.setText("Stop");
                    //countdownStart.setAlpha(50);
                    //countdownStop.setAlpha(100);
                }

            }
        });

        countdownStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isChronometerRunning){
                    countdownChronometer.stop();
                    countdownStop.setText("Reset");
                    isChronometerRunning = false;
                    countdownStart.setAlpha(100);
                } else {
                    countdownChronometer.setBase(SystemClock.elapsedRealtime());
                    countdownStop.setText("Stop");
                    isChronometerRunning = false;
                    //countdownStart.setAlpha(100);
                    //countdownStop.setAlpha(50);
                }


            }
        });

        // Inflate the layout for this fragment
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();


    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current article selection in case we need to recreate the fragment
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}

LogCat

05-08 16:02:40.490: E/AndroidRuntime(31992): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rcd.mypr/com.rcd.mypr.Timers.TimersActivity}: java.lang.NullPointerException
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.os.Looper.loop(Looper.java:136)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread.main(ActivityThread.java:5102)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at java.lang.reflect.Method.invoke(Method.java:515)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at dalvik.system.NativeStart.main(Native Method)
05-08 16:02:40.490: E/AndroidRuntime(31992): Caused by: java.lang.NullPointerException
05-08 16:02:40.490: E/AndroidRuntime(31992):    at com.rcd.mypr.Timers.TimersCountdownFragment.onCreateView(TimersCountdownFragment.java:45)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.Fragment.performCreateView(Fragment.java:1700)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.Activity.performStart(Activity.java:5257)
05-08 16:02:40.490: E/AndroidRuntime(31992):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2182)
05-08 16:02:40.490: E/AndroidRuntime(31992):    ... 11 more

Some extra eyes and any help would be appreciated.

Thanks

EDIT:

XML Layout fragment_timer_countdown.xml

<?xml version="1.0" encoding="utf-8"?>

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

    <Chronometer
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/chronometerDown"
        android:layout_gravity="center_horizontal"
        android:textSize="100dp"
        android:layout_weight="1"
        android:gravity="center"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_height="2dip"
        android:layout_width="fill_parent"
        android:background="@color/red"
        android:layout_marginBottom="5dp"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Start"
            android:id="@+id/timerStartDown"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:textSize="50dp"/>

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_weight="0.02"
            android:background="#50ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Stop"
            android:id="@+id/timerStopDown"
            android:layout_weight="1"
            android:gravity="center"
            android:textAlignment="center"
            android:layout_gravity="bottom"
            android:textSize="50dp"/>
    </LinearLayout>


</LinearLayout>
Was it helpful?

Solution

countdownStop is null. findViewById in here:

countdownStop = (TextView) getActivity().findViewById(R.id.timerStopDown);

haven't found R.id.timerStopDown in your TimersCountdownFragment layout.

Maybe you meant view.findViewById(R.id.timerStopDown)?

OTHER TIPS

i think you should use like following way.

View view = inflater.inflate(R.layout.fragment_timer_countdown, container, false);
countdownStart = (TextView) view.findViewById(R.id.timerStartDown);
countdownStop = (TextView) view.findViewById(R.id.timerStopDown);
countdownChronometer = (Chronometer)view.findViewById(R.id.chronometerDown);

are you having these ids in the activity which holds this fragment ?

    R.id.timerStartDown, 
    R.id.timerStopDown, 
    R.id.chronometerDown

Else if it thr in ur R.layout.fragment_timer_countdown, try this

countdownStart = (TextView) view.findViewById(R.id.timerStartDown);
countdownStop = (TextView) view.findViewById(R.id.timerStopDown);
countdownChronometer = (Chronometer) view.findViewById(R.id.chronometerDown);

since u r inflating the layout in view as

View view = inflater.inflate(R.layout.fragment_timer_countdown, container, false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top