Android:输入db的新行

时间:2014-11-15 11:29:39

标签: android

我正在制作支票簿申请时遇到一些奇怪的问题。

问题是当我通过在CashBalancTable中创建另一行(TABLE_CB)来重置现金值

即使记录成功,CashSetting活动也会失效。(发出错误

消息并返回上一个活动)。 "不幸的是,钱已经停止了#34; ==>错误弹出窗口

因此,我无法在CashSetting的最后一个中激活startActivity行。你能告诉

吗? 我有什么问题?感谢。

CashBalanceTable类仅用于移动数据。

**//CashBalanceTable class**
public class CashBalanceTable {

    private int id;
    private int cash;

    public CashBalanceTable() {}
    public CashBalanceTable(int id, int cash)
    {
        this.id = id;
        this.cash = cash;
    }
    public CashBalanceTable(int cash)
    {
        this.cash = cash;
    }

    public void setId(int id)
    {
        this.id = id;
    }


    public void setCash(int cash)
    {
        this.cash = cash;
    }

    public int getId()
    {
        return id;
    }

    public int getCash()
    {
        return cash;
    }
}


**//CashSetting.java**

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class CashSetting extends ActionBarActivity {

    Button  enterButton;
    EditText editText1;
    DatabaseHelper dbHelper;
    TextView textView3;

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

        enterButton = (Button)findViewById(R.id.enterButton);
        editText1 = (EditText)findViewById(R.id.editText1);     
        textView3 = (TextView)findViewById(R.id.textView3);

    }

    public void initializingCash(View view)
    {
        dbHelper = new DatabaseHelper(this);        
        if(editText1.getText().toString() == null)
            return;

        int cash = Integer.parseInt(editText1.getText().toString());    

        CashBalanceTable cb = new CashBalanceTable(cash);
        long id = dbHelper.insertCash(cb);

        if(id == -1)
        {
            Message.message(this, "Unsuccessful");
        }
        else
        {
            Message.message(this, id + " row created");
        }

        editText1.setText(null);
        textView3.setText("Your cash balance set to " + cash + " won");

        startActivity(new Intent(CashSetting.this, SettingActivity.class));
    }
}








**//This is insertCash method in DatabaseHelper class**
public long insertCash(CashBalanceTable cb) //done
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_CASH, cb.getCash());

    //insert a row
    long id = db.insert(TABLE_CB, null, values);    
    return id;
}

仅供您参考,下面是logcat

11-15 11:01:05.641: E/AndroidRuntime(2069): FATAL EXCEPTION: main
11-15 11:01:05.641: E/AndroidRuntime(2069): Process: com.example.money, PID: 2069
11-15 11:01:05.641: E/AndroidRuntime(2069): java.lang.IllegalStateException: Could not execute method of the activity
11-15 11:01:05.641: E/AndroidRuntime(2069): at android.view.View$1.onClick(View.java:3823)
11-15 11:01:05.641: E/AndroidRuntime(2069): at android.view.View.performClick(View.java:4438)
11-15 11:01:05.641: E/AndroidRuntime(2069): at android.view.View$PerformClick.run(View.java:18422)
11-15 11:01:05.641: E/AndroidRuntime(2069): at android.os.Handler.handleCallback(Handler.java:733)
11-15 11:01:05.641: E/AndroidRuntime(2069): at android.os.Handler.dispatchMessage(Handler.java:95)
11-15 11:01:05.641: E/AndroidRuntime(2069): at android.os.Looper.loop(Looper.java:136)
11-15 11:01:05.641: E/AndroidRuntime(2069): at android.app.ActivityThread.main(ActivityThread.java:5001)
11-15 11:01:05.641: E/AndroidRuntime(2069): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:01:05.641: E/AndroidRuntime(2069): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 11:01:05.641: E/AndroidRuntime(2069): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-15 11:01:05.641: E/AndroidRuntime(2069): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-15 11:01:05.641: E/AndroidRuntime(2069): at dalvik.system.NativeStart.main(Native Method)
11-15 11:01:05.641: E/AndroidRuntime(2069): Caused by: java.lang.reflect.InvocationTargetException
11-15 11:01:05.641: E/AndroidRuntime(2069): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:01:05.641: E/AndroidRuntime(2069): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 11:01:05.641: E/AndroidRuntime(2069): at android.view.View$1.onClick(View.java:3818)
11-15 11:01:05.641: E/AndroidRuntime(2069): ... 11 more
11-15 11:01:05.641: E/AndroidRuntime(2069): Caused by: java.lang.NullPointerException
11-15 11:01:05.641: E/AndroidRuntime(2069): at com.example.money.CashSetting.initializingCash(CashSetting.java:53)
11-15 11:01:05.641: E/AndroidRuntime(2069): ... 14 more
11-15 11:01:12.993: W/EGL_genymotion(2097): eglSurfaceAttrib not implemented
11-15 11:22:56.137: W/EGL_genymotion(2097): eglSurfaceAttrib not implemented
11-15 11:22:57.605: W/EGL_genymotion(2097): eglSurfaceAttrib not implemented
11-15 11:23:48.437: W/EGL_genymotion(2097): eglSurfaceAttrib not implemented
11-15 11:23:48.721: W/EGL_genymotion(2097): eglSurfaceAttrib not implemented
11-15 11:24:04.189: D/dalvikvm(2097): GC_FOR_ALLOC freed 158K, 6% free 3702K/3916K, paused 8ms, total 14ms
11-15 11:24:04.325: W/EGL_genymotion(2097): eglSurfaceAttrib not implemented
11-15 11:24:06.821: W/EGL_genymotion(2097): eglSurfaceAttrib not implemented
11-15 11:24:09.073: W/EGL_genymotion(2097): eglSurfaceAttrib not implemented
11-15 11:24:12.073: D/AndroidRuntime(2097): Shutting down VM
11-15 11:24:12.073: W/dalvikvm(2097): threadid=1: thread exiting with uncaught exception (group=0xa4d6cb20)
11-15 11:24:12.077: E/AndroidRuntime(2097): FATAL EXCEPTION: main
11-15 11:24:12.077: E/AndroidRuntime(2097): Process: com.example.money, PID: 2097
11-15 11:24:12.077: E/AndroidRuntime(2097): java.lang.IllegalStateException: Could not execute method of the activity
11-15 11:24:12.077: E/AndroidRuntime(2097): at android.view.View$1.onClick(View.java:3823)
11-15 11:24:12.077: E/AndroidRuntime(2097): at android.view.View.performClick(View.java:4438)
11-15 11:24:12.077: E/AndroidRuntime(2097): at android.view.View$PerformClick.run(View.java:18422)
11-15 11:24:12.077: E/AndroidRuntime(2097): at android.os.Handler.handleCallback(Handler.java:733)
11-15 11:24:12.077: E/AndroidRuntime(2097): at android.os.Handler.dispatchMessage(Handler.java:95)
11-15 11:24:12.077: E/AndroidRuntime(2097): at android.os.Looper.loop(Looper.java:136)
11-15 11:24:12.077: E/AndroidRuntime(2097): at android.app.ActivityThread.main(ActivityThread.java:5001)
11-15 11:24:12.077: E/AndroidRuntime(2097): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:24:12.077: E/AndroidRuntime(2097): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 11:24:12.077: E/AndroidRuntime(2097): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-15 11:24:12.077: E/AndroidRuntime(2097): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-15 11:24:12.077: E/AndroidRuntime(2097): at dalvik.system.NativeStart.main(Native Method)
11-15 11:24:12.077: E/AndroidRuntime(2097): Caused by: java.lang.reflect.InvocationTargetException
11-15 11:24:12.077: E/AndroidRuntime(2097): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:24:12.077: E/AndroidRuntime(2097): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 11:24:12.077: E/AndroidRuntime(2097): at android.view.View$1.onClick(View.java:3818)
11-15 11:24:12.077: E/AndroidRuntime(2097): ... 11 more
11-15 11:24:12.077: E/AndroidRuntime(2097): Caused by: java.lang.NullPointerException
11-15 11:24:12.077: E/AndroidRuntime(2097): at com.example.money.CashSetting.initializingCash(CashSetting.java:53)
11-15 11:24:12.077: E/AndroidRuntime(2097): ... 14 more

1 个答案:

答案 0 :(得分:0)

问题似乎与

有关
public void initializingCash(View view)

logCat表示你试图在OnClick方法中调用此方法(代码中的某个地方)

我认为当你尝试在startActivity中使用CashSettings.this时问题就开始了,引用可能是NULL,因为你在OnClick方法中(CashSettings活动第53行的NullPointerException)

尝试在onCreate中设置CashSettings变量,添加此

CashSettings csActivity;

在这些行下

Button  enterButton;
EditText editText1;
DatabaseHelper dbHelper;
TextView textView3;

然后在onCreate中添加此行

csActivity = this;

然后使用此

启动意图
startActivity(new Intent(csActivity, SettingActivity.class));

如果发生了变化,请告诉我们。)