小编Tai*_*aig的帖子

如何将SearchWidget添加到ActionBar?

我正在尝试将Search-ActionView添加到我的应用程序中(如http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget中所述).不幸的是,我一直得到一个NullPointerException,我很难发现实际上出了什么问题.

我创建了一个可搜索的配置和一个可搜索的活动,如android页面所示.我的菜单.xml文件如下所示:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    ...
    <item
        android:id="@+id/menu_item_search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/icon_search"
        android:showAsAction="always"
        android:title="@string/action_bar_button_search">
    </item>

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

这是抛出异常的方法:

public boolean onCreateOptionsMenu( Menu menu )
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate( R.menu.action_bar, menu );

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();

    // NullPointerException thrown here; searchView is null.
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);

    return super.onCreateOptionsMenu( menu );
}
Run Code Online (Sandbox Code Playgroud)

完整的堆栈跟踪:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.activities.Test.onCreateOptionsMenu(Test.java:41)
at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:408)
at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:759)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2997)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at …
Run Code Online (Sandbox Code Playgroud)

android nullpointerexception android-actionbar searchview

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

如何使用Guice DI和Slick运行ScalaTest?

我不知道如何以GuiceApplicationBuilder这种方式配置,我能够加载需要DatabaseConfigProvider注入的控制器.

我想指定一个替代的postgres数据库用于测试,或者在内存数据库中指定(如果可能的话).

class   User
extends MySpecs
with    OneAppPerTest
{
    override def newAppForTest( testData: TestData ) = new GuiceApplicationBuilder()
        // Somehow bind a database here, I guess?
        .build()

    "A test" should "test" in
    {
        val result = Application.instanceCache[api.controller.User]
            .apply( app )
            .list()( FakeRequest() )

        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪

[info] - should return an entity *** FAILED ***
[info]   com.google.inject.ConfigurationException: Guice configuration errors:
[info] 
[info] 1) No implementation for play.api.db.slick.DatabaseConfigProvider was bound.
[info]   while locating …
Run Code Online (Sandbox Code Playgroud)

scala guice playframework slick play-slick

23
推荐指数
1
解决办法
3106
查看次数

android:fitsSystemWindows无效

在我的主题中,我定义了以下规则来在状态栏后面绘制我的观点:

<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
Run Code Online (Sandbox Code Playgroud)

并在Activity(onCreate)中:

getWindow.getDecorView.setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
)
Run Code Online (Sandbox Code Playgroud)

然后,在我的View中使用Barcodescanner,它应该在状态栏后面绘制,这是有效的.但是,当我应用于android:fitsSystemWindows任何子视图时,他们不会调整其位置.但是,当我将它应用于根元素时,它正在工作.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content">

    <my.Scanner
        android:id="@+id/scanner"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:fitsSystemWindows="true"> <!-- Should receive a top padding, right? -->

        <View android:layout_width="50dp"
              android:layout_height="50dp"
              android:background="@color/red" />

    </FrameLayout>

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

截图

android android-view

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

矢量drawables自动转换为pngs

在谷歌IO,工具团队表现出短暂的可能性来定义具有特定后缀矢量绘图资源(如ic_heart_ 48像素,然后将转换到所有必要的密度PNG格式的.xml).不幸的是,除了IO录音之外,我找不到任何关于此功能的文档.

这是Build Tools还是Android Studio的功能?假设它是Build Tools的一部分,它是否已经可用?我应该使用哪个版本?我必须明确启用该功能吗?

Google是否以Android xml矢量格式提供其素材图标?

android vector-graphics android-build android-icons

19
推荐指数
2
解决办法
9969
查看次数

从Android中的上一个活动中淡出活动

我目前正在开发Android应用程序,我的启动活动存在一些问题.我希望我的主要活动从我的飞溅活动淡入,而不是从黑屏.我的启动和主要活动都使用相同的背景图像,所以如果我做一个fade_in/fade_out组合,它看起来很奇怪,因为在转换过程中背​​景类型会稍微消失.

这里的想法是我必须使用我的徽标版本:正常和发光.我希望我的主要活动淡入,所以我的发光徽标覆盖了我的正常徽标,导致整洁的小"开启"效果.以下代码从黑屏中消失,这是不理想的.

/**
 * Pause Activity
 */
@Override
protected void onPause()
{
    super.onPause();
    finish();
    overridePendingTransition(android.R.anim.fade_in, 0);
}
Run Code Online (Sandbox Code Playgroud)

编辑:

创建我自己的fade_out动画以保持1.0 alpha级别(从1.0到1.0)并添加android:fillAfter="true"到我的初始动画集修复了问题.

android android-activity

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

Android的HttpURLConnection在HEAD请求上抛出EOFException

这个小代码片段可以在Mac的JVM上正常运行.不幸的是,它在Android 4.2上执行时崩溃了.

import java.net.HttpURLConnection;
import java.net.URL;

public class App
{
    public static void main( String... arguments ) throws Exception
    {
        HttpURLConnection connection = (HttpURLConnection) new URL( "https://github.com" ).openConnection();
        connection.setRequestMethod( "HEAD" );

        System.out.println( connection.getResponseCode() + "" );
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我更换https://github.comhttps://www.facebook.com它工作正常,但我无法找出原因.

该例外不包含消息; 所以这里至少是堆栈跟踪.

java.io.EOFException
        at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:206)
        at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:98)
        at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
        at libcore.net.http.HttpEngine.initContentStream(HttpEngine.java:541)
        at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:844)
        at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
        at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
        at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
Run Code Online (Sandbox Code Playgroud)

java android http urlconnection httpurlconnection

17
推荐指数
1
解决办法
6825
查看次数

React继续转义属性中的放大器字符(&)

我有一个具有data-icon属性的组件.例如,此属性的值应该是&#xf00fcss可以通过它呈现它content: attr( data-icon );.

然而,无论我尝试什么:React继续逃避&amp;.即使我提供了正确的unicode角色\u0026#xf00f.

有没有办法阻止React搞乱这个价值?除了危险地设置内部html,因为我不想添加另一个包装器.

零件

define( [ 'react', 'util' ], function( React, Util )
{
    return React.createClass(
    {
        render: function()
        {
            //var amp = '\u0026',
            var amp = String.fromCharCode( 38 ),    
            // Util.icons[x] returns a String, such as "f00f"
            code = amp + '#x' + Util.icons[this.props.name] + ';';

            return (
                <i data-icon={code}>
                    {this.props.children ? <span>{this.props.children}</span> : null}
                </i>
            );
        }
    } );
} );
Run Code Online (Sandbox Code Playgroud)

用法

<Widget.Icon name="add" /> …
Run Code Online (Sandbox Code Playgroud)

javascript css escaping reactjs react-jsx

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

键入类和依赖类型

首先,我不知道如何正确标记我的问题.这也可能是我找不到有用资源的原因.任何提示都非常感谢.

trait Context[T]
{
    self =>

    trait Rule
    {
        def apply( value: T ): Boolean
    }

    implicit class RichRule[A <: Rule]( a: A )
    {
        def and[B <: Rule]( b: B ): and[A, B] = self.and( a, b )
        def or[B <: Rule]( b: B ): or[A, B] = self.or( a, b )
    }

    sealed trait Group[A <: Rule, B <: Rule] extends Rule
    {
        def a: A

        def b: B

        override def apply( value: T ) = ???
    } …
Run Code Online (Sandbox Code Playgroud)

scala implicit typeclass

11
推荐指数
1
解决办法
475
查看次数

使用Slick和无形HList

Slick的支持HList通常是一件好事.不幸的是,它带有自己的实现,几乎不提供任何有用的操作.因此,我想用无形 HList代替.这应该是" 微不足道的 ",但我不知道如何做到这一点.在网上搜索我发现没有证据表明某人设法完成了这项任务.

我认为这足以实现ProvenShape(如广告在这里),但因为我不明白的概念,油滑的 (Proven)Shape S,我没实现这一点.

我基本上是想把它煮沸

class   Users( tag: Tag )
extends Table[Long :: String :: HNil]( tag, "users" )
{
    def id = column[Long]( "id", O.PrimaryKey, O.AutoInc )

    def email = column[String]( "email" )

    override def * = ( id, email ) <>[TableElementType, ( Long, String )](
        _.productElements,
        hlist => Some( hlist.tupled )
    )
}
Run Code Online (Sandbox Code Playgroud)

向下

class   Users( tag: Tag )
extends Table[Long :: String :: HNil]( …
Run Code Online (Sandbox Code Playgroud)

scala hlist shapeless slick

11
推荐指数
1
解决办法
1147
查看次数

缓存隐式解决方案

为了减少项目的编译时间,我正在缓存某些通过隐式查找解析的类型类.这看起来有些麻烦,因为直接实现不起作用:

scala> implicit val x: String = implicitly[String]
x: String = null
Run Code Online (Sandbox Code Playgroud)

隐式查找将其自己的未初始化定义视为有效实现.A lazy val会以无限递归的方式吹制堆栈.因此,我正在以这种方式处理它:

implicit val x: String = cache.x

object cache {
   val x: String = implicitly[String]
}
Run Code Online (Sandbox Code Playgroud)

但这使得它过于复杂,并且缓存定义不能轻易地使用其他缓存类型(因为它们不是隐式的).

此外,遗憾的是,将值本身隐藏在范围内并不起作用.

scala> :pas
// Entering paste mode (ctrl-D to finish)

object scope {
    implicit val x: String = {
        import scope.{ x => _ }
        implicitly[String]
    }
}

// Exiting paste mode, now interpreting.

defined object scope

scala> scope.x
res0: String = null
Run Code Online (Sandbox Code Playgroud)

是否有更优雅的方法来实现隐式解析缓存?

scala scala-implicits

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