前提・実現したいこと
Androidアプリを開発しています。
ViewModelとLiveDataを実装してデータ共有したいです。
発生している問題・エラーメッセージ
LiveDataのObserverの設定が上手くいきません。
具体的にはMutableLiveDataのobseveメソッドの第一引数にLifecycleOwnerオブジェクトを入れるため
getViewLifecycleOwner()にて取得を試みているのですがNullが返ってくるのっだと思います。(CenterFragment.javaのコメント行の箇所)
なぜNullが返ってくるのでしょうか?
ライフサイクル的にNullが返る状況ではないと思いますが、、、
エラーメッセージ 2020-05-05 17:33:21.125 10181-10181/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication_ver2, PID: 10181 java.lang.IllegalStateException: Can't access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView() at androidx.fragment.app.Fragment.getViewLifecycleOwner(Fragment.java:328) at com.example.myapplication_ver2.CenterFragment.onCreate(CenterFragment.java:25) at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684) at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:270) at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1173) at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1255) at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1138) at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:136) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1987) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1945) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1847) at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2621) at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2569) at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247) at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541) at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391) at android.app.Activity.performStart(Activity.java:7157) at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2937) at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180) at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
該当のソースコード
CenterFragment.java
java
1package com.example.myapplication_ver2; 2 3import android.graphics.Color; 4import android.os.Bundle; 5import android.util.Log; 6import android.view.LayoutInflater; 7import android.view.View; 8import android.view.ViewGroup; 9import android.widget.Button; 10import android.widget.LinearLayout; 11import androidx.annotation.NonNull; 12import androidx.annotation.Nullable; 13import androidx.fragment.app.Fragment; 14import androidx.lifecycle.LifecycleOwner; 15import androidx.lifecycle.MutableLiveData; 16import androidx.lifecycle.Observer; 17import androidx.lifecycle.ViewModelProviders; 18 19public class CenterFragment extends Fragment { 20 21 @Nullable 22 @Override 23 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 24 25 LinearLayout linearLayout = new LinearLayout(getContext()); 26 linearLayout.setBackgroundColor(Color.BLUE); 27 container.addView(linearLayout); 28 29 Button button = new Button(getContext()); 30 linearLayout.addView(button); 31 return super.onCreateView(inflater, container, savedInstanceState); 32 } 33 34 @Override 35 public void onActivityCreated(@Nullable Bundle savedInstanceState) { 36 37 super.onActivityCreated(savedInstanceState); 38 ViewModelA viewModelA = ViewModelProviders.of(this).get(ViewModelA.class); 39 viewModelA.getList().observe(getViewLifecycleOwner(), new Observer<Integer>() { //恐らくgetViewLifecycleOwner()でnullが返ってくる事によるエラー 40 @Override 41 public void onChanged(Integer s) { 42 } 43 }); 44 45 } 46} 47
MainActivity.java
java
1package com.example.myapplication_ver2; 2 3import androidx.appcompat.app.AppCompatActivity; 4import androidx.constraintlayout.widget.ConstraintLayout; 5import androidx.fragment.app.Fragment; 6import androidx.fragment.app.FragmentManager; 7import androidx.fragment.app.FragmentTransaction; 8 9import android.os.Bundle; 10import android.widget.LinearLayout; 11 12public class MainActivity extends AppCompatActivity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 19 FragmentManager fragmentManager = getSupportFragmentManager(); 20 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 21 fragmentTransaction.add(R.id.layout,new CenterFragment()); 22 fragmentTransaction.commit(); 23 } 24} 25
ViewModelA.java
java
1package com.example.myapplication_ver2; 2 3import androidx.lifecycle.LiveData; 4import androidx.lifecycle.MutableLiveData; 5import androidx.lifecycle.ViewModel; 6 7public class ViewModelA extends ViewModel { 8 private MutableLiveData<Integer> liveData; 9 10 public MutableLiveData<Integer> getList(){ 11 if(liveData==null){ 12 liveData = new MutableLiveData(); 13 } 14 return liveData; 15 } 16 17} 18
回答2件
あなたの回答
tips
プレビュー