小编Gav*_*iel的帖子

java:(String [])List.toArray()给出了ClassCastException

以下代码(在android中运行)总是在第3行给出一个ClassCastException:

final String[] v1 = i18nCategory.translation.get(id);
final ArrayList<String> v2 = new ArrayList<String>(Arrays.asList(v1));
String[] v3 = (String[])v2.toArray();
Run Code Online (Sandbox Code Playgroud)

它也发生在v2是Object [0]时,以及当它中有字符串时.任何想法为什么?

java list classcastexception toarray

96
推荐指数
2
解决办法
7万
查看次数

如何使用appcompat v21在独立工具栏上启用homeAsUp或调用setDisplayHomeAsUpEnabled()

我正在尝试将我的应用程序转换为使用v21 AppCompat库,因此我开始使用工具栏而不是ActionBar.在我的所有常规活动(扩展ActionBarActivity)中,一切都很好.但在我的SettingsActivity中扩展了PreferenceActivity,因此我无法使用setSupportActionBar(actionbar)调用,我需要使用"独立"工具栏.工具栏显示,但我无法弄清楚如何将"home/up"按钮添加到工具栏.

SettingsActivity.java:

public class SettingsActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        Toolbar actionbar = (Toolbar) findViewById(R.id.actionbar);
        if (null != actionbar) {

            // In every other activity that extends ActionBarActivity I simply use:
            // setSupportActionBar(actionbar);
            // final ActionBar supportActionBar = getSupportActionBar();
            // supportActionBar.setDisplayHomeAsUpEnabled(true);

            // but in PreferenceActivity you need to add a standalone toolbar:    
            actionbar.setTitle(R.string.title_activity_settings);
            actionbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SettingsActivity.this.finish();
                }
            });

            // Inflate a menu to be displayed in …
Run Code Online (Sandbox Code Playgroud)

android android-appcompat android-actionbar-compat android-toolbar

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

Java中的最大大小列表

在Java中拥有一个具有List的所有功能但具有最大存储容量的数据结构对我来说很有用,并且在添加新数据时丢弃旧数据.可以想象,在某些时候,我可能想要实现一个固定大小的队列,它保持数据的更一般顺序,并在该顺序中丢弃最旧的数据,但这是未来的.

目前我正在实现它:

public class FixedSizeList<T> {

  private final int maxSize;
  private final LinkedList<T> list = new LinkedList<T>();

  public FixedSizeQueue(int maxSize) {
    this.maxSize = maxSize < 0 ? 0 : maxSize;
  }

  public T add(T t) {
    list.add(t);
    return list.size() > maxSize ? list.remove() : null;
  }

  // add remaining methods...

}
Run Code Online (Sandbox Code Playgroud)

是否存在(a)满足我需求的现有数据结构,或者(b)实现此数据结构的更好方法?

java arrays linked-list list

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

如何修复不受保护的SMS BroadcastReceiver lint警告

我的应用程序需要能够接收短信.一切正常,但我得到这个lint警告:

声明SMS_DELIVER或SMS_RECEIVED的intent-filter的BroadcastReceivers必须确保调用者具有BROADCAST_SMS权限,否则恶意actor可能会欺骗意图.

如何"确保调用者具有BROADCAST_SMS权限"?

在我的清单中我有:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application ...>
    <receiver
        android:name=".SmsReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="1000">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>
Run Code Online (Sandbox Code Playgroud)

我的代码:

public class SmsReceiver extends BroadcastReceiver {
    public SmsReceiver() {}

    @Override
    public void onReceive(final Context context, final Intent intent) {

        final Bundle bundle = intent.getExtras();
        if (bundle != null) {
            final Object[] pdusObj = (Object[]) bundle.get("pdus");
            for (int i = 0; i < pdusObj.length; i++) {
                final SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                // use currentMessage
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

android intentfilter broadcastreceiver android-lint android-permissions

14
推荐指数
1
解决办法
2901
查看次数

具有默认回退的pom.xml环境变量

我希望能够使用环境变量(如果已设置)或我在pom.xml中设置的默认回退值,类似于bash中的$ {VARIABLE:-default}.可能吗?就像是:

${env.BUILD_NUMBER:0}
Run Code Online (Sandbox Code Playgroud)

environment-variables pom.xml maven

12
推荐指数
2
解决办法
6924
查看次数

gradle中的多风格编译依赖项

有没有办法在Android Studio(build.gradle)中通过多种口味编译依赖项?

我有2个flavorGroups,每2个变种.在4种可能的组合中,我希望能够依赖于lib,只要我是最新的和免费的.latestCompile或freeCompile有效,但latestFreeCompile没有.这是我的build.gradle的相关部分:

android {
    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
        versionCode 15
        versionName "1.9." + versionCode
    }

    flavorGroups 'sdk', 'cost'

    productFlavors {
        latest {
            flavorGroup 'sdk'
            minSdkVersion 8
        }
        sdk7 {
            flavorGroup 'sdk'
            minSdkVersion 7
            versionName android.defaultConfig.versionName + ".sdk7"
        }
        free {
            flavorGroup 'cost'
        }
        pro {
            flavorGroup 'cost'
        }
    }
}
dependencies {
    // this works:
    freeCompile files('libs/StartAppInApp-2.2.1.jar')

    // and I would like something like this:
    latestFreeCompile 'com.google.android.gms:play-services:4.1.32' // minSdkVersion:8
}
Run Code Online (Sandbox Code Playgroud)

如果我愿意的话:

latestCompile 'com.google.android.gms:play-services:4.1.32'
Run Code Online (Sandbox Code Playgroud)

那么它也将被包含在latestPro中(不需要),如果我使用:

freeCompile 'com.google.android.gms:play-services:4.1.32'
Run Code Online (Sandbox Code Playgroud)

然后它也将包含在sdk7Free中(虽然它需要SDK …

dependencies android gradle android-gradle-plugin android-productflavors

11
推荐指数
2
解决办法
5375
查看次数

如何在Android Studio的run/debug配置中将参数传递给Java代码

我的Android应用程序对我的服务器做了一些http请求.但有时我正在调试在我的开发机器上运行的新api代码.我希望能够传递一些东西(比如环境变量),所以在我的代码中,如果它存在,我将能够使用它作为来自android模拟器的api请求的主机名.

所以我正在寻找一种传递类似的方法:

API_SERVER=http://10.0.2.2/myapp/
Run Code Online (Sandbox Code Playgroud)

在我的代码中我会以某种方式使用它,例如:

final static String API_SERVER_REAL = "http://example.com/";
final String apiServerOverride = System.getenv("API_SERVER");
final String API_SERVER = (null != apiServerOverride && !apiServerOverride.isEmpty() ? apiServerOverride : API_SERVER_REAL);
Run Code Online (Sandbox Code Playgroud)

android environment-variables run-configuration android-studio

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

在Android中的方向更改时,视图布局不会刷新

我希望我的VIEW布局可以根据方向变化进行调整.

我的清单设置为:android:configChanges="keyboardHidden|orientation"在活动上.

我有res/layout-land,res/layout-port.

我有onConfigurationChanged我的Activity(它被称为轮换).

在我,Activity我有一个ViewFlipper内部,我有LinearLayouts.

当我启动Activityin port或land模式时,View的布局是正确的(根据我的res/layout-????文件).

但是当我已经进入Activity并且我旋转手机时,我的视图布局不会改变.旋转屏幕,并且onCreate不会调用活动,因为它应该onConfigurationChanged被调用Activity,但是屏幕总是重新绘制,并且LinearLayoutActivity启动时加载了屏幕.

我有一种感觉,我错过了一些东西onConfigurationChanged,但不知道是什么.我想我需要ViewFlipperLinearLayout内部或内部调用一些函数.我应该打电话给哪些功能?

ViewFlipper flipper;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    flipper = new ViewFlipper(getApplicationContext());
    setContentView(flipper);
    initLayout();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(flipper);
}

void initLayout() {
    MyView myview = createMyView(0);
    flipper.addView(myview, 0); …
Run Code Online (Sandbox Code Playgroud)

android view rotation orientation onconfigurationchanged

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

如何在three.js中的球体上使用纹理

我从http://aerotwist.com/lab/getting-started-with-three-js/下载了一个球体示例,我可以看到漂亮的红色球体.我想在上面使用纹理.我试过这个:

var texture = THREE.ImageUtils.loadTexture("ball-texture.jpg");
texture.wrapS = texture.wrapT = THREE.ClampToEdgeWrapping;
texture.repeat.set( 125, 125 );
texture.offset.set( 15, 15 );
texture.needsUpdate = true;
var sphereMaterial = new THREE.MeshBasicMaterial( { map: texture } );
var sphere = new THREE.Mesh(new THREE.Sphere(radius, segments, rings),sphereMaterial);
Run Code Online (Sandbox Code Playgroud)

但我看不到任何东西,一切都是黑色的.有没有人有球体纹理的工作示例?

javascript geometry textures three.js

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

如何使用android DownloadManager下载多个文件,以便它出现在一个队列中

我正在使用DownloadManager下载文件.我知道如何下载一个文件并使用

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Run Code Online (Sandbox Code Playgroud)

或不.但是我有多个文件在某种意义上"归属"在一起,所以我想为这些文件设置一个"队列",并以显示"队列"进度的方式显示通知(假设我添加了) 5个文件到队列,因此在下载第一个文件后会显示20%,在其中2个文件后显示40%,...)

文件的顺序并不重要,但我希望能够在下载队列的最后一个文件时在BroadcastReceiver的onReceive()函数中进行检测.

到目前为止,我设法做的最接近的是不使用request.setNotificationVisibility,通过匹配onReceive()中的downloadId来计算下载的文件,并在下载最后一个文件时执行我需要做的事情.

有没有办法让它更"可见"?

android download-manager

7
推荐指数
0
解决办法
875
查看次数