やりたいこと
ListViewにアクティビティから得た結果(title)を表示させたいけど、何も表示されない。
試していること
ListViewの更新
不明点
logcatで青文字でどこのアクティビティの何行目でヌルポが吐かれているか書かれておらず、わからないです。
知りたいこと
ヌルポがどこで吐かれているのか・できれば、何が原因でどうすればいいかを知りたいです。
MainActivity
Java
1public class MainActivity extends AppCompatActivity { 2 3 static ArrayList data = new ArrayList<>(); 4 private static final int REQUEST_CODE = 1; 5 private ArrayAdapter adapter; 6 7 @Override 8 protected void onActivityResult(int request_Code, int resultCode, Intent result){ 9 switch(request_Code){ 10 //MakeQuizActivityから戻ってきた場合 11 case(REQUEST_CODE): 12 //クイズを作るボタンを押して戻ってきたときの処理 13 if(resultCode == RESULT_OK){ 14 data.add(result.getStringExtra("INPUT_TILE")); 15 adapter.notifyDataSetChanged(); 16 } 17 //押されなかったときの処理 18 else if(resultCode == RESULT_CANCELED){ 19 20 } 21 break; 22 default: 23 break; 24 } 25 } 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 30 // リスト項目とListViewを対応付けるArrayAdapterを用意する 31 adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);; 32 // ListViewにArrayAdapterを設定する 33 ListView listView = (ListView) findViewById(R.id.listView); 34 listView.setAdapter(adapter); 35 Button button = (Button) findViewById(R.id.makeQuiz_Button1); 36 button.setOnClickListener(new View.OnClickListener() { 37 @Override 38 public void onClick(View v) { 39 Intent intent = new Intent(MainActivity.this, com.test.quizmaker.MakeQuizActivity.class); 40 startActivityForResult(intent, REQUEST_CODE); 41 } 42 }); 43 } 44}
MakeQuizActivity
Java
1public class MakeQuizActivity extends AppCompatActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_make_quiz); 7 8 Button button2 = (Button)findViewById(R.id.makeQuiz_Button2); 9 button2.setOnClickListener(new View.OnClickListener() { 10 @Override 11 public void onClick(View v) { 12 Intent intent = new Intent(MakeQuizActivity.this,com.test.quizmaker.MainActivity.class); 13 PreferenceManager pm = new PreferenceManager(); 14 SharedPreferences sp = getSharedPreferences("DataStore",MODE_PRIVATE); 15 SharedPreferences.Editor editor = sp.edit(); 16 pm.init(sp,editor); 17 String strTitle = ((EditText)findViewById(R.id.editText)).getText().toString(); 18 String strQuiz = ((EditText)findViewById(R.id.editText2)).getText().toString(); 19 String strAnswer = ((EditText)findViewById(R.id.editText3)).getText().toString(); 20 int count = sp.getInt("count",0) + 1; 21 pm.saveInt(count); 22 pm.saveString(strTitle, PreferenceManager.DataKeys.TITLE, String.valueOf(count)); 23 pm.saveString(strQuiz, PreferenceManager.DataKeys.QUIZ, String.valueOf(count)); 24 pm.saveString(strAnswer, PreferenceManager.DataKeys.ANSWER, String.valueOf(count)); 25 String title = sp.getString(PreferenceManager.DataKeys.TITLE.getValue() + String.valueOf(count), "Nothing"); 26 intent.putExtra("INPUT_TITLE", title); 27 setResult(RESULT_OK, intent); 28 finish(); 29 } 30 }); 31 } 32}
Error
Java
1java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference 2 at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:409) 3 at android.widget.ArrayAdapter.getView(ArrayAdapter.java:371) 4 at android.widget.AbsListView.obtainView(AbsListView.java:2362) 5 at android.widget.ListView.makeAndAddView(ListView.java:1970) 6 at android.widget.ListView.fillDown(ListView.java:704) 7 at android.widget.ListView.fillFromTop(ListView.java:765) 8 at android.widget.ListView.layoutChildren(ListView.java:1744) 9 at android.widget.AbsListView.onLayout(AbsListView.java:2161) 10 at android.view.View.layout(View.java:17523) 11 at android.view.ViewGroup.layout(ViewGroup.java:5612) 12 at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915) 13 at android.view.View.layout(View.java:17523) 14 at android.view.ViewGroup.layout(ViewGroup.java:5612) 15 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 16 at android.widget.FrameLayout.onLayout(FrameLayout.java:261) 17 at android.view.View.layout(View.java:17523) 18 at android.view.ViewGroup.layout(ViewGroup.java:5612) 19 at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444) 20 at android.view.View.layout(View.java:17523) 21 at android.view.ViewGroup.layout(ViewGroup.java:5612) 22 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 23 at android.widget.FrameLayout.onLayout(FrameLayout.java:261) 24 at android.view.View.layout(View.java:17523) 25 at android.view.ViewGroup.layout(ViewGroup.java:5612) 26 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) 27 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) 28 at android.widget.LinearLayout.onLayout(LinearLayout.java:1494) 29 at android.view.View.layout(View.java:17523) 30 at android.view.ViewGroup.layout(ViewGroup.java:5612) 31 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 32 at android.widget.FrameLayout.onLayout(FrameLayout.java:261) 33 at com.android.internal.policy.DecorView.onLayout(DecorView.java:724) 34 at android.view.View.layout(View.java:17523) 35 at android.view.ViewGroup.layout(ViewGroup.java:5612) 36 at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342) 37 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069) 38 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246) 39 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301) 40 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871) 41 at android.view.Choreographer.doCallbacks(Choreographer.java:683) 42 at android.view.Choreographer.doFrame(Choreographer.java:619) 43 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857) 44 at android.os.Handler.handleCallback(Handler.java:751) 45 at android.os.Handler.dispatchMessage(Handler.java:95) 46 at android.os.Looper.loop(Looper.java:154) 47 at android.app.ActivityThread.main(ActivityThread.java:6077) 48 at java.lang.reflect.Method.invoke(Native Method) 49 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 50 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
回答1件
あなたの回答
tips
プレビュー