listviewの1行をタップして、次の画面に遷移したいのですが、
取得したメソッドがnullと言われ、クラッシュし、画面遷移できません。
どなたか原因をご教授いただけると嬉しいです。
以下、エラー文です。
2020-10-20 00:45:10.522 24959-24959/com.websarva.wing.android.trylistviewbydatebinding E/AndroidRuntime: FATAL EXCEPTION: main Process: com.websarva.wing.android.trylistviewbydatebinding, PID: 24959 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.websarva.wing.android.trylistviewbydatebinding.MainActivity.onGoEditRecord()' on a null object reference
以下、コードです。
↓activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2 3<layout> 4 <data> 5 <variable 6 name="ViewModel" 7 type="com.websarva.wing.android.trylistviewadaptermvvm.MainViewModel" /> 8 </data> 9 <LinearLayout 10 android:id="@+id/container" 11 xmlns:android="http://schemas.android.com/apk/res/android" 12 xmlns:tools="http://schemas.android.com/tools" 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 tools:context=".MainActivity"> 16 17 18 <ListView 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:id="@+id/listView"/> 22 23 24 </LinearLayout> 25</layout>
↓foodrow.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<layout> 3 <data> 4 <variable 5 name="ViewModel" 6 type="com.websarva.wing.android.trylistviewadaptermvvm.MainViewModel" /> 7 </data> 8 <LinearLayout 9 xmlns:android="http://schemas.android.com/apk/res/android" 10 android:orientation="vertical" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent"> 13 14 <LinearLayout 15 android:orientation="horizontal" 16 android:layout_width="match_parent" 17 android:layout_height="match_parent" 18 android:onClick="@{()->ViewModel.onClicked()}"> 19 20 <TextView 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:text="@{ViewModel.header ,default = v3月14日}" 24 android:id="@+id/name" 25 android:textSize="30dp" 26 android:layout_weight="2" /> 27 28 <TextView 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="@{ViewModel.desc ,default = v3月14日}" 32 android:id="@+id/price" 33 android:textSize="30dp" 34 android:layout_weight="1" /> 35 </LinearLayout> 36 37 </LinearLayout> 38</layout>
↓MainActivity.java
java
1package com.websarva.wing.android.trylistviewadaptermvvm; 2 3import androidx.appcompat.app.AppCompatActivity; 4import androidx.databinding.DataBindingUtil; 5 6import android.content.Intent; 7import android.os.Bundle; 8 9import com.websarva.wing.android.trylistviewadaptermvvm.databinding.ActivityMainBinding; 10import com.websarva.wing.android.trylistviewadaptermvvm.databinding.ActivityMainBindingImpl; 11 12import java.util.ArrayList; 13 14public class MainActivity extends AppCompatActivity implements Navigator { 15 private ActivityMainBinding activityMainBinding; 16 private MainViewModel mainViewModel; 17 private ArrayList<MainViewModel> mainViewModelArrayList; 18 private MyAdapter myAdapter; 19 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 //setContentView(R.layout.activity_main); 25 activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); 26 mainViewModel= new MainViewModel(); 27 mainViewModel.setNavigator(this); 28 mainViewModelArrayList = mainViewModel.getArrayList(); 29 myAdapter = new MyAdapter(this, mainViewModelArrayList); 30 activityMainBinding.listView.setAdapter(myAdapter); 31 //mainFragment.setViewModel(mainViewModel); 32 } 33 34 @Override 35 public void onGoEditRecord() { 36 Intent intent = new Intent(this, Bottom.class); 37 startActivity(intent); 38 } 39} 40 41
↓MainViewModel.java
java
1package com.websarva.wing.android.trylistviewadaptermvvm; 2 3import androidx.lifecycle.MutableLiveData; 4 5import java.util.ArrayList; 6 7public class MainViewModel { 8 9 public String header; 10 public String desc; 11 public Navigator mNavigator; 12 13 public MainViewModel(Model model){ 14 this.header = model.header; 15 this.desc = model.desc; 16 17 } 18 19 public MainViewModel() { 20 } 21 22 23 public void setNavigator(Navigator navigator) { 24 this.mNavigator = navigator; 25 26 } 27 28 public String getHeader() { 29 return header; 30 } 31 32 public void setHeader(String header) { 33 this.header = header; 34 } 35 36 public String getDesc() { 37 return desc; 38 } 39 40 public void setDesc(String desc) { 41 this.desc = desc; 42 } 43 44 public ArrayList<MainViewModel> getArrayList(){ 45 ArrayList<MainViewModel> modelArrayList = new ArrayList<>(); 46 MainViewModel viewModel1 = new MainViewModel(new Model("りんご","100")); 47 MainViewModel viewModel2 = new MainViewModel(new Model("みかん","100")); 48 MainViewModel viewModel3 = new MainViewModel(new Model("いちご","100")); 49 MainViewModel viewModel4 = new MainViewModel(new Model("スイカ","100")); 50 51 modelArrayList.add(viewModel1); 52 modelArrayList.add(viewModel2); 53 modelArrayList.add(viewModel3); 54 modelArrayList.add(viewModel4); 55 56 return modelArrayList; 57 58 } 59 60 public void onClicked(){ 61 System.out.println(mNavigator); 62 mNavigator.onGoEditRecord(); 63 } 64} 65 66
↓MyAdapter.java
java
1package com.websarva.wing.android.trylistviewadaptermvvm; 2 3import android.content.Context; 4import android.view.LayoutInflater; 5import android.view.View; 6import android.view.ViewGroup; 7import android.widget.ArrayAdapter; 8 9import androidx.annotation.NonNull; 10import androidx.annotation.Nullable; 11import androidx.databinding.DataBindingUtil; 12 13import com.websarva.wing.android.trylistviewadaptermvvm.databinding.FoodrowBinding; 14 15import java.util.ArrayList; 16 17public class MyAdapter extends ArrayAdapter<MainViewModel> { 18 private ArrayList<MainViewModel> modelArrayList; 19 private Context context1; 20 21 public MyAdapter(@NonNull Context context, ArrayList<MainViewModel> modelArrayList){ 22 super(context, R.layout.foodrow,modelArrayList); 23 this.context1 = context; 24 this.modelArrayList = modelArrayList; 25 } 26 27 @NonNull 28 @Override 29 public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 30 31 LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); 32 FoodrowBinding foodrowBinding = DataBindingUtil.inflate(layoutInflater,R.layout.foodrow,parent,false); 33 foodrowBinding.setViewModel(modelArrayList.get(position)); 34 35 return foodrowBinding.getRoot(); 36 } 37} 38
↓Navigator.java
JAVA
1package com.websarva.wing.android.trylistviewadaptermvvm; 2 3public interface Navigator { 4 void onGoEditRecord(); 5 6}
↓Model.java
Java
1package com.websarva.wing.android.trylistviewadaptermvvm; 2 3public class Model { 4 public String header; 5 public String desc; 6 7 public Model(String header, String desc) { 8 this.header = header; 9 this.desc = desc; 10 } 11}
回答1件
あなたの回答
tips
プレビュー