質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1993閲覧

Android StudioでListViewが作れない

ehyai

総合スコア43

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2018/09/18 17:11

前提・実現したいこと

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.」と表示されていました。この箇所が手がかりになるのではないかと思われます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kakajika

2018/09/19 05:43

エラーが起きて落ちるなら、まずはエラーログの内容を確認して調べましょう。調べた上でわからないのであれば、エラーログの内容を追記して質問してください。
ehyai

2018/09/19 14:32

詳しく書いておらず、申し訳ありませんでした。logcatでログレベルをerrorにして調べたところ、ProfileAdapter.getView(ProfileAdapter.java:68)の箇所が青く表示され、 「android.content.res.Resources$NotFoundException: String resource ID #0x1」と書かれていました。このことから、ProfileAdapter.javaの68行目でsetText()の中にint文をそのまま入れてしまっていたことが原因であることが分かりました。setTextの中を、「item.getAge()」から「String.valueOf(item.getAge())」に変更しましたところ、正しく動くことが出来ました。このミスに気付くことが出来たのもあなたのおかげです。本当にありがとうございました。
guest

回答1

0

自己解決

以下のように修正しましたところ、正しく動きました。
ProfileAdapter.java
//68行目
holder.ageTextView.setText(item.getAge());

holder.ageTextView.setText(String.valueOf(item.getAge()));

投稿2018/09/19 14:35

ehyai

総合スコア43

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問