我在Android Studio应用程序中遇到一些问题,该问题与按“后退”按钮有关。基本上,用户输入到文本视图中,而另一种方法(不包括在内,因为它可以正常工作,但我可以在需要时发布),在API中搜索相关数据并将其显示在回收者视图中。然后,用户从“回收者”视图中进行选择,下面的方法将解析一个JSON文件,并将相关数据添加到一个单独的类“ Director”中的数组中。一旦发生这种情况,下一个活动“ GraphActivity”将开始,并在画布上为数组中的每个控制器绘制多个节点。
我遇到的问题是,当我在文本视图中输入内容,然后按后退按钮以关闭Android设备上的软键盘(它覆盖了回收站视图)并执行我刚刚提到的步骤时,当GraphActivity活动启动时,它不起作用(未绘制任何节点)。但是,如果我然后按“后退”按钮返回到第一个活动,然后再次从“回收者”视图中进行相同的选择,那么它将起作用。
据我所知,问题似乎出在将数据添加到“ Director”数组中,但我无法弄清楚是什么原因引起的,Logcat显示GraphActivity活动首次启动“ Director”数组为空,因此不会绘制任何节点,但是当我按回去并再次进行相同选择时,数组将具有正确的数据并绘制了节点。
我真的很感谢您的帮助,因为我是Android Studio的新手,并且已经搜索了数小时以尝试找到解决方案。
public class MainActivity extends AppCompatActivity implements MainAdapter.OnCompanyClickedListener {
private RecyclerView recyclerView;
private TextView userInput;
private Button search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.rView);
RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
}
//Parse JSON File
public void searchCompanyDirectors(final String companyNumber){
Thread thread = new Thread(new Runnable(){
@Override
public void run(){
HttpClient httpClient = new HttpClient();
try{
final String response = httpClient.run("myURL");
runOnUiThread(new Runnable(){
@Override
public void run(){
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("items");
for (int i = 0; i < array.length();i++){
JSONObject o = array.getJSONObject(i);
String appointment = o.getString("links");
String parsedAppointment = parseApp(appointment);
Director director = new Director(o.getString("name"),parsedAppointment);
Director.directorList.add(director);
}
}catch (JSONException e){
e.printStackTrace();
}
}
});
}
catch (Exception ex){
ex.printStackTrace();
}
}
});
thread.start();
}
@Override
public void onCompanyClicked(int position) {
Toast.makeText(this,Company.companyList.get(position).getTitle(),Toast.LENGTH_LONG).show();
chosenCompanyNumber = Company.companyList.get(position).getCompanyNumber();
chosenCompany = Company.companyList.get(position).getTitle();
searchCompanyDirectors(chosenCompanyNumber);
Intent intent = new Intent(this,GraphActivity.class);
startActivity(intent);
}
}
GraphActivity类:
public class GraphActivity extends AppCompatActivity {
DrawGraph drawGraph;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawGraph = new DrawGraph(this);
setContentView(drawGraph);
DrawGraph类:
public class DrawGraph extends View {
private Paint p1;
private Paint p2;
private Paint p3;
private Paint paint;
private Path path;
private Point point1;
private int radius = 300;
double x;
double y;
double angle;
int mWidth = this.getResources().getDisplayMetrics().widthPixels;
int mHeight = this.getResources().getDisplayMetrics().heightPixels;
List<Director> directorList = Director.getDirectorList();
private String chosenCompany = MainActivity.getChosenCompany();
private static List<Float> ordinates = new ArrayList<>();
public DrawGraph(Context context) {
super(context);
p1 = new Paint(Paint.ANTI_ALIAS_FLAG);
p1.setStrokeWidth(10);
p2 = new Paint(Paint.ANTI_ALIAS_FLAG);
p2.setStrokeWidth(10);
p3 = new Paint(Paint.ANTI_ALIAS_FLAG);
p3.setStrokeWidth(5);
path = new Path();
paint = new Paint();
point1 = new Point(mWidth/2, mHeight/2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Colors
p1.setStyle(Paint.Style.FILL);
p1.setColor(Color.RED);
p2.setStyle(Paint.Style.FILL);
p2.setColor(Color.GREEN);
p3.setStyle(Paint.Style.STROKE);
p3.setColor(Color.CYAN);
paint.setColor(Color.BLACK);
paint.setTextSize(18f);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.CENTER);
Rect bounds = new Rect();
paint.getTextBounds(chosenCompany, 0, chosenCompany.length(),bounds);
//Draw company and directors
for (int i = 1; i <= directorList.size(); i++)
{
//Divide the company node
angle = i * (360/directorList.size());
x = point1.x + radius * cos(angle);
y = point1.y + radius * sin(angle);
//Draw directors
canvas.drawCircle((float)x,(float)y - (bounds.height() / 2), bounds.width() + 5,p2);
canvas.drawText(directorList.get(i-1).getName(),(float)x,(float)y,paint);
}
答案 0 :(得分:0)
好的,我在这里提到的DrawGraph类中发现了一个问题;
List<Director> directorList = Director.getDirectorList();
private String chosenCompany = MainActivity.getChosenCompany();
您不应该直接从另一个活动访问一个数据对象。根据Android OS及其lifecycle,先前的活动可能会被破坏。您必须将其视为看不见,内存不足;这可能导致当前活动无法访问上一个活动中的数据。
您需要正确地将JSON文件中的解析数据,变量List DirectorList和String selectedCompany从MainActivity发送到GraphActivity。
您可以将数据从一个活动发送到另一个活动,方法是将数据添加到调用下一个活动的意图中。 Intent class具有可帮助您通过自身发送少量信息的功能;称为putExtra()和getExtra()。关于它们的更多信息here。
您将需要通过intent.putExtra添加这些变量数据集,并通过使用getExtras在onCreate的其他活动中将其取回。
活动A >>意图调用活动B +数据>>活动B
希望这会有所帮助。