gpt4 book ai didi

java - 在LifecycleObserver中调用mainActivity的方法

转载 作者:行者123 更新时间:2023-11-29 23:46:33 25 4
gpt4 key购买 nike

我有麻烦了。

我想从我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com