null pointer
相対レイアウトでメソッドaddView()を呼び出してtextViewを追加すると、例外( )が発生しました。
public class FormAddNewTaskActivity extends Activity {
private final String TAG = FormAddNewTaskActivity.class.getCanonicalName();
private TaskLiteApplication taskLiteApplication;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_add_task);
RelativeLayout rl = (RelativeLayout) findViewById(R.layout.form_add_task);
TextView tv = new TextView(taskLiteApplication);
tv.setText("blah blah");
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rl.addView(tv); //null pointer exeption there
//set application context
taskLiteApplication = ((TaskLiteApplication)getApplicationContext());
//Spinner set list user
Spinner s = (Spinner) this.findViewById(R.id.spinnerUser);
//set list user
ArrayList<User> listUser = taskLiteApplication.getCtrlUser().getListUserFromDataBase();
//adapter user
UserSpinnerAdapter userSpinnerAdapter = new UserSpinnerAdapter(listUser , getApplicationContext());
//set adapter on the spinner
s.setAdapter(userSpinnerAdapter);
//set listener on add button task
ImageButton imageButtonAdd = (ImageButton) findViewById(R.id.imageButtonAdd);
imageButtonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "add form new subtask");
}
});
}
}
私のXML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/editTextGroupTaskName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_task_name"
>
<requestFocus />
</EditText>
<Spinner
android:id="@+id/spinnerUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textViewDateDue"
android:layout_below="@+id/editTextGroupTaskName" />
<TextView
android:id="@+id/textViewDateDue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spinnerUser"
android:text="@string/date_due"
android:textColor="#000"
android:textAppearance="?android:attr/textAppearanceMedium" />
<DatePicker
android:id="@+id/datePickerDateDue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewDateDue" />
<TextView
android:id="@+id/textViewSubTask"
android:layout_below="@id/datePickerDateDue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sub_task"
android:textColor="#000"
android:paddingTop="11dp"
android:paddingBottom="11dp"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<ImageButton
android:id="@+id/imageButtonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/datePickerDateDue"
android:src="@drawable/add" />
</RelativeLayout>
</ScrollView>
ログ猫:
07-17 14:07:40.696: D/AndroidRuntime(338): Shutting down VM
...
07-17 14:07:40.707: E/AndroidRuntime(338): Caused by: java.lang.NullPointerException
07-17 14:07:40.707: E/AndroidRuntime(338): at android.view.View.<init>(View.java:1823)
07-17 14:07:40.707: E/AndroidRuntime(338): at android.widget.TextView.<init>(TextView.java:326)
07-17 14:07:40.707: E/AndroidRuntime(338): at android.widget.TextView.<init>(TextView.java:320)
07-17 14:07:40.707: E/AndroidRuntime(338): at android.widget.TextView.<init>(TextView.java:315)
07-17 14:07:40.707: E/AndroidRuntime(338): at activity.FormAddNewTaskActivity.onCreate(FormAddNewTaskActivity.java:38)
07-17 14:07:40.707: E/AndroidRuntime(338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)