0

我有一个布局,当按下一个名为“添加”的按钮时,它会在此布局内添加一个膨胀视图并启动一个方法来设置此视图内的所有项目(按钮、ListView..等)。因此,我可以在布局内列出我的膨胀视图并将其保存在数据库中。

我的问题是,当我从数据库加载数据时,将此数据设置在 ArrayAdapter 上并将其插入布局中,当我调用该方法来设置项目时,我得到一个“NullPointerException”

这是活动:

    public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record_tab);

    user = new UserBean();
    username = ( EditText ) findViewById( R.id.et_username );
    data = ( EditText ) findViewById( R.id.et_data );

    formatDateTime = DateFormat.getDateTimeInstance();
    dateTime = Calendar.getInstance();

            //this is the layout
    listRecord = ( LinearLayout ) findViewById( R.id.listRecord );
    initInsertButton();

  //this is an Intent to get data from other activity and where i call the adapter to add views automatically
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        user_id = extras.getInt("id");
        data_ = extras.getString("data"); 

        accessObjectUser = new UserDAO( getApplicationContext() );
        UserBean user = accessObjectUser.oneDataById(user_id);              

        username.setText(user.getNome()+" "+user.getSobrenome());
        data.setText(data_);

        accessObjectRec = new RecordDAO ( getApplicationContext() );
        recs = accessObjectRec.recDataByUserId(user_id, data_);

        adapterRec = new RecordTagAdapter( getApplicationContext(),
                R.layout.record_tag,
                rec );

        //here i set the views from adapter into layout
                    for (int i = 0; i < adapterRec.getCount(); i++) {
            View item = adapterRec.getView(i, null, null);
            listRecord.addView(item);
        }


    //here the button to add the view manually

private void initInsertButton() {


    insertRecord = ( Button ) findViewById( R.id.buttonInsertRecord );
    insertRecord.setOnClickListener( new OnClickListener(){     
        public void onClick(View v)
        {
            LayoutInflater inflate = ( LayoutInflater ) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            view = inflate.inflate( R.layout.record_tag, null );

            initRecTag();           
            listRecord.addView( view );

        }
    } );

}       

         //setting all items inside view added

          private void initRecTag(){        

    final TextView f_id = ( TextView ) view.findViewById(R.id.tx_food_id);
    final TextView m_id = ( TextView ) view.findViewById(R.id.tx_med_id);
    final EditText search_box = ( EditText ) view.findViewById( R.id.food_search );


    final ListView lstAlimentos = ( ListView ) view.findViewById( R.id.listView1 );

    search_box.addTextChangedListener(new TextWatcher() {           
           public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {       

           }

           public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                   int arg3) {
               // TODO Auto-generated method stub    
           }

           public void afterTextChanged(Editable arg0) {

           }
    });

    lstAlimentos.setOnItemClickListener( new OnItemClickListener() {

        public void onItemClick( AdapterView<?> parent, View views, int position, long id ){
            ...
    }       


    insertHorario = ( TextView ) view.findViewById(R.id.tx_timer);
    insertHorario.setOnClickListener( new OnClickListener() {

        public void onClick(View v) {
                   timeSet();                   
        }

    });


    deleteRecord = ( Button ) view.findViewById( R.id.bt_excludeRec );
    deleteRecord.setOnClickListener( new OnClickListener()
    {
        View viewTemp = view;
        public void onClick(View v)
        {
            listRecord.removeView(viewTemp);
        }
    } );
}


public void dataSet(){


    data.setOnClickListener( new OnClickListener() {

        public void onClick(View v) {

            showDialog(DATE_DIALOG_ID);

            final Calendar cal = Calendar.getInstance();
            cal.getTime();

            pYear = cal.get(Calendar.YEAR);
            pMonth = cal.get(Calendar.MONTH);
            pDay = cal.get(Calendar.DAY_OF_MONTH);



        }
    });     
}   

private DatePickerDialog.OnDateSetListener pDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, 
            int monthOfYear, int dayOfMonth) {
        pYear = year;
        pMonth = monthOfYear;
        pDay = dayOfMonth;
        updateDisplay();

    }
};


   public void updateDisplay() {
        data.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(pDay).append("-")
                    .append(pMonth + 1).append("-")                     
                    .append(pYear).append(" "));
    }



public void timeSet(){

    new TimePickerDialog( RecordTabActivity.this, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();

}


TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        int hour = hourOfDay;
        int minut = minute;

        updateHour(hour, minut);
    }
};

private void updateHour(int hour, int minute) {
    insertHorario.setText( hour+":"+minute );
}


private void updateData(int year, int month, int day) {
    data.setText(day+"-"+month+"-"+day);
}

日志猫:

09-10 19:34:08.711: E/AndroidRuntime(23643): FATAL EXCEPTION: main
09-10 19:34:08.711: E/AndroidRuntime(23643): java.lang.RuntimeException: Unable to     start activity     ComponentInfo{com.centeias.nutrirecord/com.centeias.nutrirecord.RecordTabActivity}:     java.lang.NullPointerException
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1817)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1833)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     android.app.ActivityThread.access$500(ActivityThread.java:124)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1026)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     android.os.Handler.dispatchMessage(Handler.java:99)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     android.os.Looper.loop(Looper.java:132)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     android.app.ActivityThread.main(ActivityThread.java:4134)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     java.lang.reflect.Method.invokeNative(Native Method)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     java.lang.reflect.Method.invoke(Method.java:491)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     dalvik.system.NativeStart.main(Native Method)
09-10 19:34:08.711: E/AndroidRuntime(23643): Caused by: java.lang.NullPointerException
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     com.centeias.nutrirecord.RecordTabActivity.initRecTag(RecordTabActivity.java:358)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     com.centeias.nutrirecord.RecordTabActivity.onCreate(RecordTabActivity.java:254)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     android.app.Activity.performCreate(Activity.java:4411)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
09-10 19:34:08.711: E/AndroidRuntime(23643):    at          android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1781)
09-10 19:34:08.711: E/AndroidRuntime(23643):    ... 11 more
4

0 回答 0