当我onScrollListener
为我设置时ListView
,它会调用onScroll
.这会导致崩溃,因为某些事情尚未初始化.
这是正常的吗?注意:即使没有触摸手机,也会发生这种情况.
public class MainActivity1 extends Activity implements OnClickListener, OnScrollListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
ListView lv = (ListView)findViewById(R.id.listview1);
lv.setOnScrollListener(this);
...
}
...
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount){
if( firstVisibleItem+visibleItemCount == totalItemCount ){
pullContactList();
}
}
Run Code Online (Sandbox Code Playgroud) 当脚本因异常退出时,打开的文件(和其他资源)会自动关闭吗?
我想知道在异常处理期间是否需要关闭我的资源.
**编辑:更具体地说,我在我的脚本中创建一个简单的日志文件.我想知道是否需要关注在异常情况下显式关闭日志文件.由于我的脚本有一个复杂的,嵌套的,try/except块,这样做有点复杂,所以如果python,CLIB或操作系统要在脚本崩溃/出错时关闭我的文本文件,我不想浪费太多时间确保文件关闭.
如果Python手册中有一个部分讨论这个问题,请转介我,但我找不到它.
我为我的一个操作栏MenuBar设置了一个自定义视图
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.loop_thread, menu);
ImageView iv = (ImageView)(getLayoutInflater().inflate(R.layout.image_container, null));
menu.findItem(R.id.action_filter).setActionView(iv);
...
Run Code Online (Sandbox Code Playgroud)
但是当我在我的应用程序中点击该菜单项时,onOptionsItemSelected()
不会被调用.(我已经通过在方法开头设置断点来验证这一点).如果我点击其他按钮,在操作栏中,会调用onOptionsItemSelected().
这是自定义视图的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ibtnFilterMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:src="@drawable/btn_action_down_arrow"
style = "@android:style/Widget.ActionButton"/>
Run Code Online (Sandbox Code Playgroud)
我也尝试在菜单xml文件上设置Action视图:
<item android:id="@+id/action_filter"
android:title="@string/filter_options"
android:actionLayout="@layout/image_container"
android:orderInCategory="100"
android:showAsAction="ifRoom" />
Run Code Online (Sandbox Code Playgroud)
注:我知道我可以使图像可点击,并手动设置onClickListener它,但对于学习的缘故,我想要去的这条底线.
表现:
<application
android:name="..."
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme"
tools:replace="icon,label,theme,name,allowBackup">
Run Code Online (Sandbox Code Playgroud)
在mipmap-anydpi-v26
我定义的文件夹下ic_launcher.xml
:
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/white"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
Run Code Online (Sandbox Code Playgroud)
的build.gradle:
compileSdkVersion = 26
buildToolsVersion = "25.0.2"
supportLibVersion = "25.3.1"
targetSdkVersion = 25
minSdkVersion = 18
Run Code Online (Sandbox Code Playgroud)
而且,我正在使用android studio 3.0
但最终结果是我得到了一个默认的android图标而不是我提供的图标.
我也尝试将前景png放在所有的密度文件夹(mipmap-xhdpi等)中,虽然我在执行此操作时只使用相同的png进行测试
我创建了一个默认的 KMP 项目,没有进行任何更改。它在 Android 上运行良好,但是当我尝试在 iOS 上运行它时,它告诉我找不到 gradle 插件:
A problem occurred configuring root project 'KMPSandBox'.
> Could not resolve all files for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:7.4.0-rc01.
Required by:
project : > com.android.application:com.android.application.gradle.plugin:7.4.0-rc01
project : > com.android.library:com.android.library.gradle.plugin:7.4.0-rc01
> No matching variant of com.android.tools.build:gradle:7.4.0-rc01 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5' …
Run Code Online (Sandbox Code Playgroud) 我在GCM上使用了一个名为"doInBackground"的函数的android教程,他们将这个函数定义如下:
private void registerInBackground() {
new AsyncTask() {
@Override
protected String doInBackground(Void... params) {
//do stuff
}
@Override
protected void onPostExecute(String msg) {
//do stuff
}
}.execute(null, null, null);
}
Run Code Online (Sandbox Code Playgroud)
但是当我将自己的代码粘贴并粘贴到eclipse中时,它会抱怨并说我没有实现doInBackground.这是因为期望doInBackground具有"Object"的输入参数,并且它不识别由于其输入参数是void而定义的那个.现在,如果我将async任务声明为一个类,我会把它放在<Void, Void, String>
它前面,这会告诉编译器我希望我的doInBackground将VOid作为输入.但是当我把<Void, Void, String>
"新的AsyncTask()" 放在前面时:
private void registerInBackground() {
new AsyncTask() <Void, Void, String>{
Run Code Online (Sandbox Code Playgroud)
我收到编译器错误:
Syntax error on tokens, delete these tokens
Run Code Online (Sandbox Code Playgroud) 我已经使用LinearSnapHelper实现了一个水平的recyclelerView,以实现选择特定配置的UI输入.有点像旧学号码选择器/选择器或旋转器.中心的项目是选定的位置.
它工作得很好,所有,但这是问题所在.在初始启动时,我需要以编程方式设置回收器视图的位置,以便所选项(其索引从磁盘加载)位于中心位置.
.scrollToPosition()
不会工作,因为它将所选项目放在开头.
现在我知道我可以做所有的数学运算并计算x坐标并手动设置它,但这就是很多冗余的工作,因为LinearSnapHelper已经这样做了,我觉得应该有一种方法来重用那个逻辑,但是实际上发起了一场狂欢.
我需要类似的东西 LinearSnapHelper.snapToPosition()
我有一个TextView
带maxLines:5
和ellipsize:end
应用,我还使用setMovementMethod(LinkMovementMethod.getInstance())
上TextView
,使可点击的链接(HTML内容).
上述所有组合的组合禁用了被截断的文本和要追加的"..."后缀.
知道出了什么问题以及如何解决它?
如果不设置移动方法,一切都按预期工作.
关于赏金的更新:寻找除手动设置省略号之外的解决方案
根据python文档,Exception派生自BaseExceptions,我应该将它用于用户定义的异常.所以我有:
class VisaIOError(Exception):
def __init__(self, error_code):
abbreviation, description = _completion_and_error_messages[error_code]
Error.__init__(self, abbreviation + ": " + description)
self.error_code = error_code
Run Code Online (Sandbox Code Playgroud)
和
raise(visa_exceptions.VisaIOError, status)
Run Code Online (Sandbox Code Playgroud)
但我得到(trackback片段):
File "C:\Python32\Lib\site-packages\pyvisa\vpp43.py", line 400, in check_status
raise(visa_exceptions.VisaIOError, status)
TypeError: exceptions must derive from BaseException
Run Code Online (Sandbox Code Playgroud)
注意:我正在将代码从python 27转换为32
阅读完这篇文章后,我开始考虑与Volley的内存泄漏.通常,使用Volley实现的侦听器具有对外部类(活动)的隐式或显式引用.例如:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
updateLayout();
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下有一个隐式引用...或者我可能想创建一个自定义JsonObjectRequest来内化响应处理,并需要在其构造函数中传入对调用活动的引用.
现在让我说我开始一个Web请求,但在响应回来之前,我离开了启动请求的活动.根据我的理解,JsonObjectRequest对象将保留对我的活动的引用并防止它被垃圾收集.
- 我正确理解这一点,这是一种合理的恐惧吗?
- Volley库会自动处理这个吗?
- 如果我正在创建一个自定义的JsonObjectRequest并传入一个"this"(对活动的引用),我是否需要为该活动创建一个WeakReference?