- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有麻烦了。
我想从我的 LifeCycleManager(它是一个 LifecycleObserver)调用我的 MainActivity 方法并执行简单的 XML 修改。但是那次崩溃是因为我正在尝试访问 XML 值并且 main_activity.xml 尚未完全创建
首先这是我的 LifeCycleManager :
class LifeCycleManager(context: Context) : LifecycleObserver {
companion object {
var notif = NotificationManager()
var onForeground = false
var main = MainActivity()
// If we are on MainActivity return true , otherwise return false
fun isOnHomeController(context: Context?): Boolean {
if (context == null || context !is MainActivity)
return false
return true
}
var mContext = context
// WHEN THE APP BECOME ACTIVE
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun appComeBackFromBackground() {
onForeground = true
notif.cancelAllLocalNotifications(mContext)
if (isOnHomeController(mContext)) {
// Call the refresh dialog and print it
main.appComeBackFromBackground("refresh", null)
}
}
}
这是我的 MainActivity 方法:
fun appComeBackFromBackground(status: String?, elementsadded: Int?)
{
Log.e("enterForeground", "App come back in Foreground")
if (status == null) return
when (status)
{
"refresh" ->{
val text = findViewById<TextView>(R.id.refreshtext)
text.setText("test")
}
else -> {
return
}
}
}
如您所见,当我的 MainActivity Start 和 Lifecycle 获取事件并直接进入我的 appComeBackFromBackground() 方法时。在其中我调用了我的 MainActivity 方法,我想在其中修改 activity_main.xml 中的元素
论文崩溃:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mControllers.MainActivity}: java.lang.RuntimeException: Failed to call observer method
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
请问我该怎么办?我真的需要这样做(从 LifecycleObserver 到 MainActivity)。谢谢
编辑:
我试图像这样延迟调用 MainActivity 的方法:
Handler().postDelayed({
main.appComeBackFromBackground("refresh", null)
}, 5000)
即使加载了 MainActivity 布局,它也会崩溃
最佳答案
我正在用 Java 写这个,(如果您不想将数据保存在首选项或存储中,这是方法)
应用类:
public class TestApplication extends Application implements ActivityLifecycleCallbacks {
private static String mStringToSet;
@Override
public void onCreate() {
super.onCreate();
//App is not from background, a new instance of your app is created
mStringToSet="new_instance";
registerActivityLifecycleCallbacks(this);
}
public static String getStringToSet() {
return mStringToSet;
}
@Override
public void onActivityDestroyed(Activity activity) {
if(activity instanceof MainActivity){
//This is only called when the activity is destroyed not the application, so if the context is your desired activity then set the string here
mStringToSet="refresh";
}
}
//Implement other life cycle methods**
在您的 Activity 中:
public class MainActivity extends AppCompatActivity {
private final String TAG="ACTIVITY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if("refresh".equals(TestApplication.getStringToSet())){
Toast.makeText(this,"From Background",Toast.LENGTH_SHORT).show();
(TextView)findViewById(R.id.YOUR_TEXT_VIEW).setText("refresh")
}else{
Log.d(TAG,TestApplication.getStringToSet());
Toast.makeText(this,"Not from background",Toast.LENGTH_SHORT).show();
(TextView)findViewById(R.id.YOUR_TEXT_VIEW).setText("new_instance")
}
}
}
//Implement other life cycle methods
}
关于java - 在LifecycleObserver中调用mainActivity的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51290032/
因此,我正在学习 Lynda.com 上关于使用 Google map V2 构建移动应用程序的教程,并且我正在学习一个让您创建 map 应用程序的部分,该应用程序以编程方式查找位置,而不是依赖于se
我有一个 Mainactivity,其中包含一个 Layout,它是 4 个子布局的父级。单击子布局后,我将使用一个新 fragment 替换主布局。但我无法在按下返回按钮后返回 MainActivi
如你所见 this@MainActivity 或 MainActivity@this 在 kotlin 中开始 Activity 时有效。我试图找出答案,但我什么也没找到。谁能知道它的确切区别以及哪个
Solve by using getActivity() 我有这个 MainActivity.java 和 RepeatEntry.java 在我的 MainActivity 中,我有这个代码来实现
我正在尝试将 Google Analytics 集成到我的应用程序中,并遵循来自 here 的教程一切就绪: 我有一个 MainActivity,它扩展了 Activity 并附加了三个 Tab Fr
EditText 对象未创建,为什么? EditText editText=(EditText)findViewById(R.id.edittext); 这适用于扩展 AsyncTask 类的应用程序
所以我正在尝试了解 Android Fragments。如果我将以下代码放入我的 MainActivity 中: public void getMessage(Object obj) { Lo
我试图在我的Android设备上运行我的Android应用程序,但它一直显示我的主活动不存在,尽管我的主活动类在那里。我试图创建一个新的项目,然后复制我以前的代码,它在一段时间内奏效了。但随后它又产生
//主要活动 package com.going.books; import androidx.appcompat.app.AppCompatActivity; import androi
这似乎是一个我不好意思问的基本问题,但我对我的 Fragment 学习曲线感到非常沮丧,以至于我会暴露我的无知。 教科书中的一个例子,它削减了很多角落,使得扩展变得困难,即使它们有效,也没有按钮可以点
我正在将我的启动 Activity 从“MainActivity”更改为“RealMain”我在 list 中声明了这一点,但是 MainActivity 仍然首先出现。有人可以启发我吗?代码来 se
它失败了@ below line - bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE); 下面是轨迹.... Act
我的项目在调试状态下正常工作。当我打开MainActivity.java文件时,这在import androidx.annotation.NonNull;上显示了问题 并在下面显示错误。 FAILUR
我在 MainActivity 类中看到了这样的代码: class MainActivity : AppCompatActivity() { private val languages = a
我的问题是关于如何制作启动屏幕,并在 MainActivity 完成渲染时完成启动屏幕,而不是通过设置超时。 我已经搜索过如何制作闪屏并且我已经做到了,但主要是他们使用超时来设置闪屏何时必须关闭/完成
大家好,我正在尝试为我的 Android 手机开发一个手电筒应用程序,但是当我运行它时出现错误。 GUI 有 2 个按钮:1 个用于开灯,另一个用于关灯 这是代码: package com.examp
如何解决这个问题? MainActivity.this is not an enclosing class. 谢谢 public class uploadToServer extends AsyncT
我的 logcat 中出现一些错误,并且我的应用程序无法在我的智能手机上运行...我更改了 list 和 MainActivity 中的一些代码(名为 HomaActivity ) 这是我的 list
我想知道使用单例 MainAcitivity 是否是一个糟糕的设计选择,如下所示: public class MainActivity extends AppCompatActivity ... {
我正在创建一个像 WhatsApp 一样的应用程序,但我遇到了问题。该应用程序应该进入 LoginActivity,但它直接进入 MainActivity,而无需我登录。 我尝试更改 Android
我是一名优秀的程序员,十分优秀!