是否可以将参数传递给 Android 处理程序?我有两段代码。
new Thread(){
public void run(){
for(;;){
uiCallback.sendEmptyMessage(0);
Thread.sleep(2000); //sleep for 2 seconds
}
}
}.start();
private Handler uiCallback = new Handler(){
public void handleMessage(Message msg){
//add a new blossom to the blossom ArrayList!!
blossomArrayList.add(new Blossom(context, R.drawable.blossom));
}
};
Run Code Online (Sandbox Code Playgroud)
我当然得到一个错误,因为 Handler 方法看不到我的上下文。这可能是因为这段代码
public BoardView(Context context){
super(context);
Run Code Online (Sandbox Code Playgroud)
Context 在其他地方不可见,我想知道是否可以将它作为参数传递给我的 Handler。
编辑:我发布了两段主要代码来回答一个关于为什么我的 Blossom 对象需要上下文的问题。我自己也不是 100% 确定 >.> 也许你可以看看发生了什么。
public class Blossom{
private Bitmap blossom;
private float blossomX = 0;
private float blossomY = 0;
private Random generator = new Random(); …Run Code Online (Sandbox Code Playgroud) Context.getApplicationContext()在 Android 应用程序中,在应用程序的整个生命周期中保留对返回的应用程序上下文的引用是否安全?
Context ac = context.getApplicationContext();
// keep the ac reference throughout the app lifetime
Run Code Online (Sandbox Code Playgroud) 我知道android中有很多上下文
所以当我创建一个new TextView我必须context像这样传递构造函数
TextView textView = new TextView(this);
Run Code Online (Sandbox Code Playgroud)
我知道它是必要的,但为什么不只是创建和Android为我处理上下文?
我有一个包含RecyclerAdapter的片段.我想在onCreateView方法中初始化适配器,但它会抛出"Type mismatch. Required : Context , Found : FragmentActivity"此语句中的错误
我不知道为什么第一个显示此错误,第二个不包含编译时错误.
显示错误
recyclerView!!.adapter = RestaurantMenuAdapter(activity)
Run Code Online (Sandbox Code Playgroud)
没有显示错误
recyclerView!!.layoutManager = LinearLayoutManager(activity)
Run Code Online (Sandbox Code Playgroud)
Fragment.kt
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_restaurant_menu, container, false)
recyclerView = view.findViewById(R.id.restaurant_container)
recyclerView!!.adapter = RestaurantMenuAdapter(activity)
recyclerView!!.layoutManager = LinearLayoutManager(activity)
Run Code Online (Sandbox Code Playgroud)
RecyclerAdapter.kt
class RestaurantMenuAdapter (val context : Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
return …Run Code Online (Sandbox Code Playgroud) 当我尝试getResources().getColor(R.color.yellow)在正常活动中获取颜色时,我得到以下异常:
07-12 11:58:38.019: E/AndroidRuntime(3233): FATAL EXCEPTION: main
07-12 11:58:38.019: E/AndroidRuntime(3233): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.moveinblue.planner/com.moveinblue.planner.controller.plan.PlanDayScreen}: java.lang.NullPointerException
07-12 11:58:38.019: E/AndroidRuntime(3233): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1993)
07-12 11:58:38.019: E/AndroidRuntime(3233): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-12 11:58:38.019: E/AndroidRuntime(3233): at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-12 11:58:38.019: E/AndroidRuntime(3233): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-12 11:58:38.019: E/AndroidRuntime(3233): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 11:58:38.019: E/AndroidRuntime(3233): at android.os.Looper.loop(Looper.java:137)
07-12 11:58:38.019: E/AndroidRuntime(3233): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-12 11:58:38.019: E/AndroidRuntime(3233): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 11:58:38.019: E/AndroidRuntime(3233): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 11:58:38.019: E/AndroidRuntime(3233): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-12 11:58:38.019: E/AndroidRuntime(3233): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-12 11:58:38.019: E/AndroidRuntime(3233): at dalvik.system.NativeStart.main(Native …Run Code Online (Sandbox Code Playgroud) android nullpointerexception android-context android-resources
在我的应用程序中,我有一个listview,每个项目都有一个按钮.如果用户单击该按钮,我想执行一些http连接.所以我在Adapter类中使用AsyncTask.现在问题是进度对话框没有显示.
private class MyClass extends AsyncTask<Void, Long, Boolean> {
private Context context;
private ServerCall call = new ServerCall();
private ProgressDialog progressDialog1;
public MyClass(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
progressDialog1 = ProgressDialog.show(context, "",
"Please wait...", true);
progressDialog1.setIndeterminate(true);
} catch (final Throwable th) {
}
}
@Override
protected void onProgressUpdate(Long... values) {
super.onProgressUpdate(values);
}
@Override
protected Boolean doInBackground(Void... params) {
//some tasks
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if (mode.equalsIgnoreCase("History")) …Run Code Online (Sandbox Code Playgroud) 这是我想要调用startActionMode的适配器类.我在setActionMode方法中调用它但得到了以下错误:
无法从Context转换为ActivityFragment.
对于ActivityFragment类型,方法startActionMode(ActivityFragment.ActionModeCallback)未定义.
public class ListAdapter extends ArrayAdapter<ListGettersSetters>
{
ArrayList<ListGettersSetters> arrayListGettersSetters;
LayoutInflater layoutInflater;
Context context;
int Resource, i = 0, j = 0, checkedItemsCount = 0;
Animation animation1;
Animation animation2;
CheckBox flipCheckBox;
viewHolder holder;
ActionMode actionMode;
boolean isActionModeShowing;
static class viewHolder
{
public CheckBox imageView;
public TextView textViewName;
public TextView textViewData;
public TextView textViewDetails;
}
public ListAdapter(Context context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters)
{
super(context, resource, arrayListGettersSetters);
this.arrayListGettersSetters = arrayListGettersSetters;
Resource = resource;
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
animation1 …Run Code Online (Sandbox Code Playgroud) android android-arrayadapter android-context android-listfragment
我从谷歌的示例中获得了如何SharedPreferences通过创建SharedPreferencesHelper类来测试代码的灵感:
您可以看到该类使用类中硬编码的实际字符串作为键的sharedPreferences- 这是类的提取:
public class SharedPreferencesHelper {
// Keys for saving values in SharedPreferences.
static final String KEY_NAME = "key_name";
static final String KEY_DOB = "key_dob_millis";
static final String KEY_EMAIL = "key_email";
public boolean savePersonalInfo(SharedPreferenceEntry sharedPreferenceEntry){
// Start a SharedPreferences transaction.
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(KEY_NAME, sharedPreferenceEntry.getName());
editor.putLong(KEY_DOB, sharedPreferenceEntry.getDateOfBirth().getTimeInMillis());
editor.putString(KEY_EMAIL, sharedPreferenceEntry.getEmail());
// Commit changes to SharedPreferences.
return editor.commit();
}
Run Code Online (Sandbox Code Playgroud)
在SharedPreferencesHelperTest这里使用他们的类测试时,他们sharedPreferences使用上面类中定义的相同变量访问模拟:
该类的摘录显示如下:
when(mMockSharedPreferences.getString(eq(SharedPreferencesHelper.KEY_NAME), anyString()))
.thenReturn(mSharedPreferenceEntry.getName());
when(mMockSharedPreferences.getString(eq(SharedPreferencesHelper.KEY_EMAIL), anyString()))
.thenReturn(mSharedPreferenceEntry.getEmail()); …Run Code Online (Sandbox Code Playgroud) public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
Run Code Online (Sandbox Code Playgroud)
如果我们打电话说
fragment.getContext()并且container.getContext将我们得到相同的结果,则主机活动?
也view.getContext()总是返回Activity context或者它可以返回其他类型的context?
android android-context android-fragments android-view android-activity
我试图连接Int一个String这样的输出是String但不确定如何去做.
到目前为止,这是我的代码:
val myMessage = context.getString(R.string.mymessage)
Run Code Online (Sandbox Code Playgroud)
哪里myMessage是String.
现在我想追加一个Int it.myinfo.codeid.
android ×10
android-context ×10
kotlin ×2
android-view ×1
handler ×1
lifecycle ×1
mockito ×1
unit-testing ×1
view ×1