私は今Android Studioでこのサイトを参考にメモアプリを作っています。
ですがいざ実行しようと思って実行したらアプリがすぐに消えてしまいます。
原因がわかる方ご教授お願いします。
全てのコードです。↓
Kotlin
1ListActivity 2 3package com.example.taguchi.simplememo 4 5import android.content.Intent 6import android.support.v7.app.AppCompatActivity 7import android.os.Bundle 8import android.view.View 9import android.widget.* 10 11class ListActivity : AppCompatActivity() { 12 13 // MemoOpenHelperクラスを定義 14 internal var helper: MemoOpenHelper? = null 15 16 override fun onCreate(savedInstanceState: Bundle?) { 17 super.onCreate(savedInstanceState) 18 setContentView(R.layout.activity_list) 19 20 // データベースから値を取得する 21 if (helper == null) { 22 helper = MemoOpenHelper(this@ListActivity) 23 } 24 // メモリストデータを格納する変数 25 val memoList = ArrayList<HashMap<String, String>>() 26 // データベースを取得する 27 val db = helper!!.writableDatabase 28 try { 29 // rawQueryというSELECT専用メソッドを使用してデータを取得する 30 val c = db.rawQuery("select uuid, body from MEMO_TABLE order by id", null) 31 // Cursorの先頭行があるかどうか確認 32 var next = c.moveToFirst() 33 34 // 取得した全ての行を取得 35 while (next) { 36 val data = HashMap<String, String>() 37 // 取得したカラムの順番(0から始まる)と型を指定してデータを取得する 38 val uuid = c.getString(0) 39 var body = c.getString(1) 40 if (body.length > 10) { 41 // リストに表示するのは10文字まで 42 body = body.substring(0, 11) + "..." 43 } 44 // 引数には、(名前,実際の値)という組合せで指定します 名前はSimpleAdapterの引数で使用します 45 data["body"] = body 46 data["id"] = uuid 47 memoList.add(data) 48 // 次の行が存在するか確認 49 next = c.moveToNext() 50 } 51 } finally { 52 // finallyは、tryの中で例外が発生した時でも必ず実行される 53 // dbを開いたら確実にclose 54 db.close() 55 } 56 57 /* 仮のデータを作成 58 ArrayList<HashMap<String, String>> tmpList = new ArrayList<>(); 59 for(int i = 1; i <= 5; i++){ 60 HashMap<String,String> data = new HashMap<>(); 61 // 引数には、(名前,実際の値)という組合せで指定します 名前はSimpleAdapterの引数で使用します 62 data.put("body","サンプルデータ"+i); 63 data.put("id","sampleId"+i); 64 tmpList.add(data); 65 }*/ 66 67 // Adapter生成 68 val simpleAdapter = SimpleAdapter(this, 69 memoList, // 使用するデータ 70 android.R.layout.simple_list_item_2, // 使用するレイアウト 71 arrayOf("body", "id"), // どの項目を 72 intArrayOf(android.R.id.text1, android.R.id.text2) // どのidの項目に入れるか 73 ) 74 75 // idがmemoListのListViewを取得 76 val listView = findViewById<View>(R.id.memoList) as ListView 77 listView.adapter = simpleAdapter 78 79 // リスト項目をクリックした時の処理 80 listView.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id -> 81 /** 82 * @param parent ListView 83 * @param view 選択した項目 84 * @param position 選択した項目の添え字 85 * @param id 選択した項目のID 86 */ 87 // インテント作成 第二引数にはパッケージ名からの指定で、遷移先クラスを指定 88 val intent = Intent(this@ListActivity, com.example.taguchi.simplememo.CreateMemoActivity::class.java) 89 90 // 選択されたビューを取得 TwoLineListItemを取得した後、text2の値を取得する 91 val two = view as TwoLineListItem 92 // TextView idTextView = (TextView)two.findViewById(android.R.id.text2); 93 val idTextView = two.text2 as TextView 94 val isStr = idTextView.text as String 95 // 値を引き渡す (識別名, 値)の順番で指定します 96 intent.putExtra("id", isStr) 97 // Activity起動 98 startActivity(intent) 99 } 100 101 102 /** 103 * 新規作成するボタン処理 104 */ 105 // idがnewButtonのボタンを取得 106 val newButton = findViewById<View>(R.id.newButton) as Button 107 // clickイベント追加 108 newButton.setOnClickListener { 109 // CreateMemoActivityへ遷移 110 val intent = Intent(this@ListActivity, com.example.taguchi.simplememo.CreateMemoActivity::class.java) 111 intent.putExtra("id", "") 112 startActivity(intent) 113 } 114 } 115}
Kotlin
1activity_create_memo.xml 2 3<?xml version="1.0" encoding="utf-8"?> 4<LinearLayout 5 xmlns:android="http://schemas.android.com/apk/res/android" 6 xmlns:app="http://schemas.android.com/apk/res-auto" 7 xmlns:tools="http://schemas.android.com/tools" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:orientation="vertical" 11 tools:context="com.〇〇.〇〇.simplememo.CreateMemoActivity"> 12 13 <EditText 14 android:id="@+id/body" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:textSize="20sp" 18 android:inputType="textMultiLine" 19 android:lines="50" 20 android:layout_weight="9" 21 android:gravity="top|left" 22 /> 23 24 <LinearLayout 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:orientation="horizontal" 28 android:layout_weight="1"> 29 30 <Button 31 android:id="@+id/register" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:layout_weight="1" 35 android:text="登録する" 36 /> 37 38 <Button 39 android:id="@+id/back" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:layout_weight="1" 43 android:text="戻る" 44 /> 45 46 </LinearLayout> 47 48 49</LinearLayout>
Kotlin
1CreateMemoActivity 2 3package com.〇〇.〇〇.simplememo 4 5import android.content.Intent 6import android.support.v7.app.AppCompatActivity 7import android.os.Bundle 8import android.view.View 9import android.widget.Button 10import android.widget.EditText 11import android.widget.TextView 12import java.util.* 13 14class CreateMemoActivity : AppCompatActivity() { 15 16 // MemoOpenHelperクラスを定義 17 internal var helper: MemoOpenHelper? = null 18 // 新規フラグ 19 internal var newFlag = false 20 // id 21 internal var id = "" 22 23 override fun onCreate(savedInstanceState: Bundle?) { 24 super.onCreate(savedInstanceState) 25 setContentView(R.layout.activity_create_memo) 26 27 // データベースから値を取得する 28 if (helper == null) { 29 helper = MemoOpenHelper(this@CreateMemoActivity) 30 } 31 32 33 // ListActivityからインテントを取得 34 val intent = this.intent 35 // 値を取得 36 id = intent.getStringExtra("id") 37 // 画面に表示 38 if (id == "") { 39 // 新規作成の場合 40 newFlag = true 41 } else { 42 // 編集の場合 データベースから値を取得して表示 43 // データベースを取得する 44 val db = helper!!.writableDatabase 45 try { 46 // rawQueryというSELECT専用メソッドを使用してデータを取得する 47 val c = db.rawQuery("select body from MEMO_TABLE where uuid = '$id'", null) 48 // Cursorの先頭行があるかどうか確認 49 var next = c.moveToFirst() 50 // 取得した全ての行を取得 51 while (next) { 52 // 取得したカラムの順番(0から始まる)と型を指定してデータを取得する 53 val dispBody = c.getString(0) 54 val body = findViewById<View>(R.id.body) as EditText 55 body.setText(dispBody, TextView.BufferType.NORMAL) 56 next = c.moveToNext() 57 } 58 } finally { 59 // finallyは、tryの中で例外が発生した時でも必ず実行される 60 // dbを開いたら確実にclose 61 db.close() 62 } 63 } 64 65 /** 66 * 登録ボタン処理 67 */ 68 // idがregisterのボタンを取得 69 val registerButton = findViewById<View>(R.id.register) as Button 70 // clickイベント追加 71 registerButton.setOnClickListener { 72 // 入力内容を取得する 73 val body = findViewById<View>(R.id.body) as EditText 74 val bodyStr = body.text.toString() 75 76 // データベースに保存する 77 val db = helper!!.writableDatabase 78 try { 79 if (newFlag) { 80 // 新規作成の場合 81 // 新しくuuidを発行する 82 id = UUID.randomUUID().toString() 83 // INSERT 84 db.execSQL("insert into MEMO_TABLE(uuid, body) VALUES('$id', '$bodyStr')") 85 } else { 86 // UPDATE 87 db.execSQL("update MEMO_TABLE set body = '$bodyStr' where uuid = '$id'") 88 } 89 } finally { 90 // finallyは、tryの中で例外が発生した時でも必ず実行される 91 // dbを開いたら確実にclose 92 db.close() 93 } 94 // 保存後に一覧へ戻る 95 val intent = Intent(this@CreateMemoActivity, com.〇〇.〇〇.simplememo.ListActivity::class.java) 96 startActivity(intent) 97 } 98 99 100 /** 101 * 戻るボタン処理 102 */ 103 // idがbackのボタンを取得 104 val backButton = findViewById<View>(R.id.back) as Button 105 // clickイベント追加 106 backButton.setOnClickListener { 107 // 保存せずに一覧へ戻る 108 finish() 109 } 110 } 111}
ここからは文字制限の都合上、スクショで載せます。
何か不足している部分があればご指摘お願いします。
発生している問題・エラーメッセージ
回答1件
あなたの回答
tips
プレビュー