我正在我的.IRecyclerview中使用我的 RecyclerView 类。我需要在我编写的一种方法中显示。但我收到一个错误FragmentAdapterAlertDialogAdapterClass
android.app.Activity 无法应用于 android.content.Context。
这是我的代码AlertClass
public class AlertClass {
public void noInternetAlert(Activity activity)
{
final AlertDialog alertDialog=new AlertDialog.Builder(activity).create();
AlertDialog.Builder builder=new AlertDialog.Builder(activity);
builder.setTitle("No Internet Connection");
builder.setMessage("You need to have Mobile data or Wifi to access this.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
}
});
builder.show();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的方法的代码AdapterClass
private void free() {
AlertClass alert=new AlertClass();
alert.noInternetAlert(context);
}
Run Code Online (Sandbox Code Playgroud)
这是适配器类的代码
public class BusyAdapter extends RecyclerView.Adapter<BusyAdapter.ViewHolder>
{ …Run Code Online (Sandbox Code Playgroud) android android-context android-fragments android-dialog android-recyclerview
我有一段文本,我想在单击时调用函数 X。我使用 clickable 修饰符并在 onClick 方法中调用函数 X 来执行此操作。然而,函数 X 使用 Context。
为了解决这个问题,我使用下面概述的方法:
@Composable
fun ClickableText() {
val context = LocalContext.current
Text("Click me!", Modifier.clickable { functionX(context) })
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,这种方法有什么问题吗?它似乎有效,但存储对 LocalContext.current 的本地引用似乎有点棘手。如果 Context 在 UI 组合和 onClick 方法调用之间的时间发生变化,我认为这可能会导致一些问题。有没有更好的方法在不可组合的回调函数中获取 Context?
我看到这个问题有10个答案,我已经阅读了所有这些但是无法让这个工作.
我正在将我的活动转移到这里TabGroup,我在一个标签组中有3个活动,并从这里得到帮助: TabActivity中的多个Android活动
打开自定义对话框时出现此错误:
07-12 10:07:56.985:ERROR/AndroidRuntime(9532):android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.app.LocalActivityManager $ LocalActivityRecord@462d96f0无效; 你的活动在运行吗?
我知道这个错误说上下文很糟糕.
在ColorPickerView不起作用的(下面)上,我正在调试并且可以看到构造函数正在执行它的工作,但是一旦构造函数完成它就崩溃了.
问题是我有来自其他两个活动的3个类似错误,一个CheckBox和两个Spinner.
我可以通过适当地改变上下文来轻松修复它.
这个更复杂,View堆栈看起来像这样:
TabGroup
.......TabHost (public class FileEditMainActivity extends Activity)
...............ImageView (public class FileEditImageView extends ImageView )
........................Dialog (public class ColorPickDialog extends Dialog )
..............................View (private class ColorPickerView extends View )
Run Code Online (Sandbox Code Playgroud)
无论我如何改变上下文,系统都会告诉我上下文不存在.
该ColorPickerView总是崩溃..
如果我不使用,也必须说这是有效的TabGroup.当我把FileEditMainActivitTabGroup中的内容出错时.
任何人都有想做什么?
以下是我的代码:
在MainActivity.class中
private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) {
if(CheckedButtonId==R.id.radiobutton_on){
Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show();
alert_on = true;
display_alert();
}
else{
alert_on = false;
}
}
};
public void display_alert(){
int delay = 10000;
Timer timer =new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
startService(myIntent);
}
}, delay,10000);
}
Run Code Online (Sandbox Code Playgroud)
MyService.class
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if(MainActivity.alert_on)
Toast.makeText(getApplicationContext(), "Alert is On", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Alert …Run Code Online (Sandbox Code Playgroud) 我在OnBootReceiver中创建了一个警报,如下所示:
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent i = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), savedIntervalAutomaticMilisInt, pendingIntent);
}
}
Run Code Online (Sandbox Code Playgroud)
但要取消它,我在Activity中使用此代码:
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
scManager.clearErrors();
Run Code Online (Sandbox Code Playgroud)
所以要设置它我使用BroadcastReceiver的Context参数中的上下文,但要取消它我使用thisActivity中的上下文.我的问题:即使上下文略有不同,警报仍然会被取消吗?
嗨,我有一个名为BaseActivity的活动,它扩展了Activity.从这里我必须去菜单按钮按下扩展的SettingsActivityPreferenceActivity.要启动a AsyncTask,这是一个独立的类,我需要一个实例BaseActivity.我怎样才能获得一个BaseActivity实例SettingsActivity?
有什么办法,例如:
intent.putExtra("activity_instance",BaseActivity.this);
Run Code Online (Sandbox Code Playgroud) 我正在尝试将我连接Widget到应用程序的数据库,但我一无所知。
工作原理:
import android.app.Activity;
public class Widgets1 extends Activity{
mMioDbHelper = new SqlHelper(getApplicationContext());
Run Code Online (Sandbox Code Playgroud)
这不是吗?
import android.appwidget.AppWidgetProvider;
public class Widgets1 extends AppWidgetProvider{
mMioDbHelper = new SqlHelper(getApplicationContext());
Run Code Online (Sandbox Code Playgroud)
如何将小部件连接到数据库?我的意思是如何从小部件类进行查询?
谢谢。
我在Android中是新的,我想知道是否有之间的差异getActivity(),并this.getActivity()在片段clases.例如,我们在siple类中有一个方法(不扩展Activity或Fragment),如:
public static void method(Context context){
... some code
}
Run Code Online (Sandbox Code Playgroud)
如果我们想要使用它,只需在我们的片段类中调用它:
MyMethodClass.method(getActivity());
Run Code Online (Sandbox Code Playgroud)
要么
MyMethodClass.method(this.getActivity());
Run Code Online (Sandbox Code Playgroud)
我知道两者都有效,但我需要专业的意见.
谢谢.
我用这个:
actionBar.setBackgroundColor(ContextCompat.getColor(this, R.color.action_bar));
Run Code Online (Sandbox Code Playgroud)
要使用color.xml中的颜色。
它很好用,但是在某些代码中它说:
Wrong 1st argument type. Found: 'org.telegram.ui.ActionBar.BaseFragment', required: 'android.content.Context'
Run Code Online (Sandbox Code Playgroud)
但是我导入android.content.Context:
import android.content.Context;
Run Code Online (Sandbox Code Playgroud)
我尝试使用而不是'this':
actionBar.setBackgroundColor(ContextCompat.getColor(context, R.color.action_bar));
Run Code Online (Sandbox Code Playgroud)
但是比Android Studio说的要多:
Cannot resolve symbol 'context'
Run Code Online (Sandbox Code Playgroud) 我在kotlin中创建了这样的颜色数组
private var colorArray = arrayOf(
ContextCompat.getColor(this, R.color.text_yellow),
ContextCompat.getColor(this, R.color.text_green),
ContextCompat.getColor(this, R.color.text_red)
)
Run Code Online (Sandbox Code Playgroud)
当我想通过索引从colorArray获取颜色时
var color = colorArray[0]
Run Code Online (Sandbox Code Playgroud)
我的索引0崩溃了,
尝试在空对象引用上调用虚拟方法'android.content.res.Resources android.content.Context.getResources()'
我不知道我错在哪里如果我写ContextCompat.getColor(this,R.color.text_yellow)没问题,但是崩溃通过数组索引给了我错误
android ×10
android-context ×10
java ×2
alarmmanager ×1
arrays ×1
kotlin ×1
service ×1
timer ×1
timertask ×1
widget ×1