小编Roc*_*Lee的帖子

Kotlin中的2D阵列

你如何在Kotlin中制作2D Int数组?我正在尝试将此代码转换为Kotlin:

    int[][] states = new int[][] {
        new int[]{-android.R.attr.state_pressed}, // not pressed
        new int[] { android.R.attr.state_pressed}  // pressed
    };
    int[] colors = new int[] {
        foregroundColor,
        accentColor,
        accentColor
    };
    ColorStateList myList = new ColorStateList(states, colors);
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的一次尝试,其中第一个2D阵列不起作用,但我让1D阵列工作:

    //This doesn't work:
    var states: IntArray = intArrayOf(
        intArrayOf(-android.R.attr.state_pressed), // not pressed
        intArrayOf(android.R.attr.state_pressed)  // pressed
    );
    //This array works:
    var colors: IntArray = intArrayOf(
        foregroundColor,
        accentColor,
        accentColor
    );
    val myList: ColorStateList = ColorStateList(states, colors);
Run Code Online (Sandbox Code Playgroud)

arrays kotlin

29
推荐指数
6
解决办法
3万
查看次数

Kotlin Foo :: class.java"未解决的参考:Java"错误

我正在尝试将我的Java代码转换HomePage.class为Kotlin.我按照Kotlin.org上的说明操作:

的getClass()

要从对象检索类型信息,我们使用javaClass扩展属性.

val fooClass = foo.javaClass

而不是Java的Foo.class使用 Foo::class.java.

val fooClass = Foo::class.java

我有一个名为HomePage的类,它扩展了AppCompatActivity(在Android中).我正在使用Android Studio.我试过了HomePage::class.java,它有一个错误:Unresolved reference: java

在此输入图像描述

我如何让它工作?

kotlin

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

FragmentTransaction.replace()淡入过渡显示"ghost"片段

您可以下载我的整个项目来尝试和调试.这是我的整个代码的回购:https://bitbucket.org/lexic92/studio48/


当我尝试用空白片段替换空白片段时,我在转换中出现了"鬼片段".

如何重现问题: 我有一个导航抽屉,当我点击一个项目时,它会打开一个填满整个屏幕的片段.

导航抽屉:

  • 按钮1 - 带文本的片段
  • 按钮2 - 空的片段

    1. 当我打开应用程序时,它会从包含文本的片段开始.然后,我打开导航抽屉并单击按钮2. 转换不良,瞬间变为空白屏幕,然后切换回淡入淡出的文本,直到它变为空白屏幕.

    2. 如果我打开导航抽屉并再次单击按钮2 ,它将从全文淡出到空白屏幕.因此,对于一个瞬间,它会在不应该的情况下显示带有文本的片段.我一步一步地在调试模式下运行我的应用程序,以减慢速度,并验证这种行为确实发生了.

    3. 如果我第三次打开导航抽屉并再次单击按钮2 ,它正确地不会显示任何动画,因为它正在淡入同一个屏幕.

有谁知道为什么会这样?你认为它与导航抽屉有什么关系吗?

这是我的整个代码的回购: https ://bitbucket.org/lexic92/studio48/

以下是一些代码摘录:

SongDetailActivity.java:

      @Override
      public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();

        //OPEN THE BLANK SCREEN
        if(position == 1) {
            fragmentManager.beginTransaction()
                    //.setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                    .replace(R.id.songlist_detail_container, PlaceholderFragment.newInstance(position + 1, mSongId))
                    .commit();
        }
        //OPEN THE SCREEN WITH TEXT
        else {
            SongDetailFragment …
Run Code Online (Sandbox Code Playgroud)

android android-animation android-fragments android-transitions

16
推荐指数
1
解决办法
5746
查看次数

在IntentService和AsyncTask(Android)之间使用共享代码时,Realm`从错误的线程访问`错误

我有一些代码可以下载"当前"对象的JSON.但是,每当警报响起时(当应用程序没有运行任何UI时),以及应用程序运行时的AsyncTask,IntentService都需要调用相同的代码.

但是,我得到一个错误说Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.但是,我不明白这个堆栈跟踪是如何或为什么在另一个线程上.

我能够通过复制所有共享代码并将其直接粘贴到DownloadDealService的onHandleIntent方法中来消除错误,但它非常草率,我正在寻找一个不需要复制代码的更好的解决方案.

如何在不重复代码的情况下摆脱此错误?谢谢.

public class DownloadDealService extends IntentService
{
    ...
    @Override
    protected void onHandleIntent(Intent intent)
    {
        Current todaysCurrent = Utils.downloadTodaysCurrent(); //<--- included for background info
        String dateString = Utils.getMehHeadquartersDate(); //(omitted)
        Utils.onDownloadCurrentCompleteWithAlarm(todaysCurrent, dateString); //<------ calling this...
    }
}

public class Utils
{
    // ...other methods ommitted...

    //This method is not in the stack trace, but I included it for background …
Run Code Online (Sandbox Code Playgroud)

multithreading android realm android-asynctask android-intentservice

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

Django管理员网站没有显示CSS样式

我的Django管理员网站(它是完全默认的,不是自定义的)没有显示预期的CSS.

它看起来像这样:

在此输入图像描述

我可以登录:

在此输入图像描述

但它应该看起来像这样:

在此输入图像描述

我该如何解决?

其他可能有用的信息:

我在端口80上运行Amazon EC2实例并使用真实URL连接到它.我使用本教程进行设置:http: //www.nickpolet.com/blog/deploying-django-on-aws/1/

在该教程之后,我将此代码放入一个名为的文件中/etc/apache2/sites-enabled/mysite.conf.我不明白这段代码在做什么,所以我认为它可能与问题有关.

/etc/apache2/sites-enabled/mysite.conf:

WSGIScriptAlias / /home/ubuntu/cs462/mysite/mysite/wsgi.py
WSGIPythonPath /home/ubuntu/cs462/mysite
<Directory /home/ubuntu/cs462/mysite/mysite>
    <Files wsgi.py>
        Order deny,allow
        Require all granted
    </Files>
</Directory>

Alias /media/ /home/ubuntu/cs462/mysite/media/
Alias /static/ /home/ubuntu/cs462/mysite/static/

<Directory /home/ubuntu/cs462/mysite/static>
    Require all granted
</Directory>

<Directory /home/ubuntu/cs462/mysite/media>
    Require all granted
</Directory>
Run Code Online (Sandbox Code Playgroud)

目录结构:

/home/ubuntu/cs462/
        mysite/
            manage.py
            db.sqlite3
            mysite/
                __init__.py
                __init__.pyc  
                settings.py  
                settings.pyc  
                urls.py  
                wsgi.py
            homepage/
                admin.py  
                admin.pyc  
                __init__.py  
                __init__.pyc  
                migrations  
                models.py  
                models.pyc  
                tests.py  
                views.py
Run Code Online (Sandbox Code Playgroud)

settings.py的最后一部分:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/ …
Run Code Online (Sandbox Code Playgroud)

apache django

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

如何在保持空格的同时split()一个String

你如何分割一串单词并保留空格?

这是代码:

String words[] = s.split(" "); 
Run Code Online (Sandbox Code Playgroud)

字符串包含: hello world

代码运行后,单词[]包含: "hello" "" world

理想情况下,它不应该是中间的空字符串,但包含两个空格:words []应该是: "hello" " " " " world

我怎样才能得到这个结果?

java regex

9
推荐指数
2
解决办法
3410
查看次数

由ActivityTestRule实例化引起的java.lang.IncompatibleClassChangeError

我在我的Android项目中添加了espresso测试,并在创建ActivityTestRule的行上获得了IncompatibleClassChangeError.我如何找出导致它的原因?

以下是导致错误的代码行:(HomePageScreenTest.java:27)

@Rule
public ActivityTestRule<HomePageActivity> homePageActivityTestRule = new ActivityTestRule<>(HomePageActivity.class);
Run Code Online (Sandbox Code Playgroud)

这是错误:

java.lang.IncompatibleClassChangeError: com.example.rocklee.mehmvp.HomePage.HomePageActivity
at dalvik.system.DexFile.defineClassNative(Native Method)
at dalvik.system.DexFile.defineClass(DexFile.java:226)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
at dalvik.system.DexPathList.findClass(DexPathList.java:321)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.example.rocklee.mehmvp.HomePage.HomePageScreenTest.<init>(HomePageScreenTest.java:27)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) …
Run Code Online (Sandbox Code Playgroud)

android android-testing android-espresso

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

如何创建包含RealmList的新RealmObject

我需要将我的一个模型对象(使用Retrofit从Json自动填充)转换为Realm对象.

起初,我的代码new RealmPoll()不是realm.createObject(RealmPoll.class).(我NullPointerException就像这个问题一样)所以我解决了这个问题.但我找不到复制RealmList的方法.

我找不到在创建与RealmLists RealmObjects的任何实例官方网站境界教程文档

只有Realm才能创建托管的RealmLists.每当底层Realm更新时,Managed RealmLists都会自动更新内容,并且只能使用RealmObject的getter访问.

这让我相信它在某种程度上是不可能的?但这是一项非常简单的任务.我不知道如何解释文档的含义.

有没有办法可以将一个对象(如下面的RetrofitPoll)转换为一个领域对象(如下面的RealmPoll),如果它包含一个列表?

一个功能说明了我的问题:

private RealmPoll convertRetrofitPollToRealmPoll(Realm realm, RetrofitPoll retrofitPoll)
{
    RealmPoll realmPoll = realm.createObject(RealmPoll.class);  //<----- fixed, used to be "new RealmPoll()".

    //Convert List<Answer>
    RealmList<RealmAnswer> realmAnswers = new RealmList<RealmAnswer>();  //<----- How to do same thing here?
    for(RetrofitAnswer retrofitAnswer : retrofitPoll.getAnswers())
    {
         realmAnswers.add(convertRetrofitAnswerToRealmAnswer(retrofitAnswer));
    }
    realmPoll.setAnswers(realmAnswers);
}
Run Code Online (Sandbox Code Playgroud)

RetrofitPoll.java

public class RetrofitPoll
{
    private List<Answer> answers;
    private String id;
    private Date startDate;
    private String title; …
Run Code Online (Sandbox Code Playgroud)

android realm

7
推荐指数
1
解决办法
8473
查看次数

android:fontFamily要求api等级16,但文档说15级是可以的吗?

如何在API级别15上设置字体系列?文档说API 15 确实具有fontFamily属性.我希望文档会说

"这是在API级别15中删除的.对于API级别15及更低级别,请参阅[........]"

但他们没有.如何在不生成此警告的情况下以XML(或以编程方式)设置字体?

这是Ubuntu上Android Studio的截图.

styles.xml:

在此输入图像描述

http://developer.android.com/reference/android/widget/TextView.html 在此输入图像描述

android

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

Android NullPointerException - 旋转后,Spinner onItemSelected`view`参数为null

注意:来自另一个类似SO问题的答案的限制性解决方法对我有用,但我有兴趣找到一个真正的解决方案.解决方法是将此添加到我的Activity:

@Override  
protected void onSaveInstanceState(Bundle outState) { /* do nothing */ }
Run Code Online (Sandbox Code Playgroud)

但我仍然希望将这个问题保持开放,以期找到更好的解决方案.


我的应用程序在最新的Android(Marshmallow,Lollipop)上旋转时崩溃,但它适用于KitKat.在Spinner onClick方法中,我得到了父元素的第一个子元素,它是下拉列表中的第一个textview(也就是微调器).当我不旋转它时,它工作得很好.此外,当我注释掉抛出NullPointerException的那一行时,它也可以正常工作.所以这是问题的唯一原因.

我知道它是空的,但我不明白为什么,或者如何解决它?此外,请注意我不能使用XML,因为文本颜色仅在运行时动态知道.另外,我希望最小API为15.

我加了一些调试代码,并且发现了旋转之前,parentview参数都不能为空.但旋转后,view为空.(但parent仍然不是空的).

请参阅具有空指针异常的行的******:

private void setUpSpinner(int accentColor, final int backgroundColor, Toolbar toolbar)
{
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    //Get rid of the normal toolbar's title, because the spinner is replacing the title.
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    //Set the choices on the spinner by setting the adapter.
    spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"},
            accentColor, backgroundColor));

    //Set …
Run Code Online (Sandbox Code Playgroud)

android nullpointerexception

6
推荐指数
1
解决办法
2478
查看次数