在这篇文章中,我们将为您详细介绍android–RoomInvalidationtracker初始化两次的内容,并且讨论关于android中初始化sharedpreferences的相关问题。此外,我
在这篇文章中,我们将为您详细介绍android – Room Invalidation tracker初始化两次的内容,并且讨论关于android中初始化sharedpreferences的相关问题。此外,我们还会涉及一些关于Android Browser学习一 application的初始化、Android In-App Billing Error InvalidKeySpecException、Android log cat显示android.content.pm.PackageManager $NameNotFoundException:应用程序包com.google.android.backup、Android studio启动报Error:(1, 0) Plugin with id ''com.android.application'' not found.的知识,以帮助您更全面地了解这个主题。
本文目录一览:
android – Room Invalidation tracker初始化两次(android中初始化sharedpreferences)
我有一个水平回收站视图,其中包含自定义项目.每个项目都可以在Recycler视图中保存当前项目的位置.我想使用拖放移动项目时更新项目位置.但是当水平视图中有三个以上的项目时,数据会被删除.请帮帮我.
Source Code
这是我在Logcat中得到的:
E/ROOM: Invalidation tracker is initialized twice :/.
E/Itemmoved: Counterfrom3
next item:to2
在onCreate中初始化数据库.
db = Room.databaseBuilder(getApplicationContext(),AppDatabase.class,DB_NAME)
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build();
RecyclerView适配器代码.
@Override
public boolean onItemmove(int fromPosition,int toPosition) {
String name = dataSet.get(fromPosition).getName();
//this will make "Add item" do not move from its first position..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (!(Objects.equals(name,"Add") || (toPosition == 0 && fromPosition == 1))) {
Collections.swap(dataSet,fromPosition,toPosition);
MoveItem(fromPosition,toPosition);
notifyItemmoved(fromPosition,toPosition);
return true;
}
}
return false;
}
移动项目时更新数据的代码.
public static void MoveItem(int fromPosition,int toPosition){
String name = data.get(fromPosition).getName(); //This gets the current item name in the view
String nexName = data.get(toPosition).getName(); //This gets the next item name in the view
ContentValues fromContentValues = new ContentValues();
fromContentValues.put("posItem",toPosition); //adding data to ContentValues
ContentValues toContentValues = new ContentValues();
toContentValues.put("posItem",fromPosition);
Log.e("Item moved",name + "from" + fromPosition + "\n" + "next item:" + "to" + toPosition);
db.beginTransaction();
try {
db.getopenHelper().getWritableDatabase().update(name,fromContentValues,"posItem =" + fromPosition,null);
db.getopenHelper().getWritableDatabase().update(nexName,toContentValues,"posItem =" + toPosition,null);
db.setTransactionSuccessful(); //setting Transaction Successful
} finally {
db.endTransaction(); // commit or rollback
db.close(); //closing database
}
}
解决方法
当我迁移数据库版本时,同样的错误E / ROOM:失效跟踪器被初始化两次,并杀死应用程序,并重新打开工作.当我开始使用Room v1.1.0时.
但是,如果我保持一切相同并回到使用Room v1.0.0,那么就不会出现这样的问题,一切都很完美.
所以,可能是Room v1.1.0问题
google issues

Android Browser学习一 application的初始化
Android Browser 是一个非常好的学习资料, 使用了自己设计的MVC架构,来管理书签, 浏览器 等各个功能,有具有一定的稳定性,知道我们学习
Browser是从Application开始启动的,启动流程如下:
代码很简单:
01 |
public class Browser extends Application { |
04 |
private final static String LOGTAG = "browser"; |
06 |
// Set to true to enable verbose logging. |
07 |
final static boolean LOGV_ENABLED = false; |
10 |
// Set to true to enable extra debug logging. |
11 |
final static boolean LOGD_ENABLED = true; |
15 |
public void onCreate() { |
18 |
Log.v(LOGTAG, "Browser.onCreate: this=" + this); |
21 |
// create CookieSyncManager with current Context |
23 |
CookieSyncManager.createInstance(this); |
24 |
//在application 启动的时候实例化浏览器的配置BrowserSetting, 和 一下需要预先 载入的东西 Preloader等待以后调用 |
25 |
BrowserSettings.initialize(getApplicationContext()); |
26 |
Preloader.initialize(getApplicationContext()); |
主要功能是设置一些Debug的开关,初始化一下全局的模块,包括:CookieSyncManager, BrowserSettings ,PreLoader.
那么看一下他们分别是用来做什么的
CookieSyncManager: 初始化浏览器全局的cookie, 这是Android系统的类. 这个我们在做WebView开发的时候可能也用过.
Android通过使用这个类来实现Ram和硬盘中cookie的同步,提高了cookie的访问速度
具体使用参考 http://whao189.iteye.com/blog/1121865
BrowserSettings :初始化浏览器的偏好设置, UA , WebView的设置 ,个人账户信息, 等
具体代码:
1 |
private BrowserSettings(Context context) { |
2 |
mContext = context.getApplicationContext(); |
3 |
mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);//用来取xml的数据 |
4 |
mAutofillHandler = new AutofillHandler(mContext); //账户个人信息 |
5 |
mManagedSettings = new LinkedList<WeakReference<WebSettings>>();//WebView的设置 |
6 |
mCustomUserAgents = new WeakHashMap<WebSettings, String>(); //UA |
7 |
mAutofillHandler.asyncLoadFromDb(); //异步载入个人信息 |
8 |
BackgroundHandler.execute(mSetup); //载入浏览器配置 |
有一个小点:根据dvm虚拟机的大小设置 内存缓存的网页数目 ,以及磁盘缓存
1 |
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); |
2 |
mFontSizeMult = metrics.scaledDensity / metrics.density; |
3 |
// the cost of one cached page is ~3M (measured using nytimes.com). For |
4 |
// low end devices, we only cache one page. For high end devices, we try |
5 |
// to cache more pages, currently choose 5. |
6 |
//这是获取dvm的一个大体值 16m 24m 32m ...然后设置浏览器缓存多少网页 |
7 |
if (ActivityManager.staticGetMemoryClass() > 16) { |
8 |
mPageCacheCapacity = 5; |
2 |
mWebStorageSizeManager = new WebStorageSizeManager(mContext, |
3 |
new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()), |
4 |
new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath())); |
将来BrowserSetting还会通过Controller和UI进行很多的交换,而且在Browser启动的时候是全局都要用到的,因此作出了全局唯一的单例
PreLoader: 如果Brower在后台,或者还在内存中,再次调用browser的时候就不会重新创建Webview等控件,提高启动的速度,主要是通过hold了一个含有webview的mSession,然后利用了application 部分内存不会轻易释放 来实现的. 具体后面会继续分析,
1 |
private Preloader(Context context) { |
2 |
mContext = context.getApplicationContext(); |
3 |
mHandler = new Handler(Looper.getMainLooper()); |
5 |
mFactory = new BrowserWebViewFactory(context); |
这样Application的启动就ok了
可以看到, 作者把一些App全局需要用到的东西放到了Application中初始化, 并且这些东西都是单例的,这一在app周期中他们将一直存在, 后面的组件只要调用getInstance就可以使用了.不过这里有个问题, 这些单例会占用app的内存直到app退出,所以全局初始化的东西应该尽量少一些.

Android In-App Billing Error InvalidKeySpecException
我正在尝试使用Trivial Gas教程的包装器来实现应用程序内购买.成功购买完成后应用程序崩溃,从那时起应用程序开始崩溃.
java.lang.IllegalArgumentException: java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D07209B:asn1 encoding routines:ASN1_get_object:too long
at com.nightowl.memory.Security.generatePublicKey(Security.java:85)
at com.nightowl.memory.Security.verifyPurchase(Security.java:65)
at com.nightowl.memory.IabHelper.queryPurchases(IabHelper.java:875)
at com.nightowl.memory.IabHelper.queryInventory(IabHelper.java:550)
at com.nightowl.memory.IabHelper.queryInventory(IabHelper.java:528)
at com.nightowl.memory.IabHelper$2.run(IabHelper.java:623)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D07209B:asn1 encoding routines:ASN1_get_object:too long
at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:101)
at com.android.org.conscrypt.OpenSSlrsAKeyFactory.engineGeneratePublic(OpenSSlrsAKeyFactory.java:47)
at java.security.KeyFactory.generatePublic(KeyFactory.java:171)
at com.nightowl.memory.Security.generatePublicKey(Security.java:80)
... 6 more
Caused by: java.lang.RuntimeException: error:0D07209B:asn1 encoding routines:ASN1_get_object:too long
at com.android.org.conscrypt.NativeCrypto.d2i_PUBKEY(Native Method)
at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:99)
... 9 more
我使用以下代码来调用它:
String base64EncodedPublicKey;
base64EncodedPublicKey= String.valueOf(R.string.myPubKey);
additionalSkuList = new ArrayList<String>();
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
if(i==0&&j>1)
break;
additionalSkuList.add(id[i][j]);
}
}
mHelper = new IabHelper(this,base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d("main","m here");
if (!result.isSuccess()) {
// Oh noes,there was a problem.
Log.d("main","Problem setting up in-app Billing: " + result);
}
// Hooray,IAB is fully set up!
isIAB = true;
mHelper.queryInventoryAsync(true,additionalSkuList,mQueryFinishedListener);
Log.d("main","" + isIAB + " " + isLoad);
}
});
IabHelper.QueryInventoryFinishedListener
mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,Inventory inventory)
{
Log.d("main","m here too");
if (result.isFailure()) {
// handle error
return;
}
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
if(i==0&&j>1)
break;
price[i][j]=inventory.getSkuDetails(id[i][j]).getPrice();
}
}
isLoad=true;
data.setPrice(price);
data.setDataLoad(true);
// update the UI
}
};
public void onPurchaseCall()
{
int loc[] = data.getItem();
mHelper.launchPurchaseFlow(this,id[loc[0]][loc[1]],100*loc[0]+loc[1],mpurchaseFinishedListener,id[loc[0]][loc[1]]);
}
IabHelper.OnIabPurchaseFinishedListener mpurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,Purchase purchase)
{
if (result.isFailure()) {
Log.d("main","Error purchasing: " + result);
return;
}
else if (purchase.getSku().equals(id[0][0])) {
data.setNoads(1);
mAdView.setVisibility(View.INVISIBLE);
data.setNotificationState(1);
data.setNoOfnotifications(2);
data.setNotificationMsg(0,"Purchase Complete");
data.setNotificationMsg(1,PurchaseMsg[0]);
// consume the gas and update the UI
}
}
};
任何人都可以帮我解决吗?我从日志中猜到的是我提供的公钥存在问题.但它第一次起作用(直到我成功购买才开始工作).我还得到了所有应用内商品的价目表.
我试图用测试帐户测试购买.成功完成购买后出现此错误,从那时起该应用程序未针对该帐户运行.
解决方法
不要使用String.valueOf(R.string.myPubKey),它会给你一个错误的值.而是使用getResources().getString(R.string.myPubKey_data))来获取您的公钥字符串.

Android log cat显示android.content.pm.PackageManager $NameNotFoundException:应用程序包com.google.android.backup
我是一个更新鲜的
android,当我尝试在avd中运行我的应用程序时,在日志猫中发现以下异常,显示不幸的是我的应用程序无法运行,请帮助
android.content.pm.PackageManager$NameNotFoundException: Application package com.google.android.backup not found
at android.app.ContextImpl.createPackageContextAsUser(ContextImpl.java:2172)
at android.app.ContextImpl.createPackageContext(ContextImpl.java:2148)
at android.content.Contextwrapper.createPackageContext(Contextwrapper.java:671)
at com.google.android.gms.backup.an.<init>(SourceFile:47)
at com.google.android.gms.backup.BackupTransportMigratorService.f(SourceFile:248)
at com.google.android.gms.backup.BackupTransportMigratorService.b(SourceFile:196)
at com.google.android.gms.backup.BackupTransportMigratorService.onHandleIntent(SourceFile:131)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
我的android清单如下所示
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cornex.criminalintent" >
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".CrimeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".CrimePagerActivity"
android:label="@string/app_name" >
</activity>
</application>
解决方法
要创建数据备份应用程序,您需要使用Google备份服务注册您的应用程序.这已在示例中解释.注册后,您必须在AndroidManifest.XML中指定其密钥
<application
android:allowBackup="true"
android:backupAgent="YOUR_BACKUP_AGENT">
<Meta-data
android:name="com.google.android.backup.api_key"
android:value="YOUR_API_KEY" />
</application>
Android提供BackUpAgentHelper类来处理数据备份的所有操作.要使用此类,您必须使用它扩展您的类.其语法如下:相应的备份代理可以实现,如下面的清单所示.
import android.app.backup.BackupAgentHelper;
public class YOUR_BACKUP_AGENT extends BackupAgentHelper {
}
要备份的持久性数据是两种形式之一.它可以是SharedPrefrences,也可以是File. Android在SharedPreferencesBackupHelper和FileBackupHelper各自的类中支持两种类型的备份.
要使用SharedPerefernceBackupHelper,您需要使用sharedPerefernces文件的名称实例化其对象.其语法如下 –
static final String File_Name_Of_Prefrences = "myPrefrences";
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this,File_Name_Of_Prefrences);
您需要做的最后一件事是通过指定备份密钥字符串和帮助程序对象来调用addHelper方法.其语法如下 –
addHelper(PREFS_BACKUP_KEY,helper);
addHelper方法将自动向给定数据子集添加帮助程序到代理程序的配置.
有关详细信息,请参阅this Tutorial和this article

Android studio启动报Error:(1, 0) Plugin with id ''com.android.application'' not found.
Android studio启动报Error:(1, 0) Plugin with id ''com.android.application'' not found.
今天的关于android – Room Invalidation tracker初始化两次和android中初始化sharedpreferences的分享已经结束,谢谢您的关注,如果想了解更多关于Android Browser学习一 application的初始化、Android In-App Billing Error InvalidKeySpecException、Android log cat显示android.content.pm.PackageManager $NameNotFoundException:应用程序包com.google.android.backup、Android studio启动报Error:(1, 0) Plugin with id ''com.android.application'' not found.的相关知识,请在本站进行查询。