java.lang.IllegalStateException:无法执行活动的方法

时间:2014-12-10 06:23:02

标签: android

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class PantallaProductos extends Activity{
private Button btn1,btn2,btn3,btn4;
private double total=0.00;
AdminBD admin;
SQLiteDatabase bd;

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

    btn1 = (Button) findViewById(R.id.cuaderno);
    btn2 = (Button) findViewById(R.id.lapicero);
    btn3 = (Button) findViewById(R.id.borrador);
    btn4 = (Button) findViewById(R.id.corrector);
    admin = new AdminBD(this, "BDHHL", null, 1);
    bd = admin.getWritableDatabase();


}


public void lanzarCuaderno(View v) {
    String precio="";


    String nombreBoton = btn1.getText().toString();
    Cursor fila = bd.rawQuery(
            "SELECT precio from Producto WHERE nombre='" + nombreBoton + "'", null);
    if( fila != null && fila.moveToFirst() ){
        precio = fila.getString(0);
        fila.close(); 
    }else
        Toast.makeText(this, "No existe un producto con ese nombre",       Toast.LENGTH_SHORT).show();

    bd.close();

    Bundle b = new Bundle();
    b.putDouble("PRECIO", Double.valueOf(precio));


    Intent i = new Intent(this,PantallaCantidad.class);
    i.putExtras(b);
    startActivityForResult(i, 1);



}

logcat给我:java.lang.IllegalStateException:当触发方法“lanzarCuaderno”时,无法执行活动的方法。有谁能告诉我如何解决这个问题?请。

这显示了logcat:

12-10 01:05:00.414: E/AndroidRuntime(1542): FATAL EXCEPTION: main
12-10 01:05:00.414: E/AndroidRuntime(1542): Process: com.empresa.sistventas, PID: 1542
12-10 01:05:00.414: E/AndroidRuntime(1542): java.lang.IllegalStateException: Could not execute method of the activity
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.view.View$1.onClick(View.java:4007)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.view.View.performClick(View.java:4756)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.view.View$PerformClick.run(View.java:19749)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.os.Handler.handleCallback(Handler.java:739)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.os.Looper.loop(Looper.java:135)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.app.ActivityThread.main(ActivityThread.java:5221)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at java.lang.reflect.Method.invoke(Native Method)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at java.lang.reflect.Method.invoke(Method.java:372)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-10 01:05:00.414: E/AndroidRuntime(1542): Caused by: java.lang.reflect.InvocationTargetException
12-10 01:05:00.414: E/AndroidRuntime(1542):     at java.lang.reflect.Method.invoke(Native Method)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at java.lang.reflect.Method.invoke(Method.java:372)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.view.View$1.onClick(View.java:4002)
12-10 01:05:00.414: E/AndroidRuntime(1542):     ... 10 more
12-10 01:05:00.414: E/AndroidRuntime(1542): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
12-10 01:05:00.414: E/AndroidRuntime(1542):     at com.empresa.sistventas.PantallaProductos.lanzarCuaderno(PantallaProductos.java:47)
12-10 01:05:00.414: E/AndroidRuntime(1542):     ... 13 more

1 个答案:

答案 0 :(得分:0)

您正尝试从光标

访问(-1)st列
precio=fila.getString(-1);

检查这是否是您想要做的。传递-1是异常

  

android.database.CursorIndexOutOfBoundsException:索引-1请求,   大小为0