你好,我是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 / Android运行时:致命异常:主要 流程: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.FragmentTransition.startTransitions(FragmentTransition.java:117) 在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408) 在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) 在android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243) 在android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654) 在android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1244) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1092) 在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622) 在android.view.View.measure(View.java:19857) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:758) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:640) 在android.view.View.measure(View.java:19857) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143) 在android.view.View.measure(View.java:19857) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:758) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:640) 在android.view.View.measure(View.java:19857) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在android.view.View.measure(View.java:19857) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:758) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:640) 在android.view.View.measure(View.java:19857) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在com.android.internal.policy.DecorView.onMeasure(DecorView.java:690) 在android.view.View.measure(View.java:19857) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6338) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:874) 在android.view.Choreographer.doCallbacks(Choreographer.java: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(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:891) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) 原因:org.graphstream.util.MissingDisplayException:未检测到UI包!请对所选包使用System.setProperty(“ org.graphstream.ui”)。 在org.graphstream.util.Display.getDefault(Display.java:55) 在org.graphstream.graph.implementations.AbstractGraph.display(AbstractGraph.java:209) ...更多58 应用已终止。
我的问题是如何纠正此问题?,这是做到这一点的最佳方法? 感谢您的阅读,请帮助我。
答案 0 :(得分:0)
您不能使用Graphstream 1.3和常规的display()方法。如果要开发Android应用程序,则必须将Graphstream 2.0与Android UI
一起使用您可以找到一些示例here来帮助您。