在Android实用程序类中,我想在类中获取系统首选项值,但我没有那里的上下文,因为调用它的类也没有上下文.我发现对于Resources,可以使用静态Resources.getSystem()函数.在没有上下文的情况下获取系统偏好是否有类似的方法
我的班级不是活动也不是服务.这是一个实用工具类.如果需要可以提供更多信息.
我正在编写一个库类来封装我的第一个Android应用程序中的一些逻辑.我要封装的功能之一是查询地址簿的函数.因此,它需要一个ContentResolver.我试图弄清楚如何保持库函数的黑盒子...也就是说,避免让每个Activity传递都在自己的上下文中得到一个ContentResolver.
问题是我不能为我的生活弄清楚如何ContentResolver从我的库函数中获取一个.我找不到包含的导入getContentResolver.谷歌说用来getContext得到一个Context可以打电话getContentResolver,但我找不到包含getContext任何一个的导入.下一篇文章说用于getSystemService获取一个对象来调用getContext.但是 - 我找不到包含任何内容的导入getSystemService!
所以我很困惑,我怎么能在封装的库函数中获得ContentResolver,或者我几乎在每个调用Activity传递引用它自己的上下文?
我的代码基本上是这样的:
public final class MyLibrary {
private MyLibrary() { }
// take MyGroupItem as a class representing a projection
// containing information from the address book groups
public static ArrayList<MyGroupItem> getGroups() {
// do work here that would access the contacts
// thus requiring the ContentResolver
}
}
Run Code Online (Sandbox Code Playgroud)
getGroups是我希望避免传递的方法, …
所以,我完成了一个项目,现在我面临的主要问题是应用程序中的内存泄漏("泄漏"意味着您保留对某个活动的引用,从而阻止GC收集它)
我发现的一些发生内存泄漏的案例是:
这是因为对活动上下文的长期引用.
我在这里找到的一个很好的例子,
private static Drawable sBackground;
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
TextView label = new TextView(this);
label.setText("Leaks are bad");
if (sBackground == null) {
sBackground = getDrawable(R.drawable.large_bitmap);
}
label.setBackgroundDrawable(sBackground);
setContentView(label);
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是private static Drawable sBackground;静态Drawable是以Activity作为上下文创建的,所以在这种情况下,有一个引用Activity的Drawable的静态引用,这就是泄漏的原因.只要该引用存在,Activity就会保留在内存中,泄露其所有视图.
引起注意的第二种情况是屏幕方向改变时.当屏幕方向改变时,系统默认会破坏当前活动并创建新活动,同时保留其状态.这样,Android将从资源重新加载应用程序的UI.现在假设您编写了一个带有大位图的应用程序,您不希望在每次轮换时加载该位图.
这将导致大量内存泄漏,因为可能需要加载大的位图.
第三种情况,我发现是对活动上下文的引用.它还会导致内存泄漏.
我想知道是否有任何简单的方法可以避免这种内存泄漏的发生.或者是否有工具可以检查并从应用程序中删除这些内存泄漏.
我是新手,如果这是一个非常愚蠢的问题,我很抱歉.我只想澄清一些事情.我的书说我可以使用该getApplicationContext()方法检索进程的应用程序上下文.我真的不知道在哪里键入这个或如何处理它.我可以进入层次结构,但我如何处理那里的所有脚本.另外我还会在main.xml中编写Activity Callbacks?练习要我在项目中添加日志标记,但我不知道该怎么做.确切的文字说:
"在onCreate()回调方法中,使用Log.i()方法添加信息日志消息."
另一个练习说:
"除了onCreate()之外,还实现了一些Activity回调方法,例如onStart().向每个回调方法添加一条日志消息,然后正常运行应用程序".
由于这些似乎是基本问题,有人可以帮助我.
我正在使用Android SDK和Eclipse.我已经创建了Hello World应用程序,但我不知道如何处理Context或Retrieving资源.请帮忙!
根据这个答案或android的文档,有几种方法可以在应用程序中获取Context并将其传递给其他类/方法/ anyuneed.
假设我在Foo活动中并且需要将上下文传递给Bar的构造函数.
Bar bar = new Bar(Foo.this);
Bar bar2 = new Bar(this); //same as first i guess
Bar bar3 = new Bar(getApplicationContext());
Bar bar4 = new Bar(getBaseContext());
Bar bar5 = new Bar(MyApp.getContext); // get context statically
Run Code Online (Sandbox Code Playgroud)
考虑到内存泄漏,速度,一般性能,所有这些可能性之间的更好方法是什么?
我创建了一个自定义视图,并且需要Activity引用来执行一些与Handler相关的操作.我知道getContext()是一种获取Context的方法,但有没有办法获得相同的Activity引用?
我有一个Activity类,我将一些信息传递给一个帮助类(非活动)类.在helper类中我想使用getSharedPreferences().但我无法使用它,因为它需要活动上下文.
这是我的代码:
class myActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Info = new Authenticate().execute(ContentString).get();
ItemsStore.SetItems(Info);
}
}
class ItemsStore
{
public void SetItems(Information info)
{
SharedPreferences localSettings = mContext.getSharedPreferences("FileName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = localSettings.edit();
editor.putString("Url", info.Url);
editor.putString("Email", info.Email);
}
}
Run Code Online (Sandbox Code Playgroud)
不知道如何实现这一目标?
所以我想在我的项目中获得一个字符串资源但是当我打电话时context.getResources().getString(...),我得到一个NullPointerException.在调试模式下,我发现上下文不是null但是看着它的成员,我发现它mResources是null.为什么没有为活动上下文加载资源?
编辑
显然,这是触发异常的原因
public class MyActivity extends Activity {
SomeClass someClass = new SomeClass(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
public class SomeClass {
private final Context mContext;
public SomeClass(Context context) {
mContext = context;
mContext.getResources().getString(R.string.app_name);
}
}
Run Code Online (Sandbox Code Playgroud)
我不得不按照CommonsWare的建议将初始化someClass移到后面.谢谢.super.onCreate()
Android Studio将此行标记为错误:
public class ParallaxView extends ImageView
Run Code Online (Sandbox Code Playgroud)
这里的错误:
This custom view should extend android.support.v7.widget.AppCompatImageView instead less... (Ctrl+F1)
In order to support features such as tinting, the appcompat library will automatically load special appcompat replacements for the builtin widgets.
However, this does not work for your own custom views. Instead of extending the android.widget classes directly, you should instead extend one of the delegate classes in android.support.v7.widget.AppCompat.
Run Code Online (Sandbox Code Playgroud)
它建议我扩展,AppCompatImageView但我的JUnit测试不会通过,因为AppCompatImageView需要一个模拟Context资源,Imageview不需要这个.
这里是解决其他问题的问题:
NullPointerException使用模拟Context创建AppCompatImageView
我可以忽略此错误并使用ImageView吗?还有其他方法吗?
android android-custom-view android-context android-view android-support-library
我正在研究谷歌的架构组件,以便将ViewModel和LiveData实现到我的应用程序,官方文档说:
注意:由于ViewModel比特定活动和片段实例更长,因此它永远不应引用View或任何可能包含对活动上下文的引用的类.如果ViewModel需要Application上下文(例如,查找系统服务),它可以扩展AndroidViewModel类并具有在构造函数中接收Application的构造函数(因为Application类扩展了Context)
在那之后,我最终得到了这样的代码:
public class ViewModelTest extends AndroidViewModel {
public ViewModelTest(Application application) {
super(application);
}
public void test(){
Prefs.getCurrentCode(getApplication());
}
Run Code Online (Sandbox Code Playgroud)
我应该在活动中正常实例吗?
val viewModel2 = ViewModelProviders.of(this).get(ViewModelTest::class.java)
viewModel2.test()
Run Code Online (Sandbox Code Playgroud)
不是很糟糕吗?要在需要访问SharedPreferences或需要上下文的任何内容时使用此应用程序变量?如果是,我应该避免在ViewModel上使用它并仅在视图上使用它吗?特别是如果我想用一个需要上下文的值更新UI组件.我有点不知道如何解决这个问题,我愿意接受任何建议.
提前致谢
android viewmodel android-context android-application-class android-livedata
android ×10
android-context ×10
android-view ×1
eclipse ×1
java ×1
memory-leaks ×1
resources ×1
view ×1
viewmodel ×1