2014-06-23 3 views
0

퀴즈 게임에이 코드 http://automateddeveloper.blogspot.co.uk/2011/06/getting-started-complete-android-app.html을 적용하려고합니다. 위의 링크의 의견란에 제안 된대로 http://sourceforge.net/projects/sqlitebrowser/을 다운로드했습니다. 내 장치에서 내 자신의 DB를 실행하고 Eclipse를 사용하고 Android 4를 타겟팅하고 있습니다. 모든 작업. 내가 작동하도록 할 수없는 것은 SettingsActivity입니다. 새 데이터베이스에서 난이도 등급을 변경하고 Easy, Medium 및 Hard가 있는지 확인했습니다.하나의 활동에서 다른 활동으로 SharedPreferences를 보내는 방법

설정을 "보통"으로두면 난이도없이 퀴즈 게임이 꺼집니다. 설정을 "Easy"로 변경 한 다음 메뉴로 돌아가서 게임을 클릭하면 게임이 멈추고 있음을 알려줍니다.

내가 관련 코드라고 생각하는 내용을 게시하고 있습니다. 나는 링크의 댓글 섹션에 게시 된 것 이외의 것을 찾을 수 없었습니다.

SplashActivity :

private List<Question> getQuestionSetFromDb() throws Error { 
     int diff = getDifficultySettings(); 
     Syste`enter code here`m.out.print("diff" + diff); 
     //Log("diff" + diff); 
     int numQuestions = getNumQuestions(); 
     DBHelper myDbHelper = new DBHelper(this); 
     try { 
      myDbHelper.createDataBase(); 
     } catch (IOException ioe) { 
      throw new Error("Unable to create database"); 
     } 
     try { 
      myDbHelper.openDataBase(); 
     }catch(SQLException sqle){ 
      throw sqle; 
     } 
     List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions); 
     myDbHelper.close(); 
     return questions; 
    } 

/** 
    * Method to return the difficulty settings 
    * @return 
    */ 
    private int getDifficultySettings() { 
     SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0); 
     int diff = settings.getInt(Constants.DIFFICULTY, Constants.EASY); 
     return diff; 
    } 

SettingsActivity :이에서 코드 게시물을 유지할 수 있도록 코드의 나머지 부분은 잘 작동

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.settings); 

     /** 
     * set listener on update button 
     */ 
     Button updateBtn = (Button) findViewById(R.id.nextBtn); 
     updateBtn.setOnClickListener(this); 

     /** 
     * Set selected button if saved 
     */ 
     updateButtonWithPreferences(); 

    } 


    /** 
    * Method to update default check box 
    */ 
    private void updateButtonWithPreferences() { 
     RadioButton c1 = (RadioButton)findViewById(R.id.easySetting); 
     RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting); 
     RadioButton c3 = (RadioButton)findViewById(R.id.hardSetting); 

     SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0); 
     int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM); 

     switch (diff) 
     { 
     case Constants.EASY : 
      c1.toggle(); 
      break; 

     case Constants.MEDIUM : 
      c2.toggle(); 
      break; 

     case Constants.EXTREME : 
      c3.toggle(); 
      break; 
     } 
    } 





    @Override 
    public void onClick(View arg0) { 
     /** 
     * check which settings set and return to menu 
     */ 
     if (!checkSelected()) 
     { 
      return; 
     } 
     else 
     { 
      SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0); 
      Editor e = settings.edit(); 
      e.putInt(Constants.DIFFICULTY, getSelectedSetting()); 
      e.commit(); 
      finish(); 
     } 

    } 


    /** 
    * Method to check that a checkbox is selected 
    * 
    * @return boolean 
    */ 
    private boolean checkSelected() { 
     RadioButton c1 = (RadioButton)findViewById(R.id.easySetting); 
     RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting); 
     RadioButton c3 = (RadioButton)findViewById(R.id.hardSetting); 
     return (c1.isChecked() || c2.isChecked() || c3.isChecked()); 
    } 


    /** 
    * Get the selected setting 
    */ 
    private int getSelectedSetting() { 
     RadioButton c1 = (RadioButton)findViewById(R.id.easySetting); 
     RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting); 
     if (c1.isChecked()) 
     { 
      return Constants.EASY; 
     } 
     if (c2.isChecked()) 
     { 
      return Constants.MEDIUM; 
     } 

     return Constants.EXTREME; 
    } 

} 

내가 DB에 넣어 새로운 질문을 포함 누군가가 더 이상 아무것도 요구하지 않는 한. 사전에

고마워, 테리

+0

stacktrace 게시 오류가있는 경우 – tyczj

+0

이제 디버깅에 대한 자습서를 진행합니다. "diff"(난이도 호출) 및 updateButtonWithPreferences() 호출에 중단 점을 배치했지만이 경험에 대한 내 경험은 제한적이지만 계속해서 지식을 습득합니다. 이와 관련하여 조언이 있습니까? –

+0

tyczj, 여기에 "쉬운"버튼을 선택하고 "재생"버튼을 실행 한 후 스택 추적이 있습니다 : [email protected] java.lang.IndexOutOfBoundsException : 유효하지 않은 인덱스 0, 크기가 0 com.igogeo.android .chuck.QuestionActivity 42d325f8 ActivityInfo 42d31c68 com.igogeo.android.chuck.QuestionActivity {} { ComponentInfo com.igogeo.android.chuck/com.igogeo.android.chuck.QuestionActivity} { ActivityRecord 42d324f8 토큰 = android.os @ .BinderProxy @ 42d31c08 {com.igogeo.android.chuck/com.igogeo.android.chuck.QuestionActivity}} null –

답변

1

사용이 응용 프로그램 범위 공유 환경 설정을 사용하기 :

대신에
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(Context context) 

: 된 SharedPreferences 설정 = getSharedPreferences (Constants.SETTINGS, 0);

+0

무엇 대신/어디에서 ak? –

+0

OK, ak가 테스트하고 결과를 줄 것입니다 ... 감사합니다! –

관련 문제