我正在阅读有关此问题的stackoverflow,但我仍然没有找到解决方案.我注意到有时,我的应用程序会抛出此错误:
java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:962)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:599)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
...
Run Code Online (Sandbox Code Playgroud)
我有一个名为DatabaseHelper.java的文件,使用这种方法获取它的实例:
public static DatabaseHelper getInstance(Context context) {
if (mInstance == null) {
mInstance = new DatabaseHelper(context.getApplicationContext());
}
return mInstance;
}
Run Code Online (Sandbox Code Playgroud)
然后我有像这样的方法(它在行cursor.moveToFirst()中崩溃与该错误).它几乎从不崩溃,但有时它会崩溃.
public Profile getProfile(long id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_PROFILES + " WHERE " + KEY_PROFILES_ID + " = " + id;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping …
Run Code Online (Sandbox Code Playgroud) 首先,我遵循Flux架构.
我有一个指示器显示几秒钟,例如:30秒.每一秒它显示1秒减少,所以29,28,27直到0.当到达0时,我清除间隔,使其停止重复.而且,我触发了一个动作.调度此操作后,我的商店会通知我.因此,当发生这种情况时,我将间隔重置为30秒,依此类推.组件看起来像:
var Indicator = React.createClass({
mixins: [SetIntervalMixin],
getInitialState: function(){
return{
elapsed: this.props.rate
};
},
getDefaultProps: function() {
return {
rate: 30
};
},
propTypes: {
rate: React.PropTypes.number.isRequired
},
componentDidMount: function() {
MyStore.addChangeListener(this._onChange);
},
componentWillUnmount: function() {
MyStore.removeChangeListener(this._onChange);
},
refresh: function(){
this.setState({elapsed: this.state.elapsed-1})
if(this.state.elapsed == 0){
this.clearInterval();
TriggerAnAction();
}
},
render: function() {
return (
<p>{this.state.elapsed}s</p>
);
},
/**
* Event handler for 'change' events coming from MyStore
*/
_onChange: function() {
this.setState({elapsed: this.props.rate}
this.setInterval(this.refresh, 1000);
} …
Run Code Online (Sandbox Code Playgroud) 我有一个带有3个单选按钮的表单,如下(假名):
<form className="myForm" onSubmit={this.done}>
<input className="myRadio" checked={?rue} type="radio" name="myRadio" onChange={this.change} value="value1"
<input className="myRadio" type="radio" name="myRadio" onChange={this.change} value="value2"
<input className="myRadio" type="radio" name="myRadio" onChange={this.change} value="value3"
<input type="submit" className="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
我正在努力测试onChange和onSubmit事件.
inputs = TestUtils.scryRenderedDOMComponentsWithClass(MyComponentRendered, 'myRadio');
myForm = TestUtils.findRenderedDOMComponentWithClass(MyComponentRendered, 'myForm');
Run Code Online (Sandbox Code Playgroud)
我有一个测试:
it("changes the checked state when clicked", function() {
MyComponent.change = jest.genMockFunction();
expect(inputs[0].getDOMNode().checked).toBe(true);
TestUtils.Simulate.change(inputs[1], {target: {value: 'value2'}});
expect(inputs[0].getDOMNode().checked).toBe(false);
expect(inputs[1].getDOMNode().checked).toBe(true);
expect(inputs[2].getDOMNode().checked).toBe(false);
expect(MyComponent.change).toBeCalled(); //Fails
expect(MyComponent.change.mock.calls.length).toBe(1); //Fails too
});
Run Code Online (Sandbox Code Playgroud)
除了应该调用的函数(MyComponent.change)之外,它是有效的,但它不是.
我对onSubmit也有一个测试:
it("saves on submit", function()
MyComponent.done = jest.genMockFunction();
MyComponent.insideDone = jest.genMockFunction();
TestUtils.Simulate.submit(myForm);
expect(MyComponent.done).toBeCalled(); //Fails
expect(MyComponent.insideDone).toBeCalled(); //Success …
Run Code Online (Sandbox Code Playgroud) 我在使用小部件时遇到了困难.从http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#cellstable上读取下表:
所以我的小部件想要看起来像一个发射器,它将minHeigth和minWidth作为40dp.到目前为止,我所做的是使用图标作为启动器,192px,144px,96px,72px和48px.
但我无法获得作为图标发射器的确切感觉(文字风格,动人风格......).我可以尝试猜测但是......在哪里可以找到启动器布局的源代码?我找不到它.在与其他图标进行比较时,查看小部件的外观:
我想尝试的另一件事是制作绿色图标以填充所有单元格(作为背景),然后填充另一个图像和文本.我也不能让它看起来不错,我想知道唯一的方法是在这里使用9patch图像......
到目前为止,这是我的小部件布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/appwidget_button"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:src="@drawable/widget_1_background"/>
<TextView
android:id="@+id/appwidget_text"
android:typeface="sans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="@android:color/white"
android:textSize="12sp"
android:singleLine="true"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
建议?
android widget android-widget android-layout android-launcher
在新的Material Design(使用AppCompat)中,我试图将一些动作图标与新工具栏的抽屉图标对齐.就像是:
我想我正在正确地查看所有指标......
问题是我不能完美地对齐它,因为在那之后剩下16px的填充,图标应该开始,但图标本身也有一些"填充"(来自材料github的图标),如:
也许是愚蠢但我不知道我错过了什么.如何才能考虑图像内部的填充以正确对齐它?对齐单选按钮或复选框与该操作栏图标有同样的问题.
我尝试了这段代码:
<RelativeLayout
android:id="@+id/image_button"
android:layout_width="72dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="32dp"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:src="@drawable/ic_label_grey600_48dp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
但是看起来并不符合我(就像我发布的第一张截图一样).这里也看起来完美对齐:
看起来喜欢考虑的东西超过72px和16px的填充.
好的,让我向您展示更多示例和一些代码.我的列表项目具有带标签文本的单选按钮的成像.
<RadioButton
style="?android:textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:paddingLeft="32dp"
android:singleLine="true"/>
Run Code Online (Sandbox Code Playgroud)
结果:
我实际上没有看到规则将marginLeft和paddingLeft设置为正确的值而不使用自定义按钮作为单选按钮(并且知道该按钮的尺寸)... < - 这是一个不好的选项,因为颜色按钮取自我的accentColor(材料的新东西).
它看起来似乎新的Gmail应用程序面临一些对齐问题(当我选择邮件时,1与邮件标题不再对齐):
android android-layout android-icons android-actionbar material-design
我正在使用Google提供的应用内结算的最新版本文件.今天我下载应用程序时手机出现错误(我试图重复这些步骤,但错误不再出现).
让我们说在我的MainActivity中我有一个按钮,用户可以购买一些应用内商品.我的活动调用这个构造函数:
public void launchPurchaseFlow(Activity act, String sku, int requestCode, OnIabPurchaseFinishedListener listener) {
launchPurchaseFlow(act, sku, requestCode, listener, "");
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式:
mHelper.launchPurchaseFlow(MainActivity.this, SKU_UNLOCKED, RC_REQUEST,
mPurchaseFinishedListener, "");
Run Code Online (Sandbox Code Playgroud)
今天我得到了这个NullPointerException错误:
java.lang.NullPointerException
at xxx.xxx.xxxxx.util.IabHelper.launchPurchaseFlow(IabHelper:386)
at xxx.xxx.xxxxx.util.IabHelper.launchPurchaseFlow(IabHelper:338)
at xxx.xxx.xxxxx.MainActivity$6.onClick(MainActivity:422)
Run Code Online (Sandbox Code Playgroud)
MainActivity的第422行是mHelper.launch ...... IabHelper的第338行是构造函数.
然后在launchePurchaseFlow中:
386行:
Bundle buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, itemType, extraData);
Run Code Online (Sandbox Code Playgroud)
这是try-catch块的内部:
try {
logDebug("Constructing buy intent for " + sku + ", item type: " + itemType);
Bundle buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, itemType, extraData);
int response = getResponseCodeFromBundle(buyIntentBundle);
if (response != BILLING_RESPONSE_RESULT_OK) { …
Run Code Online (Sandbox Code Playgroud) java android nullpointerexception in-app-billing android-billing
我正在使用Grunt构建一个React项目,我想要'dev'和'prod'的味道.正如反应文档所说:
要在生产模式下使用React,请将环境变量NODE_ENV设置为production.建议使用UglifyJS执行死代码消除的minifier,以完全删除开发模式中存在的额外代码.
我是非常新的使用grunt,browserify和东西,但让我们看看.我遇到的第一个问题是envify,我用它作为变换:
browserify: {
options: {
transform: ['reactify'],
extensions: ['.jsx']
},
dev:{
options: {
watch: true //Uses watchify (faster)
},
src: ['js/app.js'],
dest: 'js/bundle.js'
},
/**
* To use React in production mode, set the environment variable NODE_ENV to production.
* A minifier that performs dead-code elimination such as UglifyJS is
* recommended to completely remove the extra code present in development mode.
**/
prod: {
options: {
transform: ['envify'] //How to set up NOD_ENV='production' ?
}, …
Run Code Online (Sandbox Code Playgroud) 所以我有以下小提琴,它将文本中的省略号设置为两行.然后我想要一个与文本内联的"更多"链接.
http://jsfiddle.net/csYjC/2876/
因此,如果我们的文本有两行以上,它应该如下所示:
那是对的.然而:
这是不正确的(应该与文本内联).
代码如下:
<div class="text">
<div>Lorem ipsum dolor sit amet, Lorem Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet, Lorem Lorem i</div>
<a href="#">More</a>
</div>
Run Code Online (Sandbox Code Playgroud)
和css:
.text{
display: inline;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
line-height: 24px; /* fallback */
max-height: 48px; /* fallback */
-webkit-line-clamp: 2; /* number of lines to show */
-webkit-box-orient: vertical;
}
.text a {
position: absolute;
}
Run Code Online (Sandbox Code Playgroud)
我想一定很容易......提前谢谢你.
仅仅为了在Android Lollipop上扩展CheckBoxPreference或SwitchPreference,小部件(复选框或开关)将不再具有动画.
我想扩展SwitchPreference以强制api <21使用SwitchCompat而不是他们使用的默认值(这显然是错误的).
我正在使用新的AppCompatPreferenceActivity,appcompat-v7:22.1.1
但这似乎不会影响交换机.
问题是,只需扩展这些类,而不添加任何自定义布局或窗口小部件资源布局,动画就会消失.
我知道我可以编写两个我的preference.xml实例(在内部值-v21上)并且它可以工作......但我想知道为什么会发生这种情况,如果有人知道没有两个preference.xml的解决方案.
代码示例:
public class SwitchPreference extends android.preference.SwitchPreference {
public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public SwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SwitchPreference(Context context) {
super(context);
}
}
Run Code Online (Sandbox Code Playgroud)
对于CheckBoxPreference这个或相同,然后使用:
<com.my.package.SwitchPreference />
将使Lollipop设备中的动画消失.
-
我尝试了SwitchPreference另一件事(我能与CheckBoxPreference)是为了给使用默认ID的布局,但@android:id/switchWidget
不公开,而@android:id/checkbox
为.我也知道我可以使用a <CheckBoxPreference />
并给出一个实际上是SwitchCompat的小部件布局,但我想避免这种情况(混淆名称).
android android-preferences material-design android-5.0-lollipop
所以我有一个很重要的组成部分:
<form>
<FirstComponent value={this.state.firstValue}/>
<SecondComponent value={this.state.secondValue}/>
{more components here}
<input type="submit" ... />
</form>
Run Code Online (Sandbox Code Playgroud)
这种形式的组件使用监听一个商店更新它的值firstAction
,secondAction
等
注意:组件根据返回的store.getState()更新其状态 {firstValue: something, secondValue: something, etc}
所以让我们说我FirstComponent
是一个输入:
<input type="text" value={this.props.value}
onChange={(e)=>this.props.firstAction(e.target.value)}
</input>
Run Code Online (Sandbox Code Playgroud)
好的,所以onChange
触发了支柱firstAction
,它实际上是Flux动作,它将更新我的商店并使表格重新渲染.我有两个好东西,当用户提交表单时,我可以检查我的商店中的FirstComponent的值,并且我还控制来自父组件的所有状态.
但是,onChange
每次用户键入一个字符时,此回调将调用一个操作(因此它可以产生大量调用,因此重新渲染)< - 这会引发严重的性能问题吗?
相反,我可以使用refs,当用户按下提交按钮时,获取this.refs.myFirstComponent.state
...我也将获得该值(这将是不受控制的组件?)但这听起来不像社区的推荐.
所以我的问题是,上面描述的第一种方法是一个很好的方法吗?我该如何优化它?那么只应该影响FirstComponent的重新渲染不会使SecondComponent等重新渲染?是shouldComponentUpdate
唯一的出路吗?
编辑1:
第一种方法我面临一个问题......我使用WebdriverIO进行e2e测试,在文本字段中添加一个值:http://webdriver.io/api/action/setValue.html
我不知道为什么,但如果我试图在输入中添加"测试"这个词,webdriver只会添加最后一个字母.如果不使用状态/存储,这个问题就消失了.但是,如果我在内部拥有状态FirstComponent
,例如:
<input type="text" value={this.state.value}
onChange={(e)=>this.setState({firstValue: e.target.value})}
onBlur={()=>this.props.callback(this.state.firstValue)}
</input>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,组件在键入时似乎反应更快(仅渲染自身),然后,当用户移除焦点时,它会更新商店.我不得不说,我不喜欢这种方法,因为它不遵循你的状态(我觉得我复制状态)的模式但是它似乎工作得更快更重要:我的e2e测试工作.还有什么想法吗?