2
I’m doing a program that creates graphs, that contains a page with two tab Fragments one for the graph and another for the algorithms I’m trying to make when a button is clicked on the algorithms Fragment send a request to the graph where the algorithm is executed and returns a string that I placed on the screen of the Fragment algorithms, however if I try to use any data from Fragment graph this returned Null Pointer Exception.
this is Tab:
public class Tab extends AppCompatActivity implements DialogAddAresta.ExampleDialogListner,
DialogDeletar.DeletarElementos, FragmentAlgoritmo.Communicator{
private FragmentGrafo fragmentGrafo = new FragmentGrafo();
private FragmentAlgoritmo fragmentAlgoritmo = new FragmentAlgoritmo();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
TabLayout tabLayout = findViewById(R.id.tablayout_id);
ViewPager viewPager = findViewById(R.id.viewpager_id);
ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager());
adapter.AddFragment(new FragmentGrafo(), "Grafo");
adapter.AddFragment(new FragmentAlgoritmo(), "Algoritmo");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void applyNodes(String selectedNodeTo, String selectedNodeFrom, String peso) {
}
@Override
public void applyDeletar(String deletaNo,String deltaAresta, boolean controle) {
}
@Override
public void onEvent(Event e) {
if (e.getType() == Event.Type.CLICK){
fragmentGrafo.primeiroNo();
} else {
String data = e.getString();
fragmentAlgoritmo.colocaElem(data);
}
}
}
this is the fragment:
public class FragmentGrafo extends Fragment implements DialogAddAresta.ExampleDialogListner, DialogDeletar.DeletarElementos {
private static final int CONTENT_VIEW_ID = 10101010;
private DefaultFragment fragment;
private Graph graph;
private String[] criaNos = new String[]{"A","B","C","D","E","F","G","H","I","J","K",
"L","M","N","O","P","Q","R","S","T","U","V","X","W","Y","Z"};
private int pos = 0;
private float x = 0.0f;
private float y = 0.0f;
private String styleSheet = "graph { fill-color: #EEE; }" +
"node { size: 100px; text-size: 30px; fill-color: white; stroke-mode: plain;}" +
"node:clicked {fill-color: blue;}"+
"edge { size: 5; text-size: 30px; padding: 0, 12;}";
List<String> nos = new ArrayList<>();
private List<String> arestas = new ArrayList<>();
String[] st = new String[]{"texte","macaco"};
FragmentAlgoritmo fragmentAlgoritmo = new FragmentAlgoritmo();
FragmentAlgoritmo.Communicator listner;
public FragmentGrafo(){
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.grafo_fragment, container, false);
return view;
}
@Override
public void onCreate(@Nullable android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
graph = new MultiGraph("AthenaGraph");
graph.setAttribute( "ui.stylesheet", styleSheet);
graph.setAttribute("ui.antialias");
display(savedInstanceState, graph, false);
}
public void display(Bundle savedInstanceState, Graph graph, boolean autoLayout) {
if (savedInstanceState == null) {
FragmentManager fm = getActivity().getFragmentManager();
fragment = (DefaultFragment) fm.findFragmentByTag("fragment_tag");
if (null == fragment) {
fragment = new DefaultFragment();
fragment.init(graph, autoLayout);
}
FragmentTransaction ft = fm.beginTransaction() ;
ft.add(R.id.layoutFragment, fragment).commit();
}
}
public void openDialogAddAresta() {
DialogAddAresta dialogAddAresta = new DialogAddAresta();
Bundle bundle = new Bundle();
bundle.putStringArrayList("list", (ArrayList<String>)nos);
dialogAddAresta.setArguments(bundle);
dialogAddAresta.setTargetFragment(FragmentGrafo.this, 1);
dialogAddAresta.show(getActivity().getSupportFragmentManager(), "Add Aresta");
}
public void openDialogDeletar(){
DialogDeletar dialogDeletar = new DialogDeletar();
Bundle bundle = new Bundle();
bundle.putStringArrayList("listArestas", (ArrayList<String>)arestas);
bundle.putStringArrayList("listNos", (ArrayList<String>)nos);
dialogDeletar.setArguments(bundle);
dialogDeletar.setTargetFragment(FragmentGrafo.this, 1);
dialogDeletar.show(getActivity().getSupportFragmentManager(), "Deletar");
}
public void primeiroNo(){
String s = graph.getNode(0).getId();
listner.onEvent(new Event(s , Event.Type.DISPATCH));
}
public String pegaNos(){
StringBuilder string= new StringBuilder();
for (Node n : graph){
string.append(n);
}
return string.toString();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentAlgoritmo.Communicator) {
try {
listner = (FragmentAlgoritmo.Communicator) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "deve implementar Algoritmos");
}
}
}
}
this is the fragment:
public class FragmentAlgoritmo extends Fragment {
private Spinner spAlgo;
private ImageButton btnRun, btnExecutar;
private ImageView imageView;
private TextView textView;
private String testo;
boolean run = true;
private List<String> list;
private Communicator listner;
public FragmentAlgoritmo() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.algoritmo_fragment, container, false);
spAlgo = view.findViewById(R.id.spAlgo);
btnExecutar = view.findViewById(R.id.btn_executar);
btnRun = view.findViewById(R.id.btn_run);
imageView = view.findViewById(R.id.imageView);
textView = view.findViewById(R.id.tvLinha1);
ArrayAdapter<String> listAlgo = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.algoritmos) );
listAlgo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spAlgo.setAdapter(listAlgo);
btnRun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (run){
btnRun.setImageResource(R.drawable.ic_run_slow_white_24dp);
Toast.makeText(getContext(), "Velocidade reduzida", Toast.LENGTH_SHORT).show();
run = false;
} else {
btnRun.setImageResource(R.drawable.ic_run_fast_white_24dp);
Toast.makeText(getContext(), "Velocidade normal", Toast.LENGTH_SHORT).show();
run = true;
}
}
});
btnExecutar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (testo){
case "***":
Toast.makeText(getContext(), R.string.selecione_func, Toast.LENGTH_SHORT).show();
break;
case "Dijkstra":
listner.onEvent(new Event(null, Event.Type.CLICK));
break;
case "Coloração":
Toast.makeText(getContext(), "Coloração", Toast.LENGTH_SHORT).show();
break;
}
}
});
spAlgo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
testo = spAlgo.getItemAtPosition(position).toString();
switch (testo){
case "***":
imageView.setVisibility(View.GONE);
textView.setText(R.string.selecione_func);
break;
case "Dijkstra":
imageView.setImageResource(R.drawable.dijkstra_alog);
imageView.setVisibility(View.VISIBLE);
textView.setText("Algo Aqui");
break;
case "Coloração":
imageView.setImageResource(R.drawable.under_construction);
imageView.setVisibility(View.VISIBLE);
textView.setText("Ainda sobre construção");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
public void colocaElem(String s){
textView.setText(s);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Communicator) {
try {
listner = (Communicator) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "deve implementar Algoritmos");
}
}
}
@Override
public void onDetach() {
super.onDetach();
listner = null;
}
public interface Communicator {
void onEvent(Event e);
}
}
this is the Event
public class Event {
public String string;
public Type type;
public Event( String string, Type type) {
this.string = string;
this.type = type;
}
enum Type { CLICK, DISPATCH }
public String getString() {
return string;
}
public Type getType() {
return type;
}
public void setString(String string) {
this.string = string;
}
}
java.lang.Nullpointerexception: Attempt to invoke interface method 'org.graphstream.Graph.Node org.graphstream.Graph.Graph.getNode(int)' on a null Object Reference Fragmentgraph.primeirono(Fragmentgraph.java:202) Tab.onEvent(Tab.java:43) at com.projecto.Abriel.athenatgfinal.Fragmentalgoritmo$2.onClick(Fragmentalgoritmo.java:84) at android.view.View.performClick(View.java:5637) at android.view.View$Performclick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.Activitythread.main(Activitythread.java:6169) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:891) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:781)
this method I’m doing this right? have any easier? please help me.
Always try to place the error stack so that we can analyze it better to help you.
– Filipe L. Constante
put the error log, I can get from Fragment to the parent and call the other Fragment but ai da null nos methods, if I pass a string determined within the method works.
– Gabriel Ricardo