2017-03-27 55 views
-2

美好的一天Android Studio錯誤的第一個參數類型。找到'java.lang.String',必填'int'

我有搜索網站,但沒有得到任何東西來幫助我解決我的問題。 我是一個android的新手,並開始了一個我無法完成的項目。使用過Android Studio 2.3 應用程序的名稱「匹配追蹤」

確定這裏

它走的 例如我的應用程序允許您跟蹤匹配「橄欖球」我現在想應用到自動點添加到領先榜如果一個球隊有4次或更多的嘗試,那就是我卡住的地方!

我FactDBAdaptor.class

在我RugbyActivity.class EDITED *******

package za.co.sanrl.rugbyleague.database; 


import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.util.Log; 

import za.co.sanrl.rugbyleague.main.MatchEvent; 

import java.util.ArrayList; 

/** 
* Class that allows interaction with the Facts database such as insert, delete, update. 
* 
* @author Morne van Rooyen 
*/ 
public class FactsDbAdapter { 

    public static final String KEY_MATCHID = "match_id"; 
    public static final String KEY_TYPE  = "type"; 
    public static final String KEY_PLAYER1 = "player1"; 
    public static final String KEY_PLAYER2 = "player2"; 
    public static final String KEY_TEAM  = "team"; 
    public static final String KEY_TIME  = "time"; 
    private static final String DB_TABLE = "Facts"; 
    private SQLiteDatabase db; 
    private BaseHelper dbHelper; 
    private Context context; 

    public FactsDbAdapter(Context context) { 
     this.context = context; 
    } 

    /** 
    * Open database so we can write to it. 
    * 
    * @return Open DB connection 
    * @throws SQLException 
    */ 
    public FactsDbAdapter open() throws SQLException { 
     dbHelper = new BaseHelper(context); 
     db = dbHelper.getWritableDatabase(); 
     return this; 
    } 

    /** 
    * Close database. 
    */ 
    public void close() { 
     dbHelper.close(); 
    } 


    /** 
    * Add a match fact to the database. 
    * 
    * @param match_id Match ID 
    * @param type  Goal, penalty etc. 
    * @param player1 First player involved in event 
    * @param player2 Second player involved in event 
    * @param team  The player(s) team 
    * @param time  The time event took place 
    */ 
    public void addMatchFact(int match_id, String type, String player1, String player2, String team, String time) { 

     ContentValues values = new ContentValues(); 
     values.put(KEY_MATCHID, match_id); 
     values.put(KEY_TYPE, type); 
     values.put(KEY_PLAYER1, player1); 
     values.put(KEY_PLAYER2, player2); 
     values.put(KEY_TEAM, team); 
     values.put(KEY_TIME, time); 

     //Insert into database. 
     try { 
      db.insert(DB_TABLE, null, values); 
     } catch (Exception e) { 
      Log.e("Database error when inserting match fact", e.toString()); 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Get the column values for a specific column provided. 
    * 
    * @param column  - column you want to query. 
    * @param duplicates - if you want duplicate results. 
    * @return columnlist 
    */ 
    public ArrayList<String> getColumnValues(String column, boolean duplicates) { 
     Cursor cursor     = null; 
     ArrayList<String> columnList = new ArrayList<>(); 

     //Get column values. 
     try { 
      if (duplicates) { 
       cursor = db.rawQuery("SELECT " + column + " FROM " + DB_TABLE, null); 
      } else { 
       cursor = db.rawQuery("SELECT DISTINCT " + column + " FROM " + DB_TABLE, null); 
      } 
     } catch (Exception e) { 
      Log.e("Database error selecting facts", e.toString()); 
      e.printStackTrace(); 
     } 

     //Query result is not empty. 
     if (cursor != null && cursor.moveToFirst()) { 
      int columnIndex = cursor.getColumnIndex(column); 

      do { 
       columnList.add(cursor.getString(columnIndex)); 
      } while (cursor.moveToNext()); 
     } 

     return columnList; 
    } 

    /** 
    * Count number of types with a given match_id. 
    * 
    * @param match_id Match ID 
    * @param type  Event Type 
    * @param teamName Team Name 
    * @return count of types 
    */ 
    public int countTypesWithMatchID(int match_id, String type, String teamName) { 
     Cursor cursor     = null; 
     ArrayList<String> columnList = new ArrayList<>(); 

     //Get column values. 
     try { 
      cursor = db.rawQuery("SELECT " + KEY_TYPE + " FROM " + DB_TABLE + 
        " WHERE " + KEY_TYPE + "=? AND " + KEY_MATCHID + "=" + match_id + 
        " AND " + KEY_TEAM + "=?", new String[]{type, teamName}); 
     } catch (Exception e) { 
      Log.e("Database error counting match events", e.toString()); 
      e.printStackTrace(); 
     } 

     //Query result is not empty. 
     if (cursor != null && cursor.moveToFirst()) { 
      int typeIndex = cursor.getColumnIndex(KEY_TYPE); 

      do { 
       columnList.add(cursor.getString(typeIndex)); 
      } while (cursor.moveToNext()); 
     } 

     return columnList.size(); 
    } 
/**Count 4 or more tries in a match begins**/ 
    /** 
    * Count number of types with a given match_id. 
    * 
    * @param match_id Match ID 
    * @param type  type is Tries 
    * @param teamName Team Name 
    * @return count of types 
    */ 
    public int countTriesWithMatchID(int match_id, String type, String teamName) { 
     Cursor cursor = null; 
     int result  = 0; 

     //Get column values. 
     try { 
      cursor = db.rawQuery("SELECT COUNT(" + KEY_TYPE + ") FROM " + DB_TABLE + 
        " WHERE " + KEY_TYPE + "=Try AND " + KEY_MATCHID + "=" + match_id + 
        " AND " + KEY_TEAM + "=?", new String[]{type, teamName}); 
     } catch (Exception e) { 
      Log.e("Database error counting match events", e.toString()); 
      e.printStackTrace(); 
     } 

     //Query result is not empty. 
     if (cursor != null && cursor.moveToFirst()) { 
      result = cursor.getInt(0); 
     } 

     return result; 
    } 

/**Count 4 or more tries in a match ends**/ 


    /** 
    * Count the number a certain type with a certain player name comes up in db. 
    * 
    * @param type Event Type 
    * @param player Player involved in event 
    * @return count of types involving a specific player 
    */ 
    public int countTypesWithPlayer(String type, String player) { 
     Cursor cursor = null; 
     int result  = 0; 

     //Get column values. 
     try { 
      cursor = db.rawQuery("SELECT COUNT(" + KEY_TYPE + ") FROM " + DB_TABLE + 
          " WHERE " + KEY_TYPE + "=? AND " + KEY_PLAYER1 + "=?", new String[]{type, player}); 
     } catch (Exception e) { 
      Log.e("Db err counting type of events involving a player", e.toString()); 
      e.printStackTrace(); 
     } 

     //Query result is not empty. 
     if (cursor != null && cursor.moveToFirst()) { 
      result = cursor.getInt(0); 
     } 

     return result; 
    } 

    /** 
    * Get all match facts for a given match id. 
    * 
    * @param match_id Match ID 
    * @return All match facts 
    */ 
    public ArrayList<MatchEvent> getAllFactsForGivenMatchID(int match_id) { 
     Cursor cursor     = null; 
     ArrayList<MatchEvent> result = new ArrayList<>(); 

     //Get column values. 
     try { 
      cursor = db.rawQuery("SELECT * FROM " + DB_TABLE + " WHERE match_id=" + match_id, null); 
     } catch (Exception e) { 
      Log.e("Db err select * events", e.toString()); 
      e.printStackTrace(); 
     } 

     //Query result is not empty. 
     if (cursor != null && cursor.moveToFirst()) { 
      int typeIndex  = cursor.getColumnIndex(FactsDbAdapter.KEY_TYPE); 
      int player1Index = cursor.getColumnIndex(FactsDbAdapter.KEY_PLAYER1); 
      int player2Index = cursor.getColumnIndex(FactsDbAdapter.KEY_PLAYER2); 
      int teamIndex  = cursor.getColumnIndex(FactsDbAdapter.KEY_TEAM); 
      int timeIndex  = cursor.getColumnIndex(FactsDbAdapter.KEY_TIME); 

      do { 
       String type   = cursor.getString(typeIndex); 
       String player1  = cursor.getString(player1Index); 
       String player2  = cursor.getString(player2Index); 
       String team   = cursor.getString(teamIndex); 
       String time   = cursor.getString(timeIndex); 

       if (player2 != null) { 
        MatchEvent matchEvent = new MatchEvent(time, type, player1, player2, team); 
        result.add(matchEvent); 
       } else { 
        MatchEvent matchEvent = new MatchEvent(time, type, player1, team); 
        result.add(matchEvent); 
       } 

      } while (cursor.moveToNext()); 
     } 

     return result; 
    } 

} 

現在不工作的部分是本

int bpts = factsDbAdapter.countTriesWithMatchID(factsDbAdapter.KEY_MATCHID, factsDbAdapter.KEY_TYPE, factsDbAdapter.KEY_TEAM); 
         if (bpts<3) 

任何人都可以幫助我或引導我在正確的方向請更好地描述下面的代碼

switch (status) { 
       case WIN: {// Team won the match. 
        int totalWins = teamAdapter.getColumnValueForTeamInt(teamAdapter.KEY_WINS, team); 
        int totalPoints = teamAdapter.getColumnValueForTeamInt(teamAdapter.KEY_TOTALPOINTS, team); 
        int totalBonusPoints = teamAdapter.getColumnValueForTeamInt(teamAdapter.KEY_BONUSPOINTS, team); 
        teamAdapter.updateSingleColumn(team, teamAdapter.KEY_WINS, totalWins + 1); 
        int bpts = factsDbAdapter.countTriesWithMatchID(factsDbAdapter.KEY_MATCHID, factsDbAdapter.KEY_TYPE, factsDbAdapter.KEY_TEAM); 
        if (bpts<3) { 
         teamAdapter.updateSingleColumn(team, teamAdapter.KEY_BONUSPOINTS, totalBonusPoints + 1); 
         teamAdapter.updateSingleColumn(team, teamAdapter.KEY_TOTALPOINTS, totalPoints + 5); 
        } 
        else 
        { 
         teamAdapter.updateSingleColumn(team, teamAdapter.KEY_TOTALPOINTS, totalPoints + 4); 
        } 
        break; 
       } 

預先感謝您

+0

請顯示'factsDbAdapter.KEY_MATCHID'的定義和值。確保它是一個'int'而不是'String'。 – john16384

+0

@ Mark Rotteveel您好factDbAdapter.KEY_MATCHID的值應該是1到100之間的任何值,具體取決於記錄的匹配數量。 – Morne

+0

是的,但它是一個'字符串?因爲預計有'int',Java不會自動轉換這些類型。請顯示該行代碼。 – john16384

回答

1

你有一個方法定義爲:

public int countTriesWithMatchID(int match_id, String type, String teamName) 

你與調用它:

factsDbAdapter.countTriesWithMatchID(factsDbAdapter.KEY_MATCHID, factsDbAdapter.KEY_TYPE, factsDbAdapter.KEY_TEAM) 

如果換成你使用的是常量,呼叫變成:

factsDbAdapter.countTriesWithMatchID("match_id", "type", "team") 

但是,這將不起作用,因爲它們都是String類型,而您的countTriesWithMatchID需要intString和另一個String

所以,問題是你需要一個int第一個參數,匹配的id。像這樣的電話將工作,但你需要正確的身份證號碼:

factsDbAdapter.countTriesWithMatchID(1, "type", "team") 

我希望這可以幫助。

相關問題