前提
エディットテキストに入力した値からチェックボックスを作り、それの追加や消去を行うアプリを作っています。
実現したいこと
一度作ったチェックボックスは次回アプリ起動時にも消されずに表示されたままになるようにしたいです。
発生している問題・エラーメッセージ
共有プリファレンスに入れた値からアプリ起動時にチェックボックスを作ろうと、layout.addView(view); の一文を入れるとエラーでアプリが落ちてしまいます。
該当のソースコード
MainActivity.java
1package com.example.check2; 2import androidx.appcompat.app.AlertDialog; 3import androidx.appcompat.app.AppCompatActivity; 4 5import android.content.DialogInterface; 6import android.content.SharedPreferences; 7import android.os.Bundle; 8import android.preference.Preference; 9import android.preference.PreferenceManager; 10import android.view.View; 11import android.widget.Button; 12import android.widget.EditText; 13import android.widget.LinearLayout; 14import android.widget.TextView; 15 16public class MainActivity extends AppCompatActivity { 17 18 Button add; 19 AlertDialog dialog; 20 LinearLayout layout; 21 String s = ""; 22 String s1; 23 SharedPreferences pref; 24 SharedPreferences.Editor e; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 31 pref = PreferenceManager.getDefaultSharedPreferences(this); 32 33 add = findViewById(R.id.add); 34 layout = findViewById(R.id.container); 35 36 buildDialog(); 37 38 add.setOnClickListener(new View.OnClickListener() { 39 @Override 40 public void onClick(View v) { 41 dialog.show(); 42 } 43 }); 44 45 //もし共有プリファレンスに値があったらそれのチェックボックスを作る処理(addCard2)へ 46 s1 = pref.getString("current",null); 47 if (s1 == null) { 48 return; 49 }else{ 50 addCard2(); 51 } 52 } 53 54 private void buildDialog() { 55 AlertDialog.Builder builder = new AlertDialog.Builder(this); 56 View view = getLayoutInflater().inflate(R.layout.dialog, null); 57 58 final EditText name = view.findViewById(R.id.nameEdit); 59 60 builder.setView(view); 61 builder.setTitle("もちものついか") 62 .setPositiveButton("ついか", new DialogInterface.OnClickListener() { 63 @Override 64 public void onClick(DialogInterface dialog, int which) { 65 addCard(name.getText().toString()); 66 name.setText(""); 67 } 68 }) 69 .setNegativeButton("やめる", new DialogInterface.OnClickListener() { 70 @Override 71 public void onClick(DialogInterface dialog, int which) { 72 name.setText(""); 73 74 } 75 }); 76 dialog = builder.create(); 77 } 78 79 private void addCard(String name) { 80 final View view = getLayoutInflater().inflate(R.layout.card, null); 81 pref = PreferenceManager.getDefaultSharedPreferences(this); 82 83 SharedPreferences.Editor e = pref.edit(); 84 85 TextView nameView = view.findViewById(R.id.name); 86 Button delete = view.findViewById(R.id.delete); 87 88 nameView.setText(name); 89 90 91// 次回起動時もチェックボックスが残る様に共有プリファレンスに名前を保存 92 s = s + name + ","; 93 e.putString("current", s); 94 e.commit(); 95 96 delete.setOnClickListener(new View.OnClickListener() { 97 @Override 98 public void onClick(View v) { 99 layout.removeView(view); 100 } 101 }); 102 layout.addView(view); 103 } 104 105 106// アプリ起動時の共有プリファレンスチェックボックス作成処理 107 private void addCard2() { 108 final View view = getLayoutInflater().inflate(R.layout.card, null); 109 pref = PreferenceManager.getDefaultSharedPreferences(this); 110 111 TextView nameView = view.findViewById(R.id.name); 112 113 s1 = pref.getString("current",null); 114 115// s1の値を","で区切って一次元配列(s2)にする 116 String[] s2 = s1.split(","); 117 118// 一次元配列 s2の内容を使ってチェックボックスを作成 119 for(int i = 0; i < s2.length; i++){ 120 nameView.setText(s2[i]);. 121// ↓↓↓↓↓↓この1文を入れるとエラーが出てしまいます↓↓↓↓↓↓ 122 layout.addView(view); 123 } 124 } 125}
activityMain.xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:id="@+id/add" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="✙ ついか" 15 android:layout_marginBottom="10dp" 16 android:layout_centerHorizontal="true" 17 android:layout_alignParentBottom="true"/> 18 19 20 <ScrollView 21 android:layout_width="match_parent" 22 android:layout_height="match_parent" 23 android:layout_marginBottom="70dp" 24 android:scrollbars="none"> 25 <LinearLayout 26 android:id="@+id/container" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:orientation="vertical"> 30 31 </LinearLayout> 32 </ScrollView> 33</RelativeLayout>
dialog.xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:id="@+id/add" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="✙ ついか" 15 android:layout_marginBottom="10dp" 16 android:layout_centerHorizontal="true" 17 android:layout_alignParentBottom="true"/> 18 19 20 <ScrollView 21 android:layout_width="match_parent" 22 android:layout_height="match_parent" 23 android:layout_marginBottom="70dp" 24 android:scrollbars="none"> 25 <LinearLayout 26 android:id="@+id/container" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:orientation="vertical"> 30 31 </LinearLayout> 32 </ScrollView> 33</RelativeLayout>
card.xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 xmlns:app="http://schemas.android.com/apk/res-auto"> 6 7 <RelativeLayout 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 > 11 12 <androidx.cardview.widget.CardView 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:layout_margin="10dp" 16 app:cardCornerRadius="10dp" 17 app:cardElevation="10dp"> 18 19 <RelativeLayout 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:padding="10dp"> 23 24 <CheckBox 25 android:id="@+id/name" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:textColor="@android:color/holo_purple" 29 android:textFontWeight="1000" 30 android:textSize="20dp"/> 31 32 <Button 33 android:id="@+id/delete" 34 android:text="✖" 35 android:layout_width="50dp" 36 android:layout_height="50dp" 37 android:layout_alignParentEnd="true" 38 android:layout_alignParentRight="true"/> 39 </RelativeLayout> 40 </androidx.cardview.widget.CardView> 41 </RelativeLayout> 42</RelativeLayout>
試したこと
プログラムを消したり増やしたり。
補足情報(FW/ツールのバージョンなど)
Nexus6P API 28をアンドロイドスタジオ上では使用しています。

回答1件
あなたの回答
tips
プレビュー