-1

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)
4

3 に答える 3

1

そこではnullのみrlが可能です。

多分代わりにRelativeLayout rl = (RelativeLayout) findViewById(R.layout.form_add_task)RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative_layout)

于 2012-07-17T14:08:56.180 に答える
1

RelativeLayout rl = (RelativeLayout) findViewById(R.layout.form_add_task);

レイアウトタグをViewファインダーに送信しています。これをXMLに追加します。

<RelativeLayout
        android:id="@+id/form_add_task"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

と電話:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.form_add_task);

于 2012-07-17T14:09:31.333 に答える
1

問題は相対的なレイアウトです。試す:

<RelativeLayout
        android:id="@+id/myLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.myLayout);
于 2012-07-17T14:10:16.247 に答える