2017-03-27 3 views
-2

Good DayAndroid Studio Falscher 1. Argumenttyp. Gefunden ‚java.lang.String‘ erforderlich ‚int‘

Ich habe die Website suchen, konnte aber nichts bekommen zu mir in meinem Problem zu helfen. Ich bin ein Newby zu Android und startete ein Projekt, das ich einfach nicht abschließen kann.

Android Studio 2.3 App-Name „Match Tracker“

es die Verwendung von hier Ok gehen Beispiel meine app Sie ein Spiel „Rugby“ jetzt verfolgen können würde ich die App automatisch hinzufügen Punkte auf dem Leaderboard gefallen wenn ein Team 4 oder mehr Versuche hat und da stehe ich fest !!

meine FactDBAdaptor.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; 
    } 

} 

jetzt in meinem RugbyActivity.class der Teil, der nicht das ist

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

niemand arbeiten kann hilf mir oder steuere mich in die richtige richtung bitte besser beschreibung des codes unter

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; 
       } 

Vielen Dank im Voraus

+0

Bitte zeigen Sie die Definition und den Wert von 'factsDbAdapter.KEY_MATCHID'. Stellen Sie sicher, dass es sich um einen 'int' und nicht um einen' String' handelt. – john16384

+0

@ Mark Rotteveel Hi der Wert von factsDbAdapter.KEY_MATCHID sollte eine Sache von 1 bis 100 sein, abhängig von der Anzahl der Übereinstimmungen wird aufgezeichnet. – Morne

+0

Ja, aber ist es ein 'String'? Weil ein 'int' erwartet wird und Java diese Typen nicht automatisch konvertiert. Bitte zeigen Sie diese Codezeile an. – john16384

Antwort

1

Sie haben eine Methode wie folgt definiert:

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

Und Sie nennen es mit:

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

Wenn Sie die Konstanten ersetzen Sie verwenden, den Anruf wird:

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

Allerdings Dies funktioniert nicht, da sie alle vom Typ String sind, während Ihr countTriesWithMatchID einen int, einen String und einen weiteren String erwartet.

Also das Problem ist, dass Sie eine int für den ersten Parameter, die ID der Übereinstimmung benötigen. Ein Anruf wie dieser würde funktionieren, aber Sie benötigen die richtige ID-Nummer:

Ich hoffe, das hilft.

Verwandte Themen