我正在尝试将 onClick 方法添加front()到我的按钮。但是,当我单击按钮时,它会返回此错误:
java.lang.IllegalStateException: Could not find method front(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'front'
Run Code Online (Sandbox Code Playgroud)
这是我的 xml:
<Button
android:id="@+id/front"
android:onClick="front"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text" />
Run Code Online (Sandbox Code Playgroud)
注册.java:
public class Register extends AppCompatActivity {
private Button front;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
front = (Button) findViewById(R.id.front);
}
private void front(View v) {
Toast.makeText(Register.this, "String", Toast.LENGTH_LONG).show();
}
}
Run Code Online (Sandbox Code Playgroud)
知道问题是什么吗?
我正在运行Gunicorn / Nginx的Ubuntu 16.04 Digital Ocean服务器上运行Django项目。我有我的整个项目,除了我的settings.py文件,所以我想现在就添加它-但是不想对其进行硬编码SECRET_KEY-所以我想定义一个环境变量,如Django docs中所述SECRET_KEY = os.environ['SECRET_KEY']。
我在哪里定义这个变量?是在我的gunicorn配置文件(/etc/systemd/system/gunicorn.service)中吗
我正在使用PIL压缩上传的图片(FileField)。但是我遇到一个错误,我认为这是双重保存的问题?(保存我的图像,然后保存包括该图像的整个表单)。我想commit=False在保存图像时执行,但没有出现,这是可能的。这是我的代码:
...
if form_post.is_valid():
instance = form_post.save(commit=False)
instance.user = request.user
if instance.image:
filename = instance.image
instance.image = Image.open(instance.image)
instance.image.thumbnail((220, 130), Image.ANTIALIAS)
instance.image.save(filename, quality=60)
instance.save()
Run Code Online (Sandbox Code Playgroud)
'JpegImageFile' object has no attribute '_committed'在最后一行(instance.save())返回错误
有人可以找出问题所在吗?-知道我该如何解决吗?
完整回溯:
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/Users/zorgan/Desktop/project/site/post/views.py" in post
68. …Run Code Online (Sandbox Code Playgroud) 尝试在我的终端中升级 pip。这是我的代码:
(env) macbook-pro83:zappatest zorgan$ pip install --upgrade pip
Could not fetch URL https://pypi.python.org/simple/pip/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:645) - skipping
Requirement already up-to-date: pip in ./env/lib/python3.5/site-packages
You are using pip version 8.1.2, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Run Code Online (Sandbox Code Playgroud)
知道问题是什么吗?我也得到了同样的错误There was a problem confirming the ssl certificate,当我执行pip install django。
编辑 pip install --upgrade pip -vvv returns …
我有一个RadioGroup这样的:
<RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/player_age"
android:id="@+id/gender"
android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="18dp">
<RadioButton
android:text="Male"
android:layout_width="wrap_content"
android:layout_height="wrap_content" tools:layout_editor_absoluteY="328dp"
tools:layout_editor_absoluteX="58dp" android:id="@+id/male"/>
<RadioButton
android:text="Female"
android:layout_width="wrap_content"
android:layout_height="wrap_content" tools:layout_editor_absoluteY="328dp"
tools:layout_editor_absoluteX="224dp" android:id="@+id/radioButton4"/>
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)
我已经通过的文档看起来RadioGroup和getCheckedRadioButtonId似乎是最合适的功能使用:
返回该组中所选单选按钮的标识符。空选择时,返回值为 -1。
但是它返回一个无用的Int而不是 id 的RadioButton:
import kotlinx.android.synthetic.main.activity_player_details.*
override fun onClick(v: View?) {
val name: String = player_name.text.toString()
val age: Int = player_age.text.toString().toInt()
val gender: Int = gender.checkedRadioButtonId
println(name)
println(age)
println(gender) // prints 2131361895
}
}
Run Code Online (Sandbox Code Playgroud)
知道如何检索id已检查的实际值RadioButton吗?
在Kotlin中函数名之前的<T>类型参数的用法是什么?
例:
fun <T> MutableList<T>.swap(index1: Int, index2: Int) {
val tmp = this[index1]
this[index1] = this[index2]
this[index2] = tmp
}
Run Code Online (Sandbox Code Playgroud)
参考<T>上面的第一个.
当我表演时:
val array = arrayListOf<String?>(null, "hello", null)
array.sortBy { it == null }
println(array)
Run Code Online (Sandbox Code Playgroud)
我希望它会null首先打印值,因为这是我指定的选择器。但是,println(array)返回[hello, null, null]。
为什么是这样?
我创建了一个自定义:Preference ClickPreference
class ClickPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs), View.OnClickListener {
override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
val box = holder?.itemView
box?.setOnClickListener(this)
}
override fun onClick(v: View?) {
action(title)
}
fun action(title: CharSequence){
when (title){
"email" -> {
...
}
"Logout" -> {
LoginManager.getInstance().logOut()
val intent = Intent(context, MainActivity::class.java) // context is from getContext()
startActivity(intent)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我在上得到此错误startActivity():
我不明白为什么它是错误的,因为它是有效的Intent构造函数。
任何的想法?
当我在我的 gradle 中添加以下依赖项时:
...
implementation 'com.github.imperiumlabs:GeoFirestore-Android:v1.5.0'
}
Run Code Online (Sandbox Code Playgroud)
我得到:
ERROR: Failed to resolve: com.github.imperiumlabs:GeoFirestore-Android:v1.5.0
Show in Project Structure dialog
Affected Modules: app
Run Code Online (Sandbox Code Playgroud)
我尝试安装以前的版本 (1.4.0),但遇到了同样的错误。
有没有办法解决?
我的协程正在主线程上运行,这是在我的协程上下文中指定的:
class ClickPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs), CoroutineScope, View.OnClickListener {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main
override fun onClick(v: View?) {
when (key){
"logout" -> {
CoroutineScope(coroutineContext).launch {
CustomApplication.database?.clearAllTables()
Log.d("MapFragment", "Cleared Tables")
}
if (Profile.getCurrentProfile() != null) LoginManager.getInstance().logOut()
FirebaseAuth.getInstance().signOut()
val intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我仍然收到此错误:
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
Run Code Online (Sandbox Code Playgroud)
在我上面对数据库的协程调用CustomApplication.database?.clearAllTables()中Room
这是我的CustomApplication: …