前提・実現したいこと
「Android StudioのListViewチュートリアル」というサイトを参考にさせていただき、Android Studioを用いて、リストビューを表示するアプリケーションを自分で作ろうと思いました。上記のサイトはあくまで参考にさせていただいたもので、自分の作っているアプリケーションは上記から改変を行っています。
私の作ろうと考えているアプリケーションは、それぞれの項目に年齢、名前、趣味を表示させるリストビューの表示を行うというものです。
発生している問題・エラーメッセージ
しかし、いざ実行させようとすると、実行自体は可能なのですが、すぐに落ちてしまいます。以下のソースコードのコメントに示す箇所が落ちるところです。
アプリが落ちずにリストビューが正しく表示出来るよう、アドバイスなどを頂けたらと思いますので、よろしくお願いします。
該当のソースコード
MainActivity.java
java
1public class MainActivity extends AppCompatActivity { 2 3 private static final String TAG = MainActivity.class.getSimpleName(); 4 private ListView lv; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 lv = (ListView) findViewById(R.id.listView1); 12 List<Profile> profiles = new ArrayList<Profile>(); 13 14 profiles.add(new Profile("bob","cooking",11)); //この箇所で落ちてしまう 15 profiles.add(new Profile("takeshi","baseball",12)); 16 profiles.add(new Profile("hirotaka","darts",13)); 17 18 ProfileAdapter adapter = new ProfileAdapter(this, 0, profiles); 19 lv.setAdapter(adapter); 20 } 21 22}
Profile.java
java
1public class Profile { 2 private static final String TAG = Profile.class.getSimpleName(); 3 private String name; //名前 4 private String hobby; //趣味 5 private int age; //年齢 6 /** 7 * @param name 8 * @param hobby 9 * @param age 10 */ 11 public Profile(String name, String hobby, int age) { 12 super(); 13 this.name = name; 14 this.hobby = hobby; 15 this.age = age; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public String getHobby() { 24 return hobby; 25 } 26 public void setHobby(String hobby) { 27 this.hobby = hobby; 28 } 29 public int getAge() { 30 return age; 31 } 32 public void setAge(int age) { 33 this.age = age; 34 } 35}
ProfileAdapter.java
java
1public class ProfileAdapter extends ArrayAdapter<Profile> { 2 3 private static final String TAG = ProfileAdapter.class.getSimpleName(); 4 5 private static class ViewHolder { 6 public TextView nameTextView; 7 public TextView hobbyTextView; 8 public TextView ageTextView; 9 10 /** 11 * @param nameTextView 12 * @param hobbyTextView 13 * @param ageTextView 14 */ 15 public ViewHolder(View v) { 16 super(); 17 this.nameTextView = (TextView) v.findViewById(R.id.textName); 18 this.hobbyTextView = (TextView) v.findViewById(R.id.textHobby); 19 this.ageTextView = (TextView)v.findViewById(R.id.textAge); 20 } 21 } 22 // 23 private LayoutInflater layoutInflater; 24 25 /** 26 * @param context 27 * @param resource 28 * @param objects 29 */ 30 public ProfileAdapter(Context context, int resource, List<Profile> objects) { 31 super(context, resource, objects); 32 this.layoutInflater = (LayoutInflater) context 33 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 34 } 35 @Override 36 public View getView(int position, View convertView, ViewGroup parent) { 37 ViewHolder holder; 38 if (convertView == null) { 39 convertView = layoutInflater.inflate(R.layout.list_profile, null); 40 holder = new ViewHolder(convertView); 41 convertView.setTag(holder); 42 } else { 43 holder = (ViewHolder) convertView.getTag(); 44 } 45 46 Profile item = getItem(position); 47 holder.nameTextView.setText(item.getName()); 48 holder.hobbyTextView.setText(item.getHobby()); 49 holder.ageTextView.setText(item.getAge()); 50 51 return convertView; 52 } 53}
Activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout 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 <ListView 11 android:id="@+id/listView1" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" /> 14</android.support.constraint.ConstraintLayout>
list_profile.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" > 10 11 <TextView 12 android:id="@+id/textAge" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="TextView" /> 16 17 <LinearLayout 18 android:layout_width="wrap_content" 19 android:layout_height="match_parent" 20 android:orientation="vertical" > 21 22 <TextView 23 android:id="@+id/textName" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:text="TextView" /> 27 28 <TextView 29 android:id="@+id/textHobby" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:text="TextView" /> 33 34 </LinearLayout> 35 36 </LinearLayout> 37</LinearLayout>
補足情報
list_profile.xmlのDesignにおいて、「Failed to load AppCompat ActionBar with unknown error.」及び、「This LinearLayout layout or its LinearLayout parent is useless
A layout with children that has no sibliings,is not a sscrollview or a root layout,and does not have a background, can be removed and have its children moved directly into the parent for a flatter and more efficient layout hierarchy.」と表示されていました。この箇所が手がかりになるのではないかと思われます。
回答1件
あなたの回答
tips
プレビュー