小编Ric*_*ick的帖子

有条件的RxJava断链

我正在创建一个可观察的序列,我基本上寻找一种很好的方法来打破链条,如果条件不满足.理想情况下,我们可以通过此方案中的错误.这基本上是删除flatmap运算符中的if/else语句.这就像我现在的样子

flatMap(new Func1<User, Observable<Response>>() {
                    @Override
                    public Observable<Response> call(Result result) {
                        if(isValid(result))) {
                            return api.getObservableResponse(); //some retrofit observable
                        } else {
                            observer.onError();
                            return null; //??? I guess this would force an error
                        }
                    }
                })
Run Code Online (Sandbox Code Playgroud)

我已经看过过滤器()和所有其他有条件的运算符,但我不确定它们是否符合我的要求.有一个更好的方法吗?或者我看起来很好吗?谢谢!

java conditional android rx-java rx-android

9
推荐指数
1
解决办法
3478
查看次数

Android Studio - ProGuard IOException重复Zip条目

所以我想在Gradle中为我的Android Studio项目设置ProGuard,在构建项目时出现以下错误:

错误:任务':app:proguardDebug'的执行失败.java.io.IOException:无法写[C:\ Users\Rich\Desktop\WebProjects\AndroidStudioProjects\Roomie\app\build\intermediates\classes-proguard\debug\classes.jar](无法读取C:\用户\ Rich\Desktop\WebProjects\AndroidStudioProjects\Roomie\app\libs\bolts-android-1.1.4.jar(;;;;;;!META-INF/MANIFEST.MF)](重复的zip条目[a/a .class == bolts-android-1.1.4.jar:bolts/AggregateException.class]))

这是我的proguard-rules.pro

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }

-dontwarn org.apache.http.annotation.**

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}
Run Code Online (Sandbox Code Playgroud)

java android proguard gradle android-studio

5
推荐指数
1
解决办法
5777
查看次数

RxJava无法在未调用Looper.prepare()的线程内创建处理程序

好的,所以我在我的Android应用程序中使用的RxJava Observable遇到了一些麻烦.这是非常令人沮丧的,因为这已经工作了一段时间,现在只是抛出错误.我知道这意味着我正在从另一个线程进行UI操作,但我不知道发生了什么.所以这是可观察的:

 ConnectionsList.getInstance(this).getConnectionsFromParse(mCurrentUser)
            .delay(3, TimeUnit.SECONDS)
            .flatMap(s -> mainData.getDataFromNetwork(this, mCurrentUser, mSimpleFacebook))
            .flatMap(s -> mainData.getPictureFromUrl(s))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Bitmap>() {
                @Override
                public void onCompleted() {
                    if (mCurrentUser.get(Constants.NAME) != null) {
                        mNavNameField.setText((String) mCurrentUser.get(Constants.NAME));
                    }
                    mSearchFragment.setupActivity();
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    mSearchFragment.setEmptyView();
                }

                @Override
                public void onNext(Bitmap bitmap) {
                    mNavProfImageField.setImageBitmap(bitmap);
                    mainData.saveImageToParse(bitmap); //save the image to Parse backend
                }
            });
Run Code Online (Sandbox Code Playgroud)

经过一些调试后,我发现这个flatmap是发生错误的地方:

.flatMap(s -> mainData.getDataFromNetwork(this, mCurrentUser, mSimpleFacebook))
Run Code Online (Sandbox Code Playgroud)

其中包括以下内容,我添加了一个注释错误的注释:

 return Observable.create(subscriber -> {
            //set the username field if ParseUser is not null
            if(currentUser != …
Run Code Online (Sandbox Code Playgroud)

java multithreading android rx-java rx-android

5
推荐指数
1
解决办法
1935
查看次数

本地路径不存在Android Studio 1.0

我最近下载了最新的Android Studio 1.0版本,并立即开始试图让新项目启动并运行.我创建了一个新的地图项目,一旦我去运行它,它给了我以下内容:

Waiting for device.
Target device: motorola-xt1063-ZX1D3226RL
Uploading file
    local path: C:\Users\Rich\Desktop\WebProjects\AndroidStudioProjects\BlocWork\BlocSpot\app\build\outputs\apk\app-debug.apk
    remote path: /data/local/tmp/com.richluick.blocspot
Local path doesn't exist.
Run Code Online (Sandbox Code Playgroud)

现在,我意识到存在类似的问题,但我尝试的任何工作都没有.我像大多数人建议的那样更新了.iml文件:

<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="java-gradle" name="Java-Gradle">
      <configuration>
        <option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
    <option name="APK_PATH" value="/build/apk/blocspot-debug-unaligned.apk" />
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" inherit-compiler-output="false">
    <output url="file://$MODULE_DIR$/build/classes/main" />
    <output-test url="file://$MODULE_DIR$/build/classes/test" />
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <excludeFolder url="file://$MODULE_DIR$/.gradle" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
</module>
Run Code Online (Sandbox Code Playgroud)

那不适合我.由于其他人也在消化,我尝试了这个gradlew clean packagedebug --debug命令,得到了以下内容: …

android gradle android-studio build.gradle android-gradle-plugin

3
推荐指数
1
解决办法
9314
查看次数

手机和平板电脑的不同布局的牛刀

我在当前项目中使用Butterknife库时遇到了一些问题。我目前正在针对手机和平板电脑优化该项目,例如,两者的布局文件之间有时会略有差异

layout / layout_example.xml

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text1/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

layout-large / layout_example.xml

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text1/>

     <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text2/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

该类如下所示:

    @Bind(R.id.text1) TextView text1;    
    @Bind(R.id.text2) TextView text2; 

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_example, container, false)
        ButterKnife.bind(this, rootView);
        return rootView;
    }
Run Code Online (Sandbox Code Playgroud)

问题在于,这在电话上查看时会由于缺少视图而引发异常,因为“ text2”视图不存在。有没有办法解决?还是我必须使用findViewById作为并非在所有布局上都可用的视图。谢谢!

android android-layout screen-size butterknife

3
推荐指数
1
解决办法
825
查看次数

使用SurfaceView的中心作物Exoplayer

我目前在一个项目中使用Exoplayer,并希望实现类似于视频视图中的视频的“ CenterCrop”类型属性。基本上,它将适合曲面视图的高度,保持相同的宽高比,并裁切屏幕边缘的边缘。我的布局看起来像这样:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/videoPlayerContainer"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="404dp">

    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/videoPlayer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        app:controller_layout_id="@layout/empty_controls">

        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </com.google.android.exoplayer2.ui.SimpleExoPlayerView>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"
        android:background="@android:color/white"/>

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

创建和启动播放器的大多数代码已经由同事在包装器中设置,基本上如下所示:

BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

LoadControl loadControl = new DefaultLoadControl();
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this.context, trackSelector, loadControl);

simpleExoplayer.setSurfaceView(surfaceView);

DataSource.Factory mediaDataSourceFactory = new DefaultDataSourceFactory(context, "Agent");

MediaSource videoSource = new HlsMediaSource(uri, mediaDataSourceFactory, new Handler(), null);

simpleExoplayer.addListener(this);
simpleExoplayer.prepare(videoSource);

simpleExoplayer.setPlayWhenReady(true);

//this is supposedly used to set the scaling mode …
Run Code Online (Sandbox Code Playgroud)

android crop video-streaming exoplayer

3
推荐指数
1
解决办法
2796
查看次数

字符串值更改时RxAndroid更新字段/ Textview

我刚刚开始使用RxJava/RxAndroid,我想知道我是否可以使用它来解决以下问题.基本上,给定一个Field,比如一个textview,一个值,一个字符串,我正在寻找一种方法来在字符串的值发生变化时自动更新textview.我不确定如何将其作为Observable实现.让我来证明一下;

String str = "Test"; //the string value
TextView textView = (TextView) findViewById(R.id.textView); //the textview

Observable o = //looking for this part. Want to observe the String str

o.subscribe(new Observer<String>() { //subscribe here looking for string changes

            @Override
            public void onCompleted() {
                System.out.println("Completed");
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(String s) {
                textView.setText(s); //update the textview here
            }

        });

//here is where the string changes, it could be hardcoded, user input, or   
//anything else really, …
Run Code Online (Sandbox Code Playgroud)

android observer-pattern rx-java rx-android

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