1

我正在尝试从我的数据库中检索单行并将其显示到活动中。我想知道是否可以使用 Getters 和 Setters 来传递值。我能够轻松地检索数据,但问题是我似乎在使用 getter 和 setter 传递值时遇到问题。这是我在 LogCat 上得到的。

09-22 13:46:33.573: D/Enter Activity(3110): You have entered Score Activity
09-22 13:46:34.177: D/Data Retreival(3110): Getting Data
09-22 13:46:34.183: D/User(3110): 1 XyLoL 0 0 null null
09-22 13:46:34.183: D/Database(3110): Database Close
09-22 13:46:34.194: D/USER INFO(3110): 0 null 0 0 null null
09-22 13:46:34.194: W/ResourceType(3110): No package identifier when getting value for resource number 0x00000000
09-22 13:46:34.203: D/AndroidRuntime(3110): Shutting down VM
09-22 13:46:34.203: W/dalvikvm(3110): threadid=1: thread exiting with uncaught exception (group=0x40014760)
09-22 13:46:34.223: E/AndroidRuntime(3110): FATAL EXCEPTION: main
09-22 13:46:34.223: E/AndroidRuntime(3110): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xylol.o_chabeta/com.xylol.o_chabeta.Enter_Score}: android.content.res.Resources$NotFoundException: String resource ID #0x0

我只想问有没有

User_Score.java(活动)

public class Enter_Score extends Activity implements OnClickListener {

    ScoreDataSource datasource;
    UserHandler user;

    //Textview Declaration

    TextView username;
    TextView score;
    TextView questions_left;
    TextView best_cat;
    TextView worst_cat;

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

        username = (TextView) findViewById(R.id.stats_username);
        score = (TextView) findViewById(R.id.stat_score);
        questions_left = (TextView) findViewById(R.id.stat_questions_left);
        best_cat = (TextView) findViewById(R.id.stats_best);
        worst_cat = (TextView) findViewById(R.id.stats_worst);

        datasource = new ScoreDataSource(this);
        datasource.open();
        Log.d("Data Retreival", "Getting Data");
        datasource.getScore();
        datasource.close();


        UserHandler user = new UserHandler();

        long user_id = user.getUSER_ID();
        String user_n =user.getUSER_NAME();
        int user_s =user.getUSER_SCORE();
        int user_ql= user.getUSER_QUESTIONS_LEFT();
        String user_b = user.getUSER_BEST_CATEGORY() ;
        String user_w =  user.getUSER_WORST_CATEGORY();

        Log.d("USER INFO", user_id+ " " + user_n + " " + user_s + " " + user_ql + " " + user_b + " " + user_w);

        username.setText(user_n);
        score.setText(user_s);
        questions_left.setText(user_ql);
        best_cat.setText(user_b);
        worst_cat.setText(user_w);


        Button b = (Button) findViewById(R.id.back);
        b.setOnClickListener(this);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.enter__score, menu);
        return true;
    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.back){
            Intent i = new Intent(Enter_Score.this, MainActivity.class);
            startActivity(i);
            finish();
        }

    }



}

ScoreDataSource.java(数据检索)

public class ScoreDataSource extends Enter_Score{


    SQLiteOpenHelper dbhelper;
    SQLiteDatabase database;


    private static final String[] allColumns = {
        O_chaDBOpenHelper.USER_ID,
        O_chaDBOpenHelper.USER_NAME,
        O_chaDBOpenHelper.USER_SCORE,
        O_chaDBOpenHelper.USER_QUESTIONS_LEFT,
        O_chaDBOpenHelper.USER_BEST_CATEGORY,
        O_chaDBOpenHelper.USER_WORST_CATEGORY
    };

    public ScoreDataSource(Context context){

        dbhelper = new O_chaDBOpenHelper(context);
    }

    public void open() {
        database = dbhelper.getReadableDatabase();
    }

    public void close() {
        Log.d("Database", "Database Close");
        dbhelper.close();
    }

    public void getScore() {

        Cursor c = database.query(O_chaDBOpenHelper.USER_TABLE, allColumns, 
                null, null, null, null, null);
                c.moveToFirst();

        int iid = c.getColumnIndex(O_chaDBOpenHelper.USER_ID);
        int iusername = c.getColumnIndex(O_chaDBOpenHelper.USER_NAME);
        int iscore = c.getColumnIndex(O_chaDBOpenHelper.USER_SCORE);
        int iql = c.getColumnIndex(O_chaDBOpenHelper.USER_QUESTIONS_LEFT);
        int ibc = c.getColumnIndex(O_chaDBOpenHelper.USER_BEST_CATEGORY);
        int iwc = c.getColumnIndex(O_chaDBOpenHelper.USER_WORST_CATEGORY);

        // Get and Set Valueszzz

        long id = c.getLong(iid);
        String username = c.getString(iusername);
        int score = c.getInt(iscore);
        int questions_left = c.getInt(iql);
        String best_cat = c.getString(ibc);
        String worst_cat = c.getString(iwc);

        Log.d("User", id+ " " + username+  " " + score+ " " + questions_left + " " + best_cat + " " + worst_cat );

        UserHandler user = new UserHandler();

        user.setUSER_ID(id);
        user.setUSER_NAME(username);
        user.setUSER_SCORE(score);
        user.setUSER_QUESTIONS_LEFT(questions_left);
        user.setUSER_BEST_CATEGORY(best_cat);
        user.setUSER_WORST_CATEGORY(worst_cat);


    }

}

UserHandler.java(Getter 和 Setter)

public class UserHandler {

    private long USER_ID;
    private String USER_NAME;
    private int USER_SCORE;
    private int USER_QUESTIONS_LEFT;
    private String USER_BEST_CATEGORY;
    private String USER_WORST_CATEGORY;

    public UserHandler(){

    }

    public UserHandler(int ID, String NAME, String GENDER, String COUNTRY, int SCORE, int QUESTION_LEFT, String BEST_CATEGORY, String WORST_CATEGORY){
        this.USER_ID = ID;
        this.USER_NAME = NAME;
        this.USER_SCORE = SCORE;
        this.USER_QUESTIONS_LEFT = QUESTION_LEFT;
        this.USER_BEST_CATEGORY = BEST_CATEGORY;
        this.USER_WORST_CATEGORY = WORST_CATEGORY;
    }

    public long getUSER_ID() {
        return USER_ID;
    }

    public void setUSER_ID(long uSER_ID) {
        this.USER_ID = uSER_ID;
    }

    public String getUSER_NAME() {
        return USER_NAME;
    }

    public void setUSER_NAME(String uSER_NAME) {
        this.USER_NAME = uSER_NAME;
    }

    public int getUSER_SCORE() {
        return USER_SCORE;
    }

    public void setUSER_SCORE(int uSER_SCORE) {
        this.USER_SCORE = uSER_SCORE;
    }

    public int getUSER_QUESTIONS_LEFT() {
        return USER_QUESTIONS_LEFT;
    }

    public void setUSER_QUESTIONS_LEFT(int uSER_QUESTIONS_LEFT) {
        this.USER_QUESTIONS_LEFT = uSER_QUESTIONS_LEFT;
    }

    public String getUSER_BEST_CATEGORY() {
        return USER_BEST_CATEGORY;
    }

    public void setUSER_BEST_CATEGORY(String uSER_BEST_CATEGORY) {
        this.USER_BEST_CATEGORY = uSER_BEST_CATEGORY;
    }

    public String getUSER_WORST_CATEGORY() {
        return USER_WORST_CATEGORY;
    }

    public void setUSER_WORST_CATEGORY(String uSER_WORST_CATEGORY) {
        this.USER_WORST_CATEGORY = uSER_WORST_CATEGORY;
    }

}
4

1 回答 1

0

The object of UserHandler you created in User_Score.java and ScoreDataSource.java are different. Hence you didn't get the correct value.

May be you can create UserHandler class Singleton.

于 2013-09-30T05:27:33.697 回答