如果我在onCreate方法之外添加GraphViewer Java代码,则会收到消息:Cannot resolve symbol 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

07-25 21:27