本のサンプルコードしたのですが、java.lang.RuntimeExceptionがでます。
他にも、java.lang.ClassNotFoundExceptionもでます。
右も左もわからず如何すればいいのでしょうか?
本気で学ぶAndroidアプリ開発入門 SDK5対応
のTODOリストアプリを作るコードです。
アドバイスをいただきたいです。宜しくお願いします。
MainActivityです。
java
1package com.yokmama.learn10.chapter06.lesson28; 2 3import android.os.Bundle; 4import android.support.v4.app.FragmentActivity; 5import android.support.v7.app.AppCompatActivity; 6import android.widget.FrameLayout; 7 8import java.util.List; 9 10 11public class MainActivity extends AppCompatActivity { 12 13 private List<Todo> mTodoList; 14 15 private boolean mIsTablet = false; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 //ダミーデータ作成 23 mTodoList = Todo.addDummyItem(); 24 25 //TODOリスト一覧を表示 26 showTodoList(); 27 28 //タブレットレイアウトなら右側にフォーム画面を表示 29 FrameLayout container2 = (FrameLayout) findViewById(R.id.container2); 30 if (container2 != null) { 31 mIsTablet = true; 32 showTodoForm(mTodoList.get(0)); 33 } 34 } 35 36 @Override 37 public void onBackPressed() { 38 if (getSupportFragmentManager().getBackStackEntryCount() > 1) { 39 //フォーム画面を開いている場合は画面を閉じる 40 getSupportFragmentManager().popBackStack(); 41 } else { 42 //リスト画面の場合は通常のバックキー処理(アプリを終了) 43 super.onBackPressed(); 44 } 45 } 46 47 /** 48 * TODOリスト一覧を表示 49 */ 50 public void showTodoList() { 51 String tag = TodoListFragment.TAG; 52 getSupportFragmentManager().beginTransaction().replace(R.id.container, 53 TodoListFragment.newInstance(), tag).commit(); 54 } 55 56 /** 57 * TODOフォーム画面を表示 58 * 59 * @param item TODOリストデータ 60 */ 61 public void showTodoForm(Todo item) { 62 String tag = TodoFormFragment.TAG; 63 TodoFormFragment fragment; 64 if (item == null) { 65 fragment = TodoFormFragment.newInstance(); 66 } else { 67 fragment = TodoFormFragment.newInstance(item.getColorLabel(), 68 item.getValue(), item.getCreatedTime()); 69 } 70 if (!mIsTablet) { 71 //スマートフォンレイアウトの場合はcontainerに表示 72 getSupportFragmentManager().beginTransaction().replace(R.id.container, 73 fragment, tag).addToBackStack(tag).commit(); 74 }else{ 75 //タブレットレイアウトの場合はcontainer2に表示 76 getSupportFragmentManager().beginTransaction().replace(R.id.container2, 77 fragment, tag).addToBackStack(tag).commit(); 78 } 79 } 80 81 public List<Todo> getTodoList() { 82 return mTodoList; 83 } 84 85 /** 86 * タブレットか判定. 87 * @return 88 */ 89 public boolean isTablet() { 90 return mIsTablet; 91 } 92} 93 94 95
TODO.javaです。
java
1package com.yokmama.learn10.chapter06.lesson28; 2 3import java.util.ArrayList; 4import java.util.List; 5 6/** 7 * Created by m_iwasaki on 15/03/12. 8 */ 9public class 10 11 12 Todo { 13 14 private int colorLabel; 15 16 private long createdTime; 17 18 private String value; 19 20 public static interface ColorLabel { 21 22 public static final int NONE = 1; 23 public static final int PINK = 2; 24 public static final int INDIGO = 3; 25 public static final int GREEN = 4; 26 public static final int AMBER = 5; 27 } 28 29 public Todo(int colorLabel, String value, long createdTime) { 30 this.colorLabel = colorLabel; 31 this.value = value; 32 this.createdTime = createdTime; 33 } 34 35 public int getColorLabel() { 36 return colorLabel; 37 } 38 39 public void setColorLabel(int colorLabel) { 40 this.colorLabel = colorLabel; 41 } 42 43 public String getValue() { 44 return value; 45 } 46 47 public void setValue(String value) { 48 this.value = value; 49 } 50 51 public long getCreatedTime() { 52 return createdTime; 53 } 54 55 public void setCreatedTime(long createdTime) { 56 this.createdTime = createdTime; 57 } 58 59 /** 60 * テスト表示用にダミーのリストアイテムを作成. 61 */ 62 public static List<Todo> addDummyItem() { 63 List<Todo> items = new ArrayList<>(); 64 items.add(new Todo(Todo.ColorLabel.INDIGO, "猫に小判", System.currentTimeMillis() + 1)); 65 items.add(new Todo(Todo.ColorLabel.PINK, "猫の手も借りたい", System.currentTimeMillis() + 2)); 66 items.add(new Todo(Todo.ColorLabel.GREEN, "窮鼠猫を噛む", System.currentTimeMillis() + 3)); 67 items.add(new Todo(Todo.ColorLabel.AMBER, 68 "猫は三年飼っても三日で恩を忘れる", System.currentTimeMillis() + 4)); 69 items.add(new Todo(Todo.ColorLabel.NONE, "猫も杓子も", System.currentTimeMillis() + 5)); 70 return items; 71 } 72}
TODOListAdapter.javaです。
java
1package com.yokmama.learn10.chapter06.lesson28; 2 3import android.content.Context; 4import android.text.TextUtils; 5import android.view.LayoutInflater; 6import android.view.View; 7import android.view.ViewGroup; 8import android.widget.ArrayAdapter; 9import android.widget.TextView; 10 11import java.text.SimpleDateFormat; 12import java.util.Date; 13import java.util.List; 14 15public class TodoListAdapter extends ArrayAdapter<Todo> { 16 17 /** フィールド */ 18 private LayoutInflater mInflator; 19 20 public TodoListAdapter(Context context, List<Todo> objects) { 21 super(context, 0, objects); 22 mInflator = (LayoutInflater) context 23 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 24 } 25 26 @Override 27 public View getView(final int position, View convertView, ViewGroup parent) { 28 ViewHolder holder; 29 if (convertView == null) { 30 convertView = mInflator.inflate(R.layout.item_list_row, parent, false); 31 holder = new ViewHolder(); 32 holder.tvColorLabel = (TextView) convertView.findViewById(R.id.color_label); 33 holder.tvValue = (TextView) convertView.findViewById(R.id.value); 34 holder.tvCreatedTime = (TextView) convertView.findViewById(R.id.created_time); 35 convertView.setTag(holder); 36 } else { 37 holder = (ViewHolder) convertView.getTag(); 38 } 39 40 //値をセット 41 String value = getItem(position).getValue(); 42 if (!TextUtils.isEmpty(value)) { 43 holder.tvValue.setText(value); 44 } 45 46 //カラーラベルをセット 47 int color = getItem(position).getColorLabel(); 48 holder.tvColorLabel.setBackgroundResource(getColorLabelResource(color)); 49 if (!TextUtils.isEmpty(value)) { 50 holder.tvColorLabel.setText(value.substring(0, 1)); 51 } 52 53 //日付をセット 54 String createdtime = getCreatedTime(getItem(position).getCreatedTime()); 55 if (!TextUtils.isEmpty(createdtime)) { 56 holder.tvCreatedTime.setText(createdtime); 57 } 58 59 return convertView; 60 } 61 62 /** 63 * 現在年月をDate型返却. 64 */ 65 public static String getCreatedTime(long createdTime) { 66 Date date = new Date(createdTime); 67 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/HH:mm"); 68 return sdf.format(date); 69 } 70 71 /** 72 * カラーラベルのdrawableリソースIDを返却. 73 * 74 * @param color : カラー 75 */ 76 private int getColorLabelResource(int color) { 77 int resId = R.drawable.bg_colorlabel_grey; 78 switch (color) { 79 case Todo.ColorLabel.PINK: 80 resId = R.drawable.bg_colorlabel_pink; 81 break; 82 case Todo.ColorLabel.INDIGO: 83 resId = R.drawable.bg_colorlabel_indigo; 84 break; 85 case Todo.ColorLabel.GREEN: 86 resId = R.drawable.bg_colorlabel_green; 87 break; 88 case Todo.ColorLabel.AMBER: 89 resId = R.drawable.bg_colorlabel_amber; 90 break; 91 } 92 return resId; 93 } 94 95 private class ViewHolder { 96 97 TextView tvColorLabel; 98 99 TextView tvCreatedTime; 100 101 TextView tvValue; 102 } 103} 104 105 106 107エラーログです。 108```java 109lesson28 E/Trace: error opening trace file: No such file or directory (2)
回答2件
あなたの回答
tips
プレビュー