Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Set items inside View from Adapter get NullPointerException
    primarykey
    data
    text
    <p>I got a Layout and when press a Button called "Add", it add one inflated view inside this Layout and start a method to set all items inside this View (Buttons, ListView..etc). So, I can make a list, inside the Layout, of my inflated Views and save it on Database.</p> <p>My problem is, when i load the data from database, set this data on an ArrayAdapter and insert it into the Layout, when i call the method to set the items, i get a "NullPointerException"</p> <p>This is the Activity:</p> <pre><code> 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 &lt; 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&lt;?&gt; 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); } </code></pre> <p>logcat:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload