您好,我是 android 开发新手,正在为大学工作,创建一个可以创建(图论)图形和执行算法的应用程序,我选择使用 graphstream,因为它是一个图形库,但我有一些问题,我不能把节点放在视图上,布局在一个片段中,因为我使用两个活动一个来绘制图形,另一个来选择算法。(这只是测试图表是否出现在屏幕上,我将尝试实现按钮,以便用户创建自己的图表)。
这是我的java代码:
package com.projeto.gabriel.athenatgfinal;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.android_viewer.DefaultView;
public class FragmentGrafo extends Fragment {
@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);
BottomNavigationView bottomNavigationView = view.findViewById(R.id.bottomNavView);
bottomNavigationView.setOnNavigationItemSelectedListener(selectedListener);
return view;
}
private BottomNavigationView.OnNavigationItemSelectedListener selectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_node:
Toast.makeText(getActivity(), "Clicou em add nó", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_add_aresta:
Toast.makeText(getActivity(), "aresta aqui", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_remover:
Toast.makeText(getActivity(), "remover", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
};
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.setProperty("org.graphstream.ui.renderer","org.graphstream.ui.j2dviewer.J2DGraphRenderer");
Graph graph = new SingleGraph("tutorial 1");
graph.addNode("A" );
graph.addNode("B" );
graph.addNode("C" );
graph.addEdge("AB", "A", "B");
graph.addEdge("BC", "B", "C");
graph.addEdge("CA", "C", "A");
graph.display();
}
}
这是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<org.graphstream.ui.android_viewer.util.DefaultFragment
android:id="@+id/fragment_tag"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@+id/bottomNavView"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="0dp"
android:layout_marginTop="369dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="287dp" />
<include layout="@layout/bottom_nav_view"
android:id="@+id/bottomNavView"/>
</RelativeLayout>
这是我的bottomNavView,我正在尝试放置一个事件来单击并绘制节点和边缘并删除它们:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.BottomNavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/nav_item_color"
app:itemTextColor="@color/nav_item_color"
app:menu="@menu/menu_grafo">
</android.support.design.widget.BottomNavigationView>
这是我的错误:
--------- beginning of crash
E/AndroidRuntime:致命异常:主进程:com.projeto.gabriel.athenatgf,PID:6000 java.lang.RuntimeException:无法启动查看器。在 org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:212) 在 org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:204) 在 com.projeto.gabriel.athenatgfinal.FragmentGrafo.onCreate (FragmentGrafo.java:67) 在 android.support.v4.app.Fragment.performCreate(Fragment.java:2414) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418) 在 android.support。 v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195) 在 android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078) 在 android.support.v4.app。686)在android.view.Choreographer.doFrame(Choreographer.java:621)在android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)在android.os.Handler.handleCallback(Handler.java:751)在android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6169) 在 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) :org.graphstream.util.MissingDisplayException:未检测到 UI 包!请使用 System.setProperty("org.graphstream.ui") 选择包。在 org.graphstream。
我的问题是如何纠正这个?,这是最好的方法吗?感谢阅读,请帮助我。