質問編集履歴

3

}の追加

2017/06/09 18:18

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -128,6 +128,8 @@
128
128
 
129
129
  arrayadapter();//spinnerのlistの更新のためのmethod
130
130
 
131
-
131
+ }
132
+
133
+ }
132
134
 
133
135
  ```

2

説明の追加

2017/06/09 18:18

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -3,3 +3,131 @@
3
3
 
4
4
 
5
5
  また、AlertDialog専用のクラスを作るということを、以前に、人に教わったのですが、現在、私は、新しく作ったclassに、様々なAlertと、そのAlertのボタンが押された時の処理をまとめて、書いているのですが、分けたほうがいいでしょうか?
6
+
7
+
8
+
9
+ このような、下のような、クラスを作って、LocationActivityから呼び出しているのですが、このようにエラーがでます。
10
+
11
+ ```java
12
+
13
+ ava.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
14
+
15
+ at android.app.Activity.findViewById(Activity.java:2323)
16
+
17
+ at com.example.android.sample.new3.Alart.newSet(Alart.java:30)
18
+
19
+ at com.example.android.sample.new3.LocationActivity.onOptionsItemSelected(LocationActivity.java:250)
20
+
21
+ ```
22
+
23
+
24
+
25
+ ```java
26
+
27
+
28
+
29
+ public class Alart extends Activity {
30
+
31
+
32
+
33
+
34
+
35
+ public String username;
36
+
37
+
38
+
39
+ public String newSet(LayoutInflater inflater, final Context context) {
40
+
41
+
42
+
43
+ //この下がエラーの出ている30行目です。
44
+
45
+ final View layout = inflater.inflate(R.layout.dialog_contact_us, (ViewGroup) findViewById(R.id.layout_root));
46
+
47
+ AlertDialog.Builder builder = new AlertDialog.Builder(context);
48
+
49
+ builder.setView(layout);
50
+
51
+
52
+
53
+ builder.setPositiveButton("新規登録", new DialogInterface.OnClickListener() {
54
+
55
+ public void onClick(DialogInterface dialog, int which) {
56
+
57
+ //この中で、EditTextに入力されたusernameの処理をしています。
58
+
59
+ EditText getusername2 = (EditText) layout.findViewById(R.id.username);
60
+
61
+ String username = getusername2.getText().toString();
62
+
63
+ }
64
+
65
+ });
66
+
67
+
68
+
69
+ AlertDialog alertDialog = builder.create();
70
+
71
+ alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
72
+
73
+ @Override
74
+
75
+ public void onShow(DialogInterface arg0) {
76
+
77
+ EditText getusername2 = (EditText) layout.findViewById(R.id.username);
78
+
79
+ InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
80
+
81
+ inputMethodManager.showSoftInput(getusername2, 0);
82
+
83
+ }
84
+
85
+ });
86
+
87
+ alertDialog.show();
88
+
89
+
90
+
91
+ return username;
92
+
93
+
94
+
95
+ }
96
+
97
+
98
+
99
+ }
100
+
101
+ ```
102
+
103
+ //そして、呼び出し元であるLocationActivityのコードです。戻り値を取得してるところでエラーが出ています。
104
+
105
+ ```java
106
+
107
+ @Override
108
+
109
+ public boolean onOptionsItemSelected(MenuItem item) {
110
+
111
+ int id = item.getItemId();
112
+
113
+ if (id == R.id.action_setlogin) {
114
+
115
+ LayoutInflater inflater = (LayoutInflater) this.getSystemService(
116
+
117
+ LAYOUT_INFLATER_SERVICE);
118
+
119
+
120
+
121
+ Alart alart=new Alart();
122
+
123
+ //この下がエラーの出ている250行目です。
124
+
125
+ username=alart.newSet(inflater,this);
126
+
127
+ spinnerItems = favorite.favorite(LocationActivity.this, username);//これでok
128
+
129
+ arrayadapter();//spinnerのlistの更新のためのmethod
130
+
131
+
132
+
133
+ ```

1

説明の追加

2017/06/09 18:17

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -1 +1,5 @@
1
1
  new AlertDialog.Builderを、methodに入れて、Activityから呼び出すことは、可能ですか?
2
+
3
+
4
+
5
+ また、AlertDialog専用のクラスを作るということを、以前に、人に教わったのですが、現在、私は、新しく作ったclassに、様々なAlertと、そのAlertのボタンが押された時の処理をまとめて、書いているのですが、分けたほうがいいでしょうか?