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

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

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

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

Android Studio

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

Q&A

解決済

1回答

5738閲覧

android studio で動的に生成させたリストをレイアウトの定位置に組み込めない。

vn2731vn

総合スコア14

Java

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

Android Studio

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

0グッド

0クリップ

投稿2016/04/17 12:29

編集2016/04/18 07:16

Android Studioで画像とテキストとリストを表示させる画面を作成しています。
xmlファイルで<ImageView>タグ<TextView>タグの下に<TableLayout>タグを配置して、画像とテキストが並列に並んだ下にリストが出るようにします。
javaファイルで配列に格納されたデータが自動的に<TableLayout>タグの中へ表形式で生成されるようコーディングしました。
端末上で実行すると、画像とテキストが表示されず、それらが表示されるべき画面の一番上からリストが出てきてしまいます。
xmlファイルの方で<TableLayout>タグの中に<TableRow><TextView>を書き、データをべた打ちで表を作ると
理想通りの配置で表示されます。
またxmlファイルの<TableLayout>とactivity.javaファイルを紐づけるのに
findViewByIdを使うとtableLayout.addViewメソッドでNullPointerExceptionのエラーが発生します。
画像とテキストの下に動的にリストを生成させるには、どうすれば良いでしょうか?

以下、xmlファイルとjavaファイルです。

list.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

<LinearLayout android:id="@+id/logo_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/image" android:src="@drawable/title_logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" /> <TextView android:id="@+id/date_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginBottom="30dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:textSize="20sp" android:layout_alignParentRight="true"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:layout_marginRight="10dp" android:layout_centerHorizontal="true"> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/HorizontalScrollView"> <TableLayout android:id="@+id/member_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true"> </TableLayout> </HorizontalScrollView> </ScrollView> </LinearLayout>
</LinearLayout>

ListActivity.java

package com.example.xxx.practice;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

/**

  • Created by XXX on 2016/04/07.

*/
public class ListActivity extends Activity{

private String[][] mPersonal_list = { ~(略)~ }; private final int mFP = ViewGroup.LayoutParams.FILL_PARENT; private final int mWC = ViewGroup.LayoutParams.WRAP_CONTENT; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); int list_length = mPersonal_list.length; TableLayout tableLayout = new TableLayout(this); setContentView(tableLayout); tableLayout = (TableLayout)findViewById(R.id.member_list); //↑↑この一文がないとエラーは発生せず、画像とテキストの位置からリストが表示される。 TextView text_number = new TextView(this); text_number.setText("No."); TextView text_name = new TextView(this); text_name.setText("名前"); TextView text_address = new TextView(this); text_address.setText("メールアドレス"); TableRow tableRow_title = new TableRow(this); tableRow_title.addView(text_number); tableRow_title.addView(text_name); tableRow_title.addView(text_address); tableLayout.addView(tableRow_title, createParam(mFP, mWC)); //↑↑ここでNullPointerException発生。 TextView[] number_count = new TextView[list_length]; ・・・*1 TextView[] name_count = new TextView[list_length]; TextView[] address_count = new TextView[list_length]; TableRow[] row_count = new TableRow[list_length]; for(int i=0; i<list_length; i++){ number_count[i] = new TextView(this); ・・・*2 number_count[i].setText(mPersonal_list[i][0]); name_count[i] = new TextView(this); name_count[i].setText(mPersonal_list[i][1]); address_count[i] = new TextView(this); address_count[i].setText(mPersonal_list[i][2]); row_count[i] = new TableRow(this); row_count[i].addView(number_count[i]); row_count[i].addView(name_count[i]); row_count[i].addView(address_count[i]); tableLayout.addView(row_count[i], createParam(mFP, mWC)); } } private TableLayout.LayoutParams createParam(int w, int h){ return new TableLayout.LayoutParams(w, h); }

}

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

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

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

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

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

guest

回答1

0

ベストアンサー

まず、Activity#setContentViewはActivityのルートViewを上書きします。そのために、現状のコードではコード上でインスタンス化したTableLayoutしかActivityに存在しません。そのためImageViewとTextViewは存在しません。
また、Activity#findViewByIdはActivityのルートViewの下にあるViewに付加されているIDからViewを探すので、コード上で追加したViewにはmember_listは存在しないのでActivity#findViewByIdはnullを返します。
そのため、それ以降その参照変数にアクセスするとNullPointerExceptionが、発生します。

回答ではないのですが、コードは専用の記述方があるので質問をそちらの記法に合わせてください。

投稿2016/04/17 13:10

yona

総合スコア18155

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

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

vn2731vn

2016/04/18 07:42

回答ありがとうございます。 setContentView(tableLayout); が、list.xmlの設定を上書きしてしまうということで記述を消去したところ、上手くいきました。 質問時のコードの記述は次回以降、テンプレートを活用したいと思います。 その後、他者レビューを受けたところ、number_countに対してfor文の中と外で2回もインスタンス化する必要はあるのか(*1と*2)と指摘を受けました。 *1と*2どちらか一方のみの記述を試したところその下のaddViewメソッドでNullPointerExceptionが発生してしまいました。 *1は配列そのものの領域を確保するのに必要で、*2はテキストを入れる領域を確保するのに必要。よって*1と*2の記述は二つとも必要。 というように解釈したのですが、この解釈は正しいでしょうか? また*2の引数であるthisとは、何を指しているのでしょうか? 追加質問で恐縮ですが、もし良かったら教えてください。
yona

2016/04/18 09:16

正しい認識だと思います。 thisはインスタンス自身を指します。 ここではActivityがthisを読んでいるので、Activity自身のインスタンスを渡しています。 Viewを画面に表示するときはContextという情報の塊のようなインスタンスが必要になります。これが今回のthisの正体です。 詳しくはリファレンスを読みましょう。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問