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

时间:2017-03-22 14:25:41

标签: java android xml android-graphview

如果我在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);
    }
}

1 个答案:

答案 0 :(得分:1)

移动

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下方