每当我尝试从数据库获取数据时应用崩溃

 收藏

每当我尝试从数据库中获取数据时,myapp似乎崩溃 是否为getString,getInt,getDouble。 我试过使用getColumnIndex而不是在里面放一个数字,但是每当调用getString(),getInt(),getDouble()时,它似乎一直崩溃

该代码承认还有数据,因为a返回一个值


package com.example.werren.testapp;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.ContactsContract;
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.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class Transaksi extends AppCompatActivity {

    ListView LISTVIEW;
    ArrayList<Transaction> translist;
    Transaction transaction;
    DatabaseHelper handler;
    public static int okko;
    TextView tvtv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaksi);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        handler = new DatabaseHelper(this);
        Cursor cursor = handler.viewTrans(getIntent().getIntExtra("EXTRA_ID",0));

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        okko = getIntent().getIntExtra("EXTRA_ID",0);


        translist = new ArrayList<>();

        int a = cursor.getCount();

        Double fg = cursor.getDouble(4); //crash here

        LISTVIEW = (ListView) findViewById(R.id.LISTVIEW);

        tvtv = (TextView) findViewById(R.id.tvtv);



        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDialog();
            }
        });


    }

    public void openDialog(){
        ExampleDialog exampleDialog = new ExampleDialog();
        exampleDialog.show(getSupportFragmentManager(),"Example Dialog");
    }
}

这是获取logcat


2020-03-27 13:19:45.208 22570-22570/com.example.werren.testapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.werren.testapp, PID: 22570
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.werren.testapp/com.example.werren.testapp.Transaksi}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
        at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
        at com.example.werren.testapp.Transaksi.onCreate(Transaksi.java:50)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

回复