小编asc*_*sco的帖子

AsyncTaskLoader:在方向更改后未调用onLoadFinished

我有一个AsyncTaskLoader长时间运行的任务,当加载器运行时,我的活动由于方向更改而被销毁,onLoadFinished不会调用回调.

我可以以某种方式'重新连接'加载器到我的新Activity /它的回调?

这是我的(简化)活动:

public class DashboardActivity extends BaseActivity {

StartupCallback startupCallback;
    boolean loading = false; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.empty_viewpager);
    startupCallback = new StartupCallback();

    if (!loading){
         getSupportLoaderManager().initLoader(GlobalApp.giveId(), null,
                startupCallback);
                 loading = true; 
    }
}


private class StartupCallback implements
        LoaderManager.LoaderCallbacks<Boolean> {
    @Override
    public void onLoadFinished(Loader<Boolean> loader, Boolean succ) {

        Log.d("LOG", "onLoadFinished"); 
    }

    @Override
    public Loader<Boolean> onCreateLoader(int id, Bundle args) {

        return new StartupLoader(getApplicationContext());
    }

    @Override
    public void onLoaderReset(Loader<Boolean> loader) {

    } …
Run Code Online (Sandbox Code Playgroud)

android

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

将样式资源ID从XML传递到类不起作用

我在库项目(Spinnerbutton)中有一个自定义小部件,我想在应用程序项目中使用它.自定义小部件包含TextView,我想从我的应用程序项目将样式传递给该TextView.

这是我的attrs.xml(在库项目中):

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

    <declare-styleable name="Spinnerbutton">
        <attr name="myTextAppearence" format="reference" />
    </declare-styleable>

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

和应用程序的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/lib/com.example.spinnerbuttonlib"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.spinnerbuttonlib.spinnerbutton.Spinnerbutton
        android:id="@+id/sbp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        custom:myTextAppearence="@style/SmallTextGray" />

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

以下是我尝试在Spinnerbutton类中读取自定义属性的方法:

public Spinnerbutton(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;

    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.Spinnerbutton);

    int textStyleId = a.getResourceId(
            R.styleable.Spinnerbutton_myTextAppearence, -1);

    a.recycle();

}
Run Code Online (Sandbox Code Playgroud)

textStyleId始终返回-1,因此该值不会从布局传递给类.

这有什么不对?

android android-widget

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

Android StreetView检查是否有给定位置的任何视图

我正在玩Android版Google Play Services 4.4的新StreetView功能(链接),我想知道是否有任何方法/可能性来检查是否有任何给定位置的'view'/ foto材料.当我加载一个未被StreetView覆盖的位置时,streetViewPanorama.setPosition(someLatLng); 我只得到一个黑屏.有事先检查吗?

android google-street-view

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

Java解耦数据库操作代码并使其可测试

我想为以下方法编写测试:

public void addItem(Item item) {

    items.add(0, item);
    DatabaseHelper.getInstance().writeOneItem(item);
}
Run Code Online (Sandbox Code Playgroud)

该类称为ItemManager,它的职责是管理用户可以保存到列表或从列表中删除的项目.它应与保留列表中项目的Sqlite数据库保持同步.

DatabaseHelper(ormlite)没有被引入时init(Context context)(通常在我的Andoid应用程序启动时发生,但在我的测试中没有完成),它的getInstance()方法将返回null并且从上面执行的方法将崩溃.

我该怎么办?我可以init(Context context)从我的测试中调用,或者DatabaseManager.getInstance()在调用它之前检查是否为null.但这似乎更像是一种解决方法.在我看来,我不应该在这个方法中做任何数据库的东西,并尝试尽可能地从数据库中分离ItemManager.

关于理想解决方案的外观的任何想法,不是从具体实施的形式,而是从一般的设计角度来看?

我是单元测试的新手,很难将相互之间的东西分开.

java unit-testing design-patterns

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

从类访问LocationManager/LocationListener

我有点迷失:在我的主要活动中,我注册一个LocationManager并将其连接到LocationListener以使用myLocation.getLatitude()等.

现在我需要使用另一个类的Location-方法.

我无法使用其他类中的那些对象,因为我无法实现主要活动.我不能使用getter来传递L.Manager或L.Listener,因为那些是非静态的.

因此,一般来说,我如何访问我在主要活动中创建的对象?关于如何更好地组织这个的任何提示?主活动类中的LocationListener类通常是一件蠢事吗?

public class URNavActivity extends Activity

{
    public LocationManager mlocManager;
    public LocationListener mlocListener;
...
}

public void onCreate(Bundle savedInstanceState)
{       
    super.onCreate(savedInstanceState);
    mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());

    actVar=this;

    initGraph();
    setMap();
    gpsEnable();
    initMyLocation();
    getItems();
    initOverlay();
}

public void gpsEnable ()
{
    mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    mlocListener = new MyLocationListener();

    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}

public class MyLocationListener implements LocationListener

{

@Override

public void onLocationChanged(Location loc)

{

    loc.getLatitude();
    loc.getLongitude();
    myMap.getController().setCenter(new GeoPoint(lati, longi));
}
Run Code Online (Sandbox Code Playgroud)

android location

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

使用TabLayout时NoClassDefFoundError

我想从Android设计支持布局中查看新的TabLayout.

使用的TabLayout只是

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
Run Code Online (Sandbox Code Playgroud)

在Java和

<TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

这是我的build.gradle文件(简化了一点):

apply plugin: 'android'

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 22        

        dexOptions {
            preDexLibraries = false
            javaMaxHeapSize "4g"
        }

    }

    dexOptions {
        preDexLibraries = false
    }
}


afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = ['--multi-dex']
        } else {
            dx.additionalParameters += '--multi-dex'
        }
    }
}

dependencies …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-support-library

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

Dagger 2注射法

我正在玩Dagger 2.

我有以下内容Module:

@Module
public class GameSetupModule {
    @Provides
    @Singleton
    GameSetup provideGameSetup() {
        return new GameSetup();
    }
}
Run Code Online (Sandbox Code Playgroud)

和根据Component:

@Singleton
@Component(modules = {GameSetupModule.class})
public interface GameSetupComponent {
    GameSetup provideGameSetup();

    void inject(SetupActivity activity);

//    void inject(Fragment fragment);

    void inject(SetupCompletedFragment fragment);

    void inject(SelectQuarterLengthFragment fragment);

    void inject(SelectTeamColorsFragment fragment);

    void inject(SelectUserRoleFragment fragment);

}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,GameSetup将注入几个不同的片段,如下所示:

@Inject
GameSetup gameSetup; 

onCreate(){
   getGameSetupComponent().inject(this); 
}
Run Code Online (Sandbox Code Playgroud)

It works fine when implemented as seen above, the injection does not work though when I just use …

android dependency-injection dagger-2

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

库函数是否应该挂起或返回延迟

假设我正在编写一个库,它返回一个字符串,这是一个复杂且长时间运行的任务。

我可以选择提供这个

interface StringGenerator {
   suspend fun generateString(): String
}
Run Code Online (Sandbox Code Playgroud)

或者

interface StringGenerator {
   fun generateString(): Deferred<String>
}
Run Code Online (Sandbox Code Playgroud)

这两种选择是否有任何(缺点)优点,它们是什么?我应该选择哪个?

kotlin kotlin-coroutines

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

根据其他View的宽度设置TextView宽度

我有一个ImageView,图像从网上加载,宽度可以变化.下面我有一个TextView图像的描述.我希望它TextView和它一样宽ImageView.

我的尝试是这样的:

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />

            <TextView
                android:id="@+id/imagedescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textAppearance="@style/SmallTextGray" />
        </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

以某种方式工作,因为TextView宽度设置符合ImageView,但TextView在2行之后结束:

在此输入图像描述

我知道我可以TextView在知道图像的宽度后以编程方式设置宽度,但是在XML布局中是否还有一种方法可以做到这一点?

android android-layout

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

Google Play Services 7.0设置对话框

谷歌在他们的博客文章中描述了一个单触设置对话框,要求用户打开他们的位置(让用户打开位置而不将他们发送到手机的设置).我只是找不到合适的方法/方法来在他们的API文档中执行此操作.

有人已经使用过这个并且可以给出解释吗?

android google-play-services

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