Podczas otwierania aktywności "Historia". Pojawia mi się błąd "java.lang.NullPointerException: Attempt to invoke virtual". Może ktoś wie jak można rozwiązać ten problem?
Error:
java.lang.NullPointerException: Attempt to invoke virtual
Powód:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.example.micha.appkillan.DatabaseSQL.listOfHistory()' on a null object reference
package com.example.micha.appkillan;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.view.LayoutInflater;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import java.util.ArrayList;
public class History extends ActionBarActivity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new VizAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_history, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"History Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.example.micha.appkillan/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"History Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.example.micha.appkillan/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
class SingleRow{
String time;
String date;
int images;
SingleRow(String time)
{
this.time = time;
// this.date = date;
}
}
class VizAdapter extends BaseAdapter{
private ArrayList<SingleRow> list;
private Cursor row;
private DatabaseSQL db;
private Context context;
VizAdapter(Context c){
context = c;
list = new ArrayList<SingleRow>();
row = db.listOfHistory();
int id[] = new int[row.getCount()];
//System.out.print(row);
if(row.getCount() > 0)
{
row.moveToFirst();
do {
String times = row.getString(row.getColumnIndex("time"));
// String dates = row.getString(row.getColumnIndex("date"));
list.add(new SingleRow(times));
}
while(row.moveToNext());
row.close();
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowList = inflater.inflate(R.layout.history_single_row,parent,false);
TextView time = (TextView) rowList.findViewById(R.id.textView);
SingleRow temp = list.get(position);
time.setText(temp.time);
return rowList;
}
}
DatabaseSQL wygląda następująco:
Może coś przeoczyłem, że ten db jest jednak nullem?
db.execSQL("CREATE TABLE SUBRUNS ( " +
"idSUBRUN INTEGER PRIMARY KEY AUTOINCREMENT, " +
"latitude DOUBLE, " +
"longitude DOUBLE, " +
"time TEXT, " +
"date TEXT, " +
"high DOUBLE " +
"idRUN INT," +
"FOREIGN KEY(idRUN) REFERENCES RUN(idRUN)" +
");"
);
...
db.execSQL("INSERT INTO SUBRUNS (" +
"idSUBRUN,latitude, longitude, time, date, high" +
")" +
"VALUES (" +
"1, 20,30,'10:20','01.09.2010', 20" +
");");
...
public Cursor listOfHistory()
{ SQLiteDatabase db = this.getReadableDatabase();
Cursor row = db.rawQuery("SELECT * from SUBRUNS",null);
return row;
}