我无法理解该bindServiceAsUser()方法是用来做什么的。有人可以解释一下吗?谷歌搜索似乎没有多大帮助。
public boolean bindService(Intent intent, ServiceConnection connection, int flags) {
return mContext.bindServiceAsUser(intent, connection, flags, UserHandle.OWNER);
}
Run Code Online (Sandbox Code Playgroud) 假设 Application 中的 getApplicationContext 和 AbstractThreadedSyncAdapter 中的 getContext 返回相同的对象是否安全?我们从活动、服务中获取上下文的其他情况如何......都属于同一个应用程序。谢谢。
我目前正在开发 Android 应用程序,并处理序列化文件。我有一个方法可以将 Java 对象序列化到带有 Context 参数的文件中。在另一个类中工作时,我可以检索有效的 Context 对象,但是当我在扩展 AppCompatActivity 的类上时,我不太确定该怎么做。我尝试使用 getApplicationContext() 但这仍然为我提供了 Context 的空值。
这是我迄今为止所掌握的基础:
public class BookView extends AppCompatActivity {
private static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
Book book = Browse.books.get(getIntent().getIntExtra("index", 0));
Browse.book = book;
setUpScreen(book);
context = getApplicationContext();
}
private void setUpChapters(Book book){
ListView chapters = (ListView) findViewById(R.id.chapters);
ChapterAdapter adapter = new ChapterAdapter(this, R.layout.row, book.getChapters());
chapters.setAdapter(adapter);
if (context != null) {
book.serialize(context);
}
else {
System.out.println("Null bookview context");
}
chapters.setOnItemClickListener(new AdapterView.OnItemClickListener() { …Run Code Online (Sandbox Code Playgroud) 在 Fragment 中读取字符串资源时,哪种方法通常更好/更安全?
这里我getResources().getString()直接阅读:
public class SomeFragment extends Fragment {
public static SomeFragment newInstance() {
Bundle args = new Bundle();
SomeFragment fragment = new SomeFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String someString = getResources().getString(R.string.my_string_id);
}
}
Run Code Online (Sandbox Code Playgroud)
或者最好这样做,先设置一个 Context 字段,然后从中读取资源:
public class SomeFragment extends Fragment {
private Context context;
public static SomeFragment newInstance() {
Bundle args = new Bundle();
SomeFragment fragment = new SomeFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onAttach(Context …Run Code Online (Sandbox Code Playgroud) string android android-context android-fragments android-resources
我正在开发一个应用程序,在该应用程序中我必须将应用程序内容从英语更改为阿拉伯语,反之亦然。语言会根据“活动”和“片段”中的语言选择而更改,但有时在 中不会更改AlertDialog。请建议我做错了什么。
以下是详细信息。
显示对话框的类和方法
public class AlertDialogManager {
public static void showAlertDialog(Context ctx, String message) {
if (ctx == null) {
return;
}
showAlertDialog(ctx, message, Gravity.CENTER);
}
public static void showAlertDialog(Context ctx, String message, int gravity) {
try {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setMessage(message).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated
// method stub
dialog.cancel();
}
});
AlertDialog dialog = builder.show();
TextView messageText = (TextView) dialog
.findViewById(android.R.id.message);
messageText.setGravity(gravity);
} catch …Run Code Online (Sandbox Code Playgroud) 我最终使用了静态函数,但我需要进行导航,它给了我一个错误,没有找到上下文的吸气剂,所以我寻找解决方案并找到了 GET 包,但当我尝试使用它时,它给了我另一个错误:
E/flutter ( 6078): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)]
Unhandled Exception: NoSuchMethodError: The method 'push' was called on null.
Run Code Online (Sandbox Code Playgroud)
我的代码:
E/flutter ( 6078): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)]
Unhandled Exception: NoSuchMethodError: The method 'push' was called on null.
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,它在域、数据和表示层之间有明确的区别。我有从我的服务器获取的数据模型,该数据模型被转换为域模型,并被传递到演示文稿以用于绑定等。我一直在尝试本地化我的项目。我有发布帖子时显示的文字。例如。“一小时前”、“几分钟前” ...
我想本地化它并翻译它,但我无法在没有上下文的情况下从数据类访问字符串资源。我想避免到处传递上下文。通常如何处理这种情况?这似乎是 的一个非常常见的用例getString,但看来我错了。
android localization android-context kotlin android-resources
我们在运行 Android 11 的摩托罗拉设备上遇到了问题。在其他设备上,即使在 Android 11 上,我们也无法重现此问题。我们已经查看了UnsupportedOperationException:尝试从与某个不相关的上下文获取显示,但该线程是目前不活跃,因为我们没有收到任何回复。
该问题似乎与在 TextInput 字段中打开键盘有关。完整的错误语句如下。
Fatal Exception: java.lang.UnsupportedOperationException
Tried to obtain display from a Context not associated with one. Only visual Contexts (such as Activity or one created with Context#createWindowContext) or ones created with Context#createDisplayContext are associated with displays. Other types of Contexts are typically related to background entities and may return an arbitrary display.
Run Code Online (Sandbox Code Playgroud) 尝试在现有应用程序中添加对深色主题的支持时遇到奇怪的行为。
我添加了values-night文件夹,在其中添加了新的colors.xml不同十六进制的暗模式。Theme.MaterialComponents.DayNight.NoActionBar然后我从AppTheme中扩展。
运行应用程序时,它会根据手机中已设置的主题采用正确的颜色。但是当我更改主题并返回应用程序时,颜色不会改变。从日志中我可以看到活动和片段已重新创建,因此这部分按照文档中的描述工作。
如果我添加uiMode到清单中的 configChanges,则不会重新创建活动,而是在onConfigurationChanged(newConfig: Configuration). 但是调用这个函数
resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)
Run Code Online (Sandbox Code Playgroud)
无论我将手机设置为哪个主题,始终返回相同的值。
如果我重新启动应用程序,手机的当前主题将正确应用。因此,感觉应用程序陷入了启动时收到的初始主题(上下文),然后永远不会更新。
我在我的应用程序中设置了 Dagger2 以注入依赖项,其中一个单例是applicationContext. 我从中删除了 @Singleton 标签,但没有帮助。
如果您能帮助解决此问题,我将不胜感激。有人能提示在哪里调试吗?
所以基本上,我有一个方法,需要一个Context以执行几个任务.
public void openDatePicker() { Context c; }
Run Code Online (Sandbox Code Playgroud)
我想知道,如何定义方法调用方的上下文,所以每次调用方法时都不必将其作为参数插入.
android-context ×10
android ×9
android-11 ×1
bindservice ×1
darkmode ×1
flutter ×1
kotlin ×1
localization ×1
motorola ×1
navigation ×1
string ×1