gpt4 book ai didi

java - 如果我在 onCreate 方法中添加 GraphViewer 代码,应用程序会崩溃

转载 作者:行者123 更新时间:2023-12-02 13:36:42 24 4
gpt4 key购买 nike

如果我在 onCreate 方法之外添加 GraphViewer Java 代码,我会收到消息:无法解析符号 addSeries。但是当我将代码放入 onCreate 方法中时,应用程序在启动屏幕后崩溃。请帮忙(代码在**之间突出显示):

package yourhome;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;


import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;


import smssca.stefandraegersoftware.de.simplemysqlconnectionapp.datasource.ActivityDataSource;

public class Hauptfenster extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
**GraphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3)
});
graph.addSeries(series);**
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hauptfenster);

final TextView textView = (TextView) findViewById(R.id.textView);

Button lesenBtn = (Button) findViewById(R.id.lesenBtn);
lesenBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ActivityDataSource(textView).execute("someParams");
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_hauptfenster, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) { return true;}
return super.onOptionsItemSelected(item);
}
}

最佳答案

移动

GraphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3)
});
graph.addSeries(series);

下面

    super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hauptfenster);

大多数情况下,这两行应该首先在 onCreate 方法中调用

当前您正在尝试findViewById(R.id.graph);,此时根本没有View - 这些是在中设置的>setContentView 下面

关于java - 如果我在 onCreate 方法中添加 GraphViewer 代码,应用程序会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42954464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com