Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Null Pointer Exception when stepView.go(1, true); only for 1...not in any other position #18

Closed
RahilRK opened this issue Mar 13, 2018 · 9 comments
Assignees
Milestone

Comments

@RahilRK
Copy link

RahilRK commented Mar 13, 2018

            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.e_commerce_app.rahil.e_commerce/com.e_commerce_app.rahil.e_commerce.Order_Detail}: java.lang.NullPointerException
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                                  Caused by: java.lang.NullPointerException
                                                                                     at com.shuhart.stepview.StepView.getAnimator(StepView.java:272)
                                                                                     at com.shuhart.stepview.StepView.animate(StepView.java:242)
                                                                                     at com.shuhart.stepview.StepView.go(StepView.java:219)
                                                                                     at com.e_commerce_app.rahil.e_commerce.Order_Detail.onCreate(Order_Detail.java:47)
                                                                                     at android.app.Activity.performCreate(Activity.java:5231)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:136) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                     at dalvik.system.NativeStart.main(Native Method) 

HERE IS MY ARRAY from resourse

Processing
Shipped
Delivery

@Mani1124
Copy link

Mani1124 commented Apr 23, 2018

don't use this library. use this https://github.com/baoyachi/StepView

@Vinayak2108
Copy link

Vinayak2108 commented Jun 18, 2018

Download the source code and add measure(int width,int height) method call in else part of go(int step, boolean animate) method.

 public void go(int step, boolean animate) {
        if (step >= START_STEP && step < getStepCount()) {
            if (animate && animationType != ANIMATION_NONE) {
                if (Math.abs(step - currentStep) > 1) {
                    endAnimation();
                    currentStep = step;
                    invalidate();
                } else {
                    measure(getWidth(),getHeight());
                    nextAnimatedStep = step;
                    state = ANIMATE_STEP_TRANSITION;
                    animate(step);
                    invalidate();
                }
            } else {
                currentStep = step;
                invalidate();
            }
        }
    }

@shuhart
Copy link
Owner

shuhart commented Jun 18, 2018 via email

@Vinayak2108
Copy link

when we call step.go(1,true) from onCreate() the app get crash due to null pointer exception and on further inspection i found in method getAnimator() startLinesX and endLinesX are null and these variables are initialized in measureLines() method which get call from chained calls started from onMeasure() method. so measure() call before getAnimator() insure startLinesX, endLinesX should not be null. if i am wrong or you know better solution for this issue then let me know.

@shuhart
Copy link
Owner

shuhart commented Jun 18, 2018 via email

@Vinayak2108
Copy link

Here is My Code

Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <include layout="@layout/toolbar_other"/>

        <com.shuhart.stepview.StepView
            android:id="@+id/step_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            app:sv_steps="@array/cafe_steps"
            app:sv_background="@color/colorPrimary"
            app:sv_stepLineWidth="2dp"
            app:sv_stepPadding="4dp"
            app:sv_selectedCircleColor="@color/white"
            app:sv_selectedTextColor="@color/white"
            app:sv_selectedCircleRadius="15dp"
            app:sv_selectedStepNumberColor="@color/colorPrimary"
            app:sv_nextTextColor="@color/transWhiteGrade2"
            app:sv_nextStepLineColor="@color/transWhiteGrade2"
            app:sv_doneCircleColor="@color/white"
            app:sv_doneStepLineColor="@color/white"
            app:sv_doneCircleRadius="15dp"
            app:sv_doneTextColor="@color/transWhiteGrade2"
            app:sv_doneStepMarkColor="@color/colorPrimary"
            app:sv_stepNumberTextSize="12sp"
            app:sv_animationType="All"/>
        <!--app:sv_stepViewStyle="@style/StepView"-->

    </android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/pager"
        android:layout_weight="1"/>

</LinearLayout>

Activity File

package com.officegenie.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.officegenie.R;
import com.officegenie.fragment.Empty;
import com.shuhart.stepview.StepView;

public class CafeteriaActivity1 extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "tag";
    int currentStep = 0;
    StepView step;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_left);
        setContentView(R.layout.cafeteria_activity);


        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(this);

        TextView title = findViewById(R.id.title);
        title.setText(R.string.cafe);


        step = findViewById(R.id.step_view);
        step.go(1,true);  //getting error here....

        ViewPager pager = findViewById(R.id.pager);
        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);

    }

    private void moveToNextStep(){
        if (currentStep < step.getStepCount() - 1) {
            currentStep++;
            step.go(currentStep, true);
        } else {
            step.done(true);
        }
    }

    private void moveToPreviousStep(){
        if (currentStep > 0) {
            currentStep--;
        }
        step.done(false);
        step.go(currentStep, true);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_right);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.back:
                onBackPressed();
                break;

            default:
                Log.i(TAG, "Invalid Click");

        }

    }


    class PagerAdapter extends FragmentPagerAdapter {
        
        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return Empty.newInstance("Number : " + position);
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

}

@shuhart
Copy link
Owner

shuhart commented Jun 18, 2018

As far as I understand, you are trying to start an animation while the view is not measured yet. We can't measure view before the framework ask us about it, providing with the actual MeasureSpecs.
So, you have to wait until view is measured to start an animtion:

stepView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                stepView.go(1, true);
            }
        });

Or just skip the animation: go(step, false).
I think the second option is preferrable because it's very unlikely that you are going to see the animation right after an activity is launched.

I am going to fix this by checking, whether we measured or not, before starting an animation.

@shuhart shuhart added this to the 1.2.8 milestone Jun 18, 2018
@shuhart shuhart self-assigned this Jun 18, 2018
@shuhart
Copy link
Owner

shuhart commented Jun 18, 2018

Fixed in 1.2.8

@shuhart shuhart closed this as completed Jun 18, 2018
@gorishankar7
Copy link

Why would you need that?

On Mon, Jun 18, 2018, 14:26 Vinayak2108 @.***> wrote: Download the source code and add measure(int width,int height) method call in go(int step, boolean animate) method. public void go(int step, boolean animate) { measure(getWidth(),getHeight()); if (step >= START_STEP && step < getStepCount()) { if (animate && animationType != ANIMATION_NONE) { if (Math.abs(step - currentStep) > 1) { endAnimation(); currentStep = step; invalidate(); } else { nextAnimatedStep = step; state = ANIMATE_STEP_TRANSITION; animate(step); invalidate(); } } else { currentStep = step; invalidate(); } } } — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#18 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AFDzvgEJUQXdORRYm7x7ktPZ381yiG5Bks5t945YgaJpZM4SoZSf .

Sir, I'm also getting same error

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.shuhart.stepview.StepView.setSteps(java.util.List)' on a null object reference

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.Button;

import com.shuhart.stepview.StepView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BookingActivity extends AppCompatActivity {

    @BindView(R.id.step_view)
    StepView stepView;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.btn_previous_step)
    Button btn_previous_step;
    @BindView(R.id.btn_next_step)
    Button btn_next_step;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booking);

        setupStepView();
        setColorButton();


        //viewpager
        viewPager.setAdapter(new MyViewPagerAdapator(getSupportFragmentManager()));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float positionOffset, int positionOffsetPixels) {
                if (i == 0)
                    btn_previous_step.setEnabled(false);
                else
                    btn_previous_step.setEnabled(true);

                setColorButton();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setColorButton() {
        if (btn_next_step.isEnabled())
        {
            btn_next_step.setBackgroundResource(R.color.Button);
        }
        else
        {
            btn_next_step.setBackgroundResource(R.color.ButtonNext);
        }
        if (btn_previous_step.isEnabled())
        {
            btn_previous_step.setBackgroundResource(R.color.Button);
        }
        else
        {
            btn_previous_step.setBackgroundResource(R.color.ButtonNext);
        }


    }

    private void setupStepView() {
        List<String> stepList = new ArrayList<>();
        stepList.add("Resturant");
        stepList.add("Table");
        stepList.add("Time");
        stepList.add("Confirm");
        stepView.setSteps(stepList);
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants