I want to execute the function that is fragment - remove task () - on an image that is in the fragment being added in the activity at runtime, and I am not getting it, help me !!!
Activity code
public class MainActivity extends AppCompatActivity {
private ImageView botao_deletar;
public static SQLiteDatabase banco_dados;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Fragment code
public class ListaNotificacoes extends Fragment {
View minha_view;
private ListView lista_notify;
private ArrayAdapter<String> itens_adaptador;
private ArrayList<String> itens;
private ArrayList<Integer> ids;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
minha_view = inflater.inflate(R.layout.lista_notificacoes, container, false);
return minha_view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void remover_tarefa(Integer id){
try {
banco_dados.execSQL("DELETE FROM lista_notificacoes WHERE id="+id);
recupera_tarefa();
}catch (Exception e){
e.printStackTrace();
}
}
What I tried to do ...
public class ListaNotificacoes extends Fragment {
View minha_view;
private ListView lista_notify;
private ImageView botao_deletar;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
minha_view = inflater.inflate(R.layout.lista_notificacoes, container, false);
return minha_view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recupera_tarefa();
botao_deletar = (ImageView) getActivity().findViewById(R.id.botao_deletar);
botao_deletar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remover_tarefa(lista_notify.getId());
}
});
}
private void remover_tarefa(Integer id){
try {
banco_dados.execSQL("DELETE FROM lista_notificacoes WHERE id="+id);
recupera_tarefa();
}catch (Exception e){
e.printStackTrace();
}
}
The error that shows in the console ...
Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference