Error: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

Error: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

我正在制作一个在 MainActiviy 中显示折线图的应用程序。顶部按钮,指向另一个 activity。当我制作图表时,第二个 activity 只包含一个 TextView,应用程序是 运行.

之后,我想实现一个系统,用户可以在其中添加数据,这些数据将保存在另一个 activity 的 SQLite 数据库中。我遵循了 thenewboston 的教程 49 到 54。

我(相信)我做了和教程中一样的事情。然而,我的应用程序不是 运行,它给出了这个错误:

FATAL EXCEPTION: main. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference.

完整的错误可以在下面找到。有人可以帮我解决这个错误吗?

错误信息

FATAL EXCEPTION: main Process: com.example.shanna.linechartgenerator, PID: 27077 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.shanna.linechartgenerator/com.example.shanna.linechartgenerator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2216) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) at android.app.ActivityThread.access0(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5272) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:68) at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:190) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184) at com.example.shanna.linechartgenerator.MainActivity.(MainActivity.java:18) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1572) at android.app.Instrumentation.newActivity(Instrumentation.java:1065) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2206) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)  at android.app.ActivityThread.access0(ActivityThread.java:148)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5272)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

MainActivity.Java:

package com.example.shanna.linechartgenerator;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    //public static final int ThresholdValue = 1;
    LineChart lineChart = (LineChart) findViewById(R.id.chart);
    //float newThreshold;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // creating list of entry
        ArrayList<Entry> entries = new ArrayList<>();
        entries.add(new Entry(4f, 0));
        entries.add(new Entry(8f, 1));
        entries.add(new Entry(6f, 2));
        entries.add(new Entry(2f, 3));
        entries.add(new Entry(18f, 4));
        entries.add(new Entry(9f, 5));

        LineDataSet dataset = new LineDataSet(entries, "# of Calls");

        // creating labels
        ArrayList<String> labels = new ArrayList<String>();
        labels.add("January");
        labels.add("February");
        labels.add("March");
        labels.add("April");
        labels.add("May");
        labels.add("June");

        LineData data = new LineData(labels, dataset);
        lineChart.setData(data); // set the data and list of lables into chart
    }

    public void goToActivityOne(View view){
        Intent intent = new Intent(this,ActivityOne.class);
        startActivity(intent); //go to other activity in app
    }

    public void goToOtherApp(View view){
        Intent intent2 = new Intent("HelloWorld_MakeValue");
        //intent2.putExtra("newThreshold", newThreshold);

        startActivity(intent2); //To go to activity in the other app
        //startActivityForResult(intent2, ThresholdValue);
    }
}

ActivityOne.java(将Activity与输入保存在数据库中)

package com.example.shanna.linechartgenerator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class ActivityOne extends AppCompatActivity {

    private EditText userInput;
    private TextView userText;
    private MyDBHandler dbHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_layout);

        userInput = (EditText) findViewById(R.id.userInput);
        userText = (TextView) findViewById(R.id.userText);
        dbHandler = new MyDBHandler(this, null, null, 1); //has 4 paramethers: context, DATABASE_NAME, factory, DATABASE_VERSION, see class

        //method for add button, see below
        printDataBase();

        //method for the delete button, see below

        //method for printing, see below
    }

    public void addButtonClicked(View view){
        Painscales painscales = new Painscales(userInput.getText().toString());
        dbHandler.addPainscale(painscales);
        printDataBase();
        //whenever user clicks the add button, take the input, add it to the database and print in below
    }

    public void printDataBase(){
        String dbString = dbHandler.databaseToSting(); //get sting we retreived
        userText.setText(dbString); //store it in here
        userInput.setText(" "); //take input and set text to 'refresh' the input
    }

    public void deleteButtonClicked(View view){
        String inputText = userInput.getText().toString();
        dbHandler.deletePainscale(inputText);
        printDataBase();

    }


}

MyDBHandlerclass

package com.example.shanna.linechartgenerator;

/**
 * Created by Shanna on 1-6-2016.
 * clas to work with the database
 * with the help of the youtube tutorial, "Android App Development for Beginners - 51, 52 & 53 " from thenewboston
 */
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;

public class MyDBHandler extends SQLiteOpenHelper{
    //name that database
    //column name etx
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "painscale.db";
    public static final String TABLE_PAINSCALE = "painscale";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PAINSCALENAME = "painscaleName";

    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    } //constructor to pass info to class that works direclu with android (myDGHandler)

    //on first call it needs to know what you are going to do, which is creating a table
    @Override
    public void onCreate(SQLiteDatabase db) {
        //specify the table
        String query = "CREATE_TABLE" + TABLE_PAINSCALE + "(" +
                COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT" +
                COLUMN_PAINSCALENAME + "TEXT" + ")";
        db.execSQL(query); //create the table

    }

    //to update the table
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_PAINSCALE); //delete the current table
        onCreate(db); //create new updated table
    }

    //Add a new row to the database
    public void addPainscale(Painscales painscales){
        ContentValues values = new ContentValues();
        values.put(COLUMN_PAINSCALENAME, painscales.get_painscaleName());
        SQLiteDatabase db = getWritableDatabase(); //key to the
        db.insert(TABLE_PAINSCALE, null, values); //inset new row into table
        db.close();//close database, we are done
    }

    //Delete painscale from the database
    public void deletePainscale(String painscaleName){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM" + TABLE_PAINSCALE + "WHERE" + COLUMN_PAINSCALENAME + "=\"" +painscaleName + "=\";" ); //delete where productname is same as the input
    }

    //print out the database, as a string
    public String databaseToSting(){
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT + FROM " + TABLE_PAINSCALE + "WHERE 1";

        //cursor point at location in results
        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();

        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("painscaleName"))!= null){
                dbString += c.getString(c.getColumnIndex("painscaleName"));
                dbString += "\n";
            } //loops throuhg all painsclase names, everytime it does, it would place a new one on a new row. Is needed so thevalesarenotwritenlikethis
        }
        db.close();
        return dbString;
    }
}

Painscales.java class

package com.example.shanna.linechartgenerator;

/**
 * Created by Shanna on 1-6-2016.
 * class to deal with the user input
 * with the help of the youtube tutorial, "Android App Development for Beginners - 49 and 50, from thenewboston
 */
public class Painscales {
    //java needs an id number and the pain scale
    private int _id;
    private String _painscaleName;

    public Painscales(){

    }

    public Painscales(String painscaleName) {
        this._painscaleName = painscaleName; //give input data automatically string to whatever user typed in
    }

    public void set_id(int _id) {
        this._id = _id; //give input an id
    }

    public void set_painscaleName(String _painscaleName) {
        this._painscaleName = _painscaleName; //set input as a name
    }

    public int get_id() {
        return _id; //get input id
    }

    public String get_painscaleName() {
        return _painscaleName; //get input name
    }
}

您不能在此处进行此初始化:

LineChart lineChart = (LineChart) findViewById(R.id.chart);

您可以在那里声明您的变量,例如: 折线图折线图; 然后在 onCreate AFTER 调用 setContentView 中将初始化设置为:

lineChart = (LineChart) findViewById(R.id.chart);