当我尝试打开我的高尔夫记分牌应用程序时让力量关闭

时间:2011-11-27 02:16:54

标签: android eclipse

我是一个非常新的机器人,似乎在我的应用程序上遇到了一个Force关闭,并且不知道如何解决它。

基本上我的高尔夫应用程序的一部分是一个记分牌,表明该人所在的洞和所采取的击球量。

我的代码可以使用2个按钮,添加和减去并更改EditText中显示的数字。然后单击一个按钮并将数字提交到listview中。我现在正试图获取它,以便有2个添加,减去按钮和edittext,当两个数字都已输入并且单击提交按钮时,将显示类似'你的中风是2''在第1洞'

以下是代码

package com.uhi.myGolfApp;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;


public class scoreboard extends Activity implements OnClickListener {
Button buttonPlus1;
Button buttonMinus1;
EditText editScore1;
Button buttonPlus; 
Button buttonMinus; 
Button buttonOk; 
EditText editScore;
ListView scoreCard;
Cursor cursor;
SimpleCursorAdapter adapter;
Integer score=0;
Integer score1=0;
SharedPreferences prefs;
databaseHelper dbHelper;
SQLiteDatabase db;
Intent i;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Inflate UI from XML
    setContentView(R.layout.scoreboard);

    // Get a hang of UI components
    buttonPlus1 = (Button)findViewById(R.id.buttonadd);
    buttonMinus1 = (Button)findViewById(R.id.buttonsub);
    editScore1 = (EditText)findViewById(R.id.score1);
    buttonPlus = (Button)findViewById(R.id.add);
    buttonMinus= (Button)findViewById(R.id.subtract);
    buttonOk   = (Button)findViewById(R.id.enter);
    editScore  = (EditText)findViewById(R.id.score);
    scoreCard  = (ListView)findViewById(R.id.scorePosted);

    // Add onClick listeners
    buttonPlus1.setOnClickListener(this);
    buttonMinus1.setOnClickListener(this);
    buttonPlus.setOnClickListener(this);
    buttonMinus.setOnClickListener(this);
    buttonOk.setOnClickListener(this);

    // Get preferences
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    editScore.setText( score.toString() );
    editScore1.setText( score1.toString() );


    // Initialize the database
    dbHelper = new databaseHelper(this);
    db = dbHelper.getWritableDatabase();

    // Load the data (essentially executes a SELECT statement)
    cursor = db.query(databaseHelper.tableName, null, null, null, null, null, null);
    startManagingCursor(cursor);

    // Set the list adapter
    String[] from = {databaseHelper.colStrokes, databaseHelper.colHole};
    int[] to = {R.id.textStroke, R.id.textHole};
    adapter = new SimpleCursorAdapter(this, R.layout.golfscores, cursor, from, to);
    scoreCard.setAdapter(adapter);
}

@Override
public void onRestoreInstanceState(Bundle inState) {
    score = (inState!=null) ? inState.getInt("score",0) : 0;
    score1 = (inState!=null) ? inState.getInt("score1",0) : 0;
    editScore.setText( score.toString() );
    editScore1.setText( score1.toString() );
}
@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt("score", score);
    outState.putInt("score1", score1);
    super.onSaveInstanceState(outState);
}

@Override
public void onClick(View src) {
    switch(src.getId()) {
    case R.id.buttonadd:
        score1++;
        break;
    case R.id.buttonsub:
        score1--;
        break;
    case R.id.add:
        score++;
        break;
    case R.id.subtract:
        score--;
        break;
    case R.id.enter:
        // Save in DB
        ContentValues values = new ContentValues();
        values.put(databaseHelper.colHole, score1);
        values.put(databaseHelper.colStrokes, score);
        db.insert(databaseHelper.tableName, null, values);

        cursor = db.query(databaseHelper.tableName, null, null, null, null, null,     null);
        startManagingCursor(cursor);

        adapter.changeCursor(cursor);
        score=0;
        adapter.changeCursor(cursor);
        score1=0;
        break;
    }
    editScore.setText( score.toString() );
    editScore1.setText( score1.toString() );
}

}

和数据库

package com.uhi.myGolfApp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class databaseHelper extends SQLiteOpenHelper {
// DB constants
private static final String DB_NAME = "scoreboard.db";
private static final int DB_VERSION = 1;    // Schema version

// Schema constants
public static final String tableName  = "scoreboard";
public static final String colHole = "hole";
public static final String colStrokes = "strokes";

// Constructor
public databaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

//create the SQL table and the attributes it will hold
@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "create table "+tableName +
        " (_id integer not null primary key autoincrement, "+
            colHole + " integer, "+ colStrokes+" integer)";
    db.execSQL(sql);
}

// upgrade the database off the old version 
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

此外,我不知道这是否有任何帮助,但这出现在LogCat ='引起:java.lang.IllegalArgumentException:列'漏洞'不存在'

感谢任何帮助=]

1 个答案:

答案 0 :(得分:0)

我猜你在添加“hole”列之前运行了一次代码,这就是正在读取的数据库。您需要删除应用的数据(设置 - >应用程序 - >您的应用),以便创建最新版本的数据库。如果您没有删除数据的选项,请将其卸载。