findViewByIdのエラー

1,283 views
Skip to first unread message

takeshi honda

unread,
Nov 28, 2011, 9:11:24 AM11/28/11
to 日本Androidの会
android初心者です。

プログラムでTextViewに文字列をセットしたいのですが、実行時エラーがでてしまいます。
エラーメッセージもないので、何が間違っているのかわかりません。
プログラムは以下のとおりです。何がおかしいのか教えていただければ幸いです。
よろしくお願いします。


/////// Io.java //////////
package localhost.study.io;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Io extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//TextView tv1 = new TextView(this); // これは動く
TextView tv1 = (TextView)findViewById(R.id.tv1); // これでエラー
tv1.setText("aaa");
setContentView(tv1);
}
}

/////// layout/main.xml /////////////

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

元木

unread,
Nov 28, 2011, 11:11:58 AM11/28/11
to android-g...@googlegroups.com
こんばんは、元木です。

setText()の後のsetContentViewは、不要です。

takeshi honda

unread,
Dec 1, 2011, 11:10:51 AM12/1/11
to 日本Androidの会
ありがとうございます。
おかげさまでうまくいきました。

On 11月29日, 午前1:11, 元木 <kiyoshi.mot...@gmail.com> wrote:
> こんばんは、元木です。
>
> setText()の後のsetContentViewは、不要です。

Reply all
Reply to author
Forward
0 new messages