我有麻烦了。
我想从LifeCycleManager(这是一个LifecycleObserver)中调用MainActivity的方法,并执行简单的XML修改。但是那次崩溃是因为我正在尝试访问XML值,并且main_activity.xml尚未完全创建
首先,这是我的LifeCycleManager:
public function inicio()
{
$pageInfo =
[
'page_title' => 'Inicio',
'menu_active' => 'Inicio',
'submenu_active' => '',
];
$globals = Globals::all();
$web_name = $globals->where('name', 'Nombre del restaurante')->first();
$web_description = $globals->where('name', 'Descripcion en google del restaurante')->first();
$restaurant_bio = $globals->where('name', 'Biografia del restaurante')->first();
$booking_phone = $globals->where('name', 'Télefono de reserva')->first();
$client_mail = $globals->where('name', 'Email de contacto')->first();
$full_address = $globals->where('name', 'Dirección del restaurante')->first();
$city_zip_address = $globals->where('name', 'Código postal y ciudad')->first();
$config = compact('web_name', 'web_description');
$sliders = Slider::all();
return view('inicio.index', compact('pageInfo', 'config', 'sliders', 'web_name'));
}
这是我的MainActivity方法:
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启动时,我的生命周期获取事件并直接进入appComeBackFromBackground()方法。 在其中调用我的MainActivity方法,该方法是要从activity_main.xml
修改元素的地方论文崩溃:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.mControllers.MainActivity}:java.lang.RuntimeException:无法调用观察者方法
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.Window $ Callback android.view.Window.getCallback()”
我该怎么办?我真的需要这样做(LifecycleObserver到MainActivity)。谢谢
编辑:
我试图像这样延迟地调用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布局,它也会崩溃
答案 0 :(得分:1)
我是用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**
您的活动中:
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
}