小编azi*_*ian的帖子

Lottie Animation setAnimation() 结果 IllegalStateException

我今天想检查 IOS 和 Android 的 Lottie lib,我在 After Effect 中制作了一个简单的指示器动画并将其导出到 .json 文件,它在 IOS 上完美运行,当我在 Android 版本上测试相同的文件时,我得到以下结果错误,我真的不知道我是否遗漏了什么?

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.lottietest/com.test.lottietest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.airbnb.lottie.LottieAnimationView.setAnimation(java.lang.String)' on a null object reference
Run Code Online (Sandbox Code Playgroud)

这是我的活动 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mtids.lottietest.MainActivity">

    <view
        class="com.airbnb.lottie.LottieAnimationView"
        id="@+id/animationView"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="133dp"
        android:layout_marginTop="20dp"
        android:onClick="animate"
        android:text="animate" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

活动代码

public class MainActivity extends AppCompatActivity {
    LottieAnimationView anim;
    @Override
    protected void onCreate(Bundle savedInstanceState) …
Run Code Online (Sandbox Code Playgroud)

animation android lottie-android

4
推荐指数
2
解决办法
1万
查看次数

HTTP 失败:java.io.IOException:在多窗口模式下已取消

一切工作正常,当我的应用程序进入onPause状态然后onResume调用任何 API 时,我遇到以下错误。

HTTP 失败:java.io.IOException:已取消

我正在使用 RxJava 和 Retrofit 来完成所有网络操作。以下是我的设置。

应用程序组件.java

@ApplicationScope
@Component(modules = {ContextModule.class, NetworkModule.class})
public interface ApplicationComponent {
    void inject(MyApplication myApplication);
}
Run Code Online (Sandbox Code Playgroud)

应用范围.java

@Scope
@Retention(RetentionPolicy.CLASS)
  public @interface ApplicationScope {
}
Run Code Online (Sandbox Code Playgroud)

ContextModule.java

@Module
public class ContextModule {

    private Context context;

    public ContextModule(Context context){
        this.context = context;
    }

    @Provides
    @ApplicationScope
    public Context applicationContext(){
        return context.getApplicationContext();
    }

}
Run Code Online (Sandbox Code Playgroud)

网络模块.java

@Module @ApplicationScope
public class NetworkModule {

    private final String BASE_CONTACT_URL = "";

    @Provides @ApplicationScope
    public PoolAPIService getPoolApiService(Retrofit retrofit){ …
Run Code Online (Sandbox Code Playgroud)

android rx-java dagger-2 retrofit2

4
推荐指数
1
解决办法
9996
查看次数

在嵌套的RecyclerView中保存滚动状态

我的申请中有问题.我有主要清单(回收者视图).此列表保留20个水平回收站视图.它看起来非常类似于Netflix应用程序.我需要在主列表中保留每个"轮播"的状态.如果我将第一个项目向右移动,向左移动,向下滚动(第一个和第二个项目被回收),旋转屏幕,向上滚动,我需要恢复第一个和第二个元素的位置.

我知道我应该使用linearLayout.saveOnInstanceState和onRestoreState但我的问题是每个轮播都在主列表中.

configChanges 在AndroidManifest.xml中无法发挥作用......

谢谢!

android android-layout android-scrollview android-view android-recyclerview

4
推荐指数
2
解决办法
2418
查看次数

PlaceAutocompleteFragment:自动打开SearchView

活动加载完成后,我想自动单击片段。

片段定义为:

 <fragment
    android:id="@+id/place_autocomplete_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
    />
Run Code Online (Sandbox Code Playgroud)

我尝试这样做

fragment = findViewById(R.id.place_autocomplete_fragment);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            fragment.performClick();
        }
    }, 1000);
Run Code Online (Sandbox Code Playgroud)

但是没有用。

有什么方法可以自动单击片段吗?

编辑:就我而言,该片段由

//PlaceAutoComplete Search Implementation
    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);


    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            Log.i(String.valueOf(this), "Place: " + place.getName() + "\nID: " + place.getId());
            String placeId = place.getId();
            try {
                Intent intent = new Intent(PlaceSearch.this, PlaceDetailsFromSearch.class);
                Bundle extras = new Bundle();
                extras.putString("placeID", placeId);
                intent.putExtras(extras);
                startActivity(intent);
            } catch (Exception e) …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments android-view google-places

4
推荐指数
1
解决办法
1348
查看次数

为什么我们使用ViewTreeObserver#addOnGlobalLayoutListener()

我们为什么使用ViewTreeObserver,任何人都可以解释一下?

在下面的代码creditsViewTextView对象。通过整个代码,我了解到“这是根据条件隐藏一些文本”,但是唯一的原因就是我们为什么要使用它ViewTreeObserver

mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = mainLayout.getRootView().getHeight() - mainLayout.getHeight();

            if (heightDiff > 100) {
                Utils.appLogger("MyActivity", "keyboard opened");
                creditsView.setVisibility(View.GONE);
            }

            if (heightDiff < 100) {
                Utils.appLogger("MyActivity", "keyboard closed");
                creditsView.setVisibility(View.VISIBLE);
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

java android android-layout android-view android-viewtreeobserver

4
推荐指数
2
解决办法
4946
查看次数

在父母之外动画儿童视图

我试图在其父视图之外设置一个视图的动画,当我这样做时,子视图无法在其父视图之外进行动画处理.我通过使用setClipChildren(false)它解决了这个问题并且它工作了...当视图动画化.当我为视图设置动画,图像仍然是隐藏的.

这是有效的代码.此代码会将平铺按钮设置为屏幕顶部的动画:

private void setGameBoard(){

        brickWall.setClipChildren(false);
        brickWall.setClipToPadding(false);

        //Build game board
        for(int ii = 0; ii < brickRows;ii++){
            final int x = ii;

            //Build table rows
            row = new TableRow(this.getApplicationContext());
            row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 50));
            row.setClipChildren(false);
            row.setClipToPadding(false);

           // row.setBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.colorAccent, null));

            //Build table tiles
            for(int jj=0; jj < brickColumns; jj++){
                final int y = jj;
                final Brick tile = new Brick(this);
                tile.setClipBounds(null);

                tile.setBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null));

                //Set margins to create look of tic-tac-toe
                TableRow.LayoutParams lp = new TableRow.LayoutParams( …
Run Code Online (Sandbox Code Playgroud)

animation android android-animation

4
推荐指数
2
解决办法
1962
查看次数

recyclerView 的 setOnClickListener 不起作用

因为RecyclerView如果它没有任何项目,则单击RecyclerView有效,但如果它有项目,则单击RecyclerView不起作用。小心我的意思是点击 just RecyclerViewnotRecyclerView的项目

recyclerView.setOnClickListener(view -> {Timber.d("recyclerView clicked");});
Run Code Online (Sandbox Code Playgroud)

RecyclerView即使上面有项目,我如何设置可点击。

android view android-layout android-view android-recyclerview

4
推荐指数
3
解决办法
4232
查看次数

不推荐使用LifecycleRegistryOwner类

我正在使用Android架构组件中的Room数据库.因此,LiveData我正在LifecycleRegistryOwner我的活动中实现界面.由于有替代解决方案可供使用,LifeCyceActivity但我的活动已经延长,BaseActivity所以我不能这样做.

什么是替代品LifecycleRegistryOwner

android android-room android-livedata android-architecture-components

4
推荐指数
2
解决办法
1162
查看次数

Glide 4:获取 GifDrawable 的 GifDecoder

我希望将我的应用程序从 Glide v3 升级到 Glide v4。我需要知道gif通过 Glide 加载的循环有多长。

v3 代码:

int duration = 0;
GifDecoder decoder = gifDrawable.getDecoder();
for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
    duration += decoder.getDelay(i);
}
Run Code Online (Sandbox Code Playgroud)

看起来GifDecoderGlide v4 不再公开。没有它我如何计算这个,或者我现在如何获得解码器?

java android image-loading android-image android-glide

4
推荐指数
1
解决办法
1268
查看次数

ConstraintLayout:基于百分比的尺寸

我可以使用约束布局中的水平和垂直偏差基于屏幕尺寸设置UI元素的位置。

但是,UI元素的宽度和高度不会根据屏幕大小而改变,因此看起来仍然不完美。

那我怎么能做到这一点呢?

下面是我的源代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.satizh.android.constraintlayouttest.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="125dp"
        android:layout_height="47dp"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="0dp"
        android:layout_weight="68"
        android:text="5%V30%H"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.050000012" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/button"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.915"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintVertical_bias="0.138" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.14" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.05" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline3"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline4"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.53" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

我已经创建了一些指导原则(以百分比为单位),以便我们可以猜测旋转设备时基于屏幕的UI按钮的实际外观。这是更好理解的gif。

在此处输入图片说明

android android-layout android-view android-constraintlayout

4
推荐指数
1
解决办法
3540
查看次数