小编Moh*_*hin的帖子

MenuItemCompat.getActionView始终返回null

我刚刚实现了v7 AppCompat支持库,但在MenuItemCompat.getActionView我测试的每个Android版本中总是返回null(4.2.2,2.3.4 ....)

SearchView显示在操作栏上,但它不响应触摸操作,不会展开,以显示其EditText与就像一个简单的图标.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    if (searchView != null) {
        SearchViewCompat.setOnQueryTextListener(searchView, mOnQueryTextListener);
        searchView.setIconifiedByDefault(false);
        Log.d(TAG,"SearchView not null");
    } else
        Log.d(TAG, "SearchView is null");
    }
    return super.onCreateOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)

menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/action_search"
          app:showAsAction="always|collapseActionView"
          android:icon="@drawable/abc_ic_search"
          android:title="@string/action_bar_search"
          android:actionViewClass="android.support.v7.widget.SearchView"/>

    <item android:id="@+id/action_refresh"
          android:icon="@drawable/refresh"
          android:title="@string/action_bar_refresh"
          app:showAsAction="ifRoom"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

android searchview android-search android-actionbar-compat

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

在Android上继续之前等待线程

我有一个线程,但我想等待它完成后再继续下一步行动.我怎么能这样做?

            new Thread(

                    new Runnable() {

                        @Override
                        public void run() {
                            mensaje = getFilesFromUrl(value);
                        }

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

在这里我想放一些东西(但循环)知道什么时候线程完成并评估结果(mensaje)

            btnOk.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);
            lblEstado.setText(mensaje);

            if(mensaje.equals("Importado"))
                startActivity(new Intent(ScanUrl.this, MainActivity.class));
Run Code Online (Sandbox Code Playgroud)

java multithreading android

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

AlertDialog主题:如何更改项目文本颜色?

当我尝试应用标准主题时 AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(MyClass.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

builder.setTitle("Change");

String[] info= this.getResources().getStringArray(R.array.info);

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice);

arrayAdapter.addAll(info);

builder.setSingleChoiceItems(arrayAdapter, ....
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

注意事项是我没有问题,builder.setItems(...)因为它的文本颜色是Black在应用主题时builder.setSingleChoiceItems(...)具有白色文本颜色.

任何快速修复?或者基于什么方式创建自定义主题AlertDialog.THEME_DEVICE_DEFAULT_LIGHT

我的自定义样式无法按预期工作:

<style name="AlertDialogCustomTheme" android:parent="android:Theme.Dialog">
    <item name="android:textColor">#7ABDFF</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>


    <!--THE FOLLOWING ITEMS HAVE NOT EFFECT ... !! -->

    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:textColorAlertDialogListItem">#A844BD</item>
    <item name="android:itemBackground">#7ABDFF</item>
</style>
Run Code Online (Sandbox Code Playgroud)

更新

@lopez答案是一个完整的解决方案,但我找到了我的问题的单行修复,一个自定义主题应用于清单中的活动:

<style name="MyTheme">
    <item name="android:textColorAlertDialogListItem">@android:color/black</item>
</style>
Run Code Online (Sandbox Code Playgroud)

android themes android-alertdialog

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

getParentFragment API 16

我们都知道getParentFragmentFragment是API 17推出.

那么如果我们想要在API 16及更低版本中获取父片段呢(考虑到我使用native Fragment支持FragmentStatePagerAdapter并且嵌套片段没有问题)

还有比我更好的方法吗?

在父母:

public class ParentFragment extends Fragment {

public static ParentFragment StaticThis;
...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

StaticThis = this;

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

在孩子:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
         parentFragment = (ParentFragment) getParentFragment();
else
         parentFragment = ParentFragment.StaticThis;
Run Code Online (Sandbox Code Playgroud)

android android-fragments

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

N层架构中的Autofac模块

目前我使用Autofac为IOC和在两个组合物根(一个用于前端和一个用于后端)我注册和解析组件跨越的Service,BusinessData层.

截至目前,我只有一个像'AccountingModule'.现在我要在应用程序中添加几个新模块,名称如下InventoryModule,......

我的问题是我应该在各层之间拆分每个模块类(解决方案1)还是为每个模块分别设置所有层(解决方案2)

解决方案1:

Service Layer

(AccountingMoudle, InventoryModule, ...)
Run Code Online (Sandbox Code Playgroud)
Business Layer

(AccountingMoudle, InventoryModule, ...)
Run Code Online (Sandbox Code Playgroud)
Data Layer

(AccountingModule, InventoryModule, ...)
Run Code Online (Sandbox Code Playgroud)

要么

解决方案2:

AccountingModule
(
 Service Layer,
 Business Layer,
 Data Layer
)
Run Code Online (Sandbox Code Playgroud)
InventoryModule
(
 Service Layer,
 Business Layer,
 Data Layer
)
Run Code Online (Sandbox Code Playgroud)

编辑1

+-----------------------------+                              +----------------------------+
+--+AccountingServiceComponent                               +-+InventoryServiceComponent
|                                      Weak Dependency       |
+--+AccountingBusinessComponent      <------------------+    +-+InventoryBusinessComponent
|                                                            |
+--+AccountingDataComponent                                  +-+InventoryDataComponent
       +                                                         +
       +-+ GetDocumentByID(int id)                               +--+GetProductByID(int id)
       |                                                         |
       +-+ SaveDocument(Document d)                              +--+SaveProduct(Product p)
Run Code Online (Sandbox Code Playgroud)

编辑2 …

c# dependency-injection inversion-of-control autofac autofac-module

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

Xml Serialization无法写入'x'类型的对象

我想使用XmlFormatter将类序列化为MVC Web API中的响应,但是在创建共振时我得到以下异常:

MediaTypeFormatter formatter = Configuration.Formatters.XmlFormatter;
HttpResponseMessage resp = Request.CreateResponse<Model>(HttpStatusCode.OK, value: modelObject, formatter: formatter);
Run Code Online (Sandbox Code Playgroud)

例外:

The configured formatter 'System.Web.Http.Tracing.Tracers.XmlMediaTypeFormatterTracer' cannot write an object of type 'Model'.
Run Code Online (Sandbox Code Playgroud)

怎么了 ?

api asp.net-mvc xml-serialization web

8
推荐指数
1
解决办法
2451
查看次数

Java等效于带有SHA-1的.NET RSACryptoServiceProvider

我在C#中有以下数据签名代码

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

string PrivateKeyText = "<RSAKeyValue><Modulus>....</D></RSAKeyValue>";

rsa.FromXmlString(PrivateKeyText);

string data = "my data";        

byte[] SignedByteData = rsa.SignData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider());
Run Code Online (Sandbox Code Playgroud)

我想在Java(Android)中重现相同的代码:

String modulusElem = "...";
String expElem = "...";

byte[] expBytes = Base64.decode(expElem, Base64.DEFAULT);
byte[] modulusBytes = Base64.decode(modulusElem, Base64.DEFAULT);

BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger exponent = new BigInteger(1, expBytes);

try {
    KeyFactory factory = KeyFactory.getInstance("RSA");

    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");

    String data = "my data";

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] hashedData = md.digest(data.getBytes("UTF-8"));

    RSAPublicKeySpec pubSpec = new …
Run Code Online (Sandbox Code Playgroud)

.net c# java android cryptography

8
推荐指数
1
解决办法
2820
查看次数

在CheckAccessCore之后,WCF访问被拒绝的异常永远不会返回到客户端

我有一个配置了TransportWithMessageCredential安全性的WCF服务.所有三种实现的IAuthorizationPolicy,ServiceAuthenticationManagerServiceAuthorizationManager到位,有效.

serviceHost.Credentials.ServiceCertificate.SetCertificate("CN=localhost");
serviceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomValidator();
serviceHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;

serviceHost.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.Custom;
serviceHost.Authorization.ServiceAuthorizationManager = new MyServiceAuthorizationManager();
serviceHost.Authentication.ServiceAuthenticationManager = new MyServiceAuthenticationManager();
serviceHost.Authorization.ExternalAuthorizationPolicies = 
    new System.Collections.ObjectModel.ReadOnlyCollection<System.IdentityModel.Policy.IAuthorizationPolicy>(
        new MyAuthorizationPolicy[] { new MyAuthorizationPolicy() });
Run Code Online (Sandbox Code Playgroud)

据我所知,在ServiceAuthorizationManager继承的类中,在CheckAccessCore方法中,return false语句表示拒绝访问.这一切都很好,直到我希望客户端知道他有一个访问被拒绝的异常,其中服务停止向客户端返回任何内容,似乎服务线程被绞死.

我尝试了各种各样try catch的客户端甚至添加了一个FaultContract操作,但问题抵制.

我只能看到诊断工具中的两个错误事件.

在此输入图像描述

我的实现中缺少什么来获取服务通知用户访问被拒绝错误?

更新

值得注意的是,我说我正在使用RoutingService,现在我猜真正的原因RoutingService是以某种方式吃异常,但我不知道究竟发生了什么.即使我介入了所有可能的方法,但我没有找到它.

更新2

IErrorHandler到位了:

   public class ServiceErrorHandler : IErrorHandler
    {
        public bool HandleError(Exception error)
        {
            //You can log …
Run Code Online (Sandbox Code Playgroud)

c# security wcf access-denied wcf-security

8
推荐指数
1
解决办法
1070
查看次数

通过资产字体更改PreferenceFragment字体

为了有自定义字体在PreferenceFragment每个偏好,我不得不写一个新的自定义类的每个偏好类型(CustomSwitchPreference,CustomEditTextPreference,CustomListPreference,...),并设置其字体onBindView的方法.

它有效,但这是最好的解决方案吗?不短吗?

@Override
public void onBindView(View view){
    super.onBindView(view);
    TextView title = (TextView) view.findViewById(android.R.id.title);
    TextView summary = (TextView) view.findViewById(android.R.id.summary);
    Utils.setFont(context, title, customfont);
    Utils.setFont(context, summary, customfont);
}

public class Utils{
    public static boolean setFont(Context context, TextView tv, String fontAssetName) {
        Typeface font = Typeface.createFromAsset(context.getResources().getAssets(), fontAssetName);
        if (font != null) {
            tv.setTypeface(font);
            return true;
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法改变PreferenceFragment包含对话框的所有部分的字体?

customization android android-preferences android-fragments

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

多行TextView边缘渐变

我有一个多行的长文本.我希望将TextView内部包裹起来,ScrollView以便在用户滚动文本时ScrollView应用TEMPORARY alpha渐变的边缘TextView.褪色根本没有出现.

我故意为高度设置一个很高的值,TextView但是它不能用于此WRAP_CONTENT或者甚至不能MATCH_PARENT.

<ScrollView android:layout_width="match_parent"
            android:layout_height="300dp"
            android:requiresFadingEdge="vertical"
            android:fadingEdgeLength="200dp">
    <TextView
            android:layout_width="match_parent"
            android:layout_height="1000dp"
            android:text="He has been involved with various online encyclopedia projects.[7]\nHe is the former editor-in-chief of Nupedia,[8] chief organizer (2001–02) of its successor, Wikipedia,[9] and founding editor-in-chief of Citizendium.[10]\nFrom his position at Nupedia, he assembled the process for article development.[11] Sanger proposed implementing a wiki, which led directly to the creation of Wikipedia.[12]\nInitially Wikipedia was a …
Run Code Online (Sandbox Code Playgroud)

android fade scrollview textview

6
推荐指数
0
解决办法
1642
查看次数