0
I want to run the function that is Fragment - remove task() - on an image that is in Fragment being added in Activity at runtime, and I’m not getting, help me !!!
Code of the Activity
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);
}
}
Code of the Fragment
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 on the console...
Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
What’s the problem? What happens?
– itscorey
I’ll edit the question and I’ll put what I tried to do so you can help me... pera
– Demetrius
To
ImageView
is in Activity or Fragment layout?– itscorey
in the layout of the Fragment
– Demetrius