2012-05-04 13 views
-2
TextView t =(TextView)findViewById(R.id.place); 
t.setText("A"); 

私はonCreate()メソッド内の上記のコードを呼び出します。私の活動はListActivityです。しかし、なぜこれは私に次のエラーヌルポインターTextViewに値を設定する際の例外

05-04 14:21:40.840: E/AndroidRuntime(16168): Caused by: java.lang.NullPointerException 

を提供し、ここで私はTextViewの

<TextView android:id="@+id/place" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:textSize="16sp" 
      android:textStyle="bold" 
      android:focusable="false" 
      android:text="" 
      android:layout_gravity="left"/> 

とどのようにそれを修正するに定義されたXMLがありますか?

+0

さらにコードを投稿 –

+0

findViewById()は、見つかった場合はビューを返し、そうでない場合はnullを返します。 – yorkw

+0

@ dinesh707あなたはコードを投稿することができますonCreateメソッドのコード – AndroidDev

答えて

2
public class yourclass extends Activity{ 
    TextView t; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.patient); 
      t =(TextView)findViewById(R.id.place); 
      t.setText("A"); 
    } 
} 
0

あなたはリストビューに適切なレイアウトを設定していないようです。

<?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:background="@drawable/gradient_unselected" 
android:orientation="vertical" > 
<ListView 
    android:id="@+id/blacklist_view" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:divider="@drawable/seperator" 
    android:dividerHeight="1dp" > 
</ListView> 

</LinearLayout> 

と行項目のレイアウトが異なるようになります。ListActivityののonCreateで

は幾分このようになるレイアウトを設定します。

関連する問題