質問編集履歴

4

言語名を追記しました

2024/01/11 09:23

投稿

reo4949
reo4949

スコア2

test CHANGED
File without changes
test CHANGED
@@ -31,7 +31,7 @@
31
31
  ```
32
32
 
33
33
  ### 該当のソースコード
34
- ```MenuListFragment
34
+ ```MenuListFragment.java
35
35
  package com.websarva.wings.android.fragmentsample;
36
36
 
37
37
  import android.app.Activity;
@@ -117,7 +117,7 @@
117
117
  }
118
118
  ```
119
119
 
120
- ```MenuThanksFragment
120
+ ```MenuThanksFragment.java
121
121
  package com.websarva.wings.android.fragmentsample;
122
122
 
123
123
 

3

記入できなかったMenuListFragment,fragment_menu_li追加しました

2024/01/11 08:52

投稿

reo4949
reo4949

スコア2

test CHANGED
File without changes
test CHANGED
@@ -31,7 +31,90 @@
31
31
  ```
32
32
 
33
33
  ### 該当のソースコード
34
- ```
34
+ ```MenuListFragment
35
+ package com.websarva.wings.android.fragmentsample;
36
+
37
+ import android.app.Activity;
38
+ import android.os.Bundle;
39
+
40
+ import androidx.annotation.NonNull;
41
+ import androidx.annotation.Nullable;
42
+ import androidx.fragment.app.Fragment;
43
+ import androidx.fragment.app.FragmentManager;
44
+ import androidx.fragment.app.FragmentTransaction;
45
+
46
+ import android.view.View;
47
+ import android.widget.AdapterView;
48
+ import android.widget.ListView;
49
+ import android.widget.SimpleAdapter;
50
+
51
+ import java.util.ArrayList;
52
+ import java.util.HashMap;
53
+ import java.util.List;
54
+ import java.util.Map;
55
+
56
+
57
+ public class MenuListFragment extends Fragment {
58
+ public MenuListFragment() {
59
+ super(R.layout.fragment_menu_list);
60
+ }
61
+
62
+ @Override
63
+ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
64
+ super.onViewCreated(view, savedInstanceState);
65
+
66
+ ListView lvMenu = view.findViewById(R.id.lvMenu);
67
+ List<Map<String, String>> menuList = new ArrayList<>();
68
+ Map<String, String> menu = new HashMap<>();
69
+ menu.put("name", "唐揚げ定食");
70
+ menu.put("price", "800円");
71
+ menuList.add(menu);
72
+
73
+ menu = new HashMap<>();
74
+ menu.put("name", "ハンバーグ定食");
75
+ menu.put("price", "850円");
76
+ menuList.add(menu);
77
+
78
+ menu = new HashMap<>();
79
+ menu.put("name", "刺身定食");
80
+ menu.put("price", "1200円");
81
+ menuList.add(menu);
82
+
83
+ menu = new HashMap<>();
84
+ menu.put("name", "お好み焼き");
85
+ menu.put("price", "700円");
86
+ menuList.add(menu);
87
+
88
+ Activity parentActivity = getActivity();
89
+ String[] from = {"name", "price"};
90
+ int[] to = {android.R.id.text1, android.R.id.text2};
91
+ SimpleAdapter adapter = new SimpleAdapter(parentActivity, menuList,
92
+ android.R.layout.simple_list_item_2, from, to);
93
+ lvMenu.setAdapter(adapter);
94
+
95
+ lvMenu.setOnItemClickListener(new ListItemClickListener());
96
+ }
97
+
98
+ private class ListItemClickListener implements AdapterView.OnItemClickListener {
99
+ @Override
100
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id){
101
+ Map<String,String> item = (Map<String, String>) parent.getItemAtPosition(position);
102
+ String menuName = item.get("name");
103
+ String menuPrice = item.get("price");
104
+
105
+ Bundle bundle = new Bundle();
106
+ bundle.putString("menuName",menuName);
107
+ bundle.putString("menuPrice",menuPrice);
108
+
109
+ FragmentManager manager = getParentFragmentManager();
110
+ FragmentTransaction transaction = manager.beginTransaction();
111
+ transaction.setReorderingAllowed(true);
112
+ transaction.addToBackStack("Only List");
113
+ transaction.replace(R.id.fragmentMainContainer, MenuThanksFragment.class, bundle);
114
+ transaction.commit();
115
+ }
116
+ }
117
+ }
35
118
  ```
36
119
 
37
120
  ```MenuThanksFragment
@@ -153,7 +236,19 @@
153
236
  </androidx.constraintlayout.widget.ConstraintLayout>
154
237
  ```
155
238
 
156
- ```
239
+ ```fragment_menu_list.xml
240
+ <?xml version="1.0" encoding="utf-8"?>
241
+ <ListView
242
+ xmlns:android="http://schemas.android.com/apk/res/android"
243
+ xmlns:tools="http://schemas.android.com/tools"
244
+ android:id="@+id/lvMenu"
245
+ android:layout_width="match_parent"
246
+ android:layout_height="match_parent"
247
+ tools:context=".MenuListFragment">
248
+
249
+ <!-- TODO: Update blank fragment layout -->
250
+
251
+ </ListView>
157
252
  ```
158
253
 
159
254
  ### 試したこと・調べたこと

2

タグを変更しました

2024/01/11 08:46

投稿

reo4949
reo4949

スコア2

test CHANGED
File without changes
test CHANGED
File without changes

1

エラーメッセージの修正をしました

2024/01/11 08:44

投稿

reo4949
reo4949

スコア2

test CHANGED
File without changes
test CHANGED
@@ -8,32 +8,30 @@
8
8
 
9
9
 
10
10
  ### エラーメッセージ
11
- ```error
12
- Process: com.websarva.wings.android.fragmentsample, PID: 16880
13
- java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
14
- at com.websarva.wings.android.fragmentsample.MenuThanksFragment.onViewCreated(MenuThanksFragment.java:32)
15
- at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
16
- at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
17
- at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
18
- at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
19
- at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
20
- at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
21
- at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
22
- at android.os.Handler.handleCallback(Handler.java:942)
23
- at android.os.Handler.dispatchMessage(Handler.java:99)
24
- at android.os.Looper.loopOnce(Looper.java:201)
25
- at android.os.Looper.loop(Looper.java:288)
26
- at android.app.ActivityThread.main(ActivityThread.java:7872)
27
- at java.lang.reflect.Method.invoke(Native Method)
28
- at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
29
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
11
+ ```logcat
12
+ Process: com.websarva.wings.android.fragmentsample, PID: 16880
13
+ java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
14
+ at com.websarva.wings.android.fragmentsample.MenuThanksFragment.onViewCreated(MenuThanksFragment.java:32)
15
+ at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
16
+ at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
17
+ at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
18
+ at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
19
+ at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
20
+ at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
21
+ at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
22
+ at android.os.Handler.handleCallback(Handler.java:942)
23
+ at android.os.Handler.dispatchMessage(Handler.java:99)
24
+ at android.os.Looper.loopOnce(Looper.java:201)
25
+ at android.os.Looper.loop(Looper.java:288)
26
+ at android.app.ActivityThread.main(ActivityThread.java:7872)
27
+ at java.lang.reflect.Method.invoke(Native Method)
28
+ at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
29
+ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
30
30
 
31
31
  ```
32
32
 
33
33
  ### 該当のソースコード
34
-
35
34
  ```
36
- 特になし
37
35
  ```
38
36
 
39
37
  ```MenuThanksFragment
@@ -156,7 +154,6 @@
156
154
  ```
157
155
 
158
156
  ```
159
- 特になし
160
157
  ```
161
158
 
162
159
  ### 試したこと・調べたこと