2017-03-05 10 views
-3

android.iの新しいカテゴリを持つクイズアプリを作ろうとしています。最初のボタンがクリックされたときに最初の10個の質問が実行され、2番目のボタンがクリックされると10個から20個の質問が実行されるように、各カテゴリーのクリックで異なるint値を渡しています。インテントを通してあるクラスから別のクラスに価値を引き出す方法は?

package com.example.chaitanya.myquiz; 

    import java.util.List; 
     import java.util.Random; 
    import java.util.Timer; 
    import java.util.concurrent.TimeUnit; 

    import android.annotation.SuppressLint; 
    import android.annotation.TargetApi; 
    import android.app.Activity; 
     import android.content.Intent; 
     import android.graphics.Typeface; 
     import android.media.MediaPlayer; 
     import android.os.Build; 
     import android.os.Bundle; 
     import android.os.CountDownTimer; 
     import android.support.annotation.Nullable; 
     import android.util.Log; 
     import android.view.View; 
     import android.widget.Button; 
     import android.widget.TextView; 

     import static java.lang.Integer.parseInt; 


    public class QuestionActivity extends Activity { 
    List<Question> quesList; 
    int score = 0; 
    Random r = new Random(); 
    Question currentQ; 
    TextView txtQuestion, times, scored; 
    Button button1, button2, button3; 
    CounterClass timer; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Intent intent2 = getIntent(); 
    int qid = intent2.getIntExtra("qid",10); 
    QuizHelper db = new QuizHelper(this); // my question bank class 
    quesList = db.getAllQuestions(); // this will fetch all quetonall 
    questions 
    currentQ = quesList.get(qid); // the current question 

    txtQuestion = (TextView) findViewById(R.id.txtQuestion); 
    // the textview in which the question will be displayed 

    // the three buttons, 

    button1 = (Button) findViewById(R.id.button1); 
    button2 = (Button) findViewById(R.id.button2); 
    button3 = (Button) findViewById(R.id.button3); 

    // the textview in which score will be displayed 
    scored = (TextView) findViewById(R.id.score); 

    // the timer 
    times = (TextView) findViewById(R.id.timers); 


    // method which will set the things up for our game 
    setQuestionView(); 
    times.setText("00:00:30"); 

    // A timer of 30 seconds to play for, with an interval of 1 second (1000 milliseconds) 
    timer = new CounterClass(30000, 1000); 
    timer.start(); 

    // button click listeners 
    button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      // passing the button text to other method 
      // to check whether the anser is correct or not 
      // same for all three buttons 
      getAnswer(button1.getText().toString()); 
     } 
    }); 

    button2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      getAnswer(button2.getText().toString()); 
     } 
    }); 

    button3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      getAnswer(button3.getText().toString()); 
     } 
    }); 
} 

public void getAnswer(String AnswerString) { 
    Intent intent2 = getIntent(); 
    int qid = intent2.getIntExtra("qid", 10); 
    if (currentQ.getANSWER().equals(AnswerString)) { 

     // if conditions matches increase the int (score) by 1 
     // and set the text of the score view 
     score++; 
     scored.setText("Score : " + score); 

    } else { 

     // if unlucky start activity and finish the game 
     timer.cancel(); 
     Intent intent = new Intent(QuestionActivity.this, 
       ResultActivity.class); 
     // passing the int value 
     Bundle b = new Bundle(); 
     b.putInt("score", score); // Your score 
     intent.putExtras(b); // Put your score to your next 
     startActivity(intent); 
     finish(); 
    } 

    if (qid < 60) { 

     // if questions are not over then do this 
     currentQ = quesList.get(qid); 
     setQuestionView(); 
    } else { 
     // if over do this 
     timer.cancel(); 
     Intent intent = new Intent(QuestionActivity.this, 
       ResultActivity.class); 
     Bundle b = new Bundle(); 
     b.putInt("score", score); // Your score 
     intent.putExtras(b); // Put your score to your next 
     startActivity(intent); 
     finish(); 
    } 


} 


@TargetApi(Build.VERSION_CODES.GINGERBREAD) 
@SuppressLint("NewApi") 
public class CounterClass extends CountDownTimer { 

    public CounterClass(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
     // TODO Auto-generated constructor stub 
    } 


    @Override 
    public void onFinish() { 
     times.setText("Time is up"); 
     Intent intent = new Intent(QuestionActivity.this, 
       ResultActivity.class); 
     timer.cancel(); 
     Bundle b = new Bundle(); 
     b.putInt("score", score); // Your score 
     intent.putExtras(b); // Put your score to your next 
     startActivity(intent); 
     finish(); 

    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     // TODO Auto-generated method stub 

     long millis = millisUntilFinished; 
     String hms = String.format(
       "%02d:%02d:%02d", 
       TimeUnit.MILLISECONDS.toHours(millis), 
       TimeUnit.MILLISECONDS.toMinutes(millis) 
         - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS 
         .toHours(millis)), 
       TimeUnit.MILLISECONDS.toSeconds(millis) 
         - 
      TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS 
         .toMinutes(millis))); 
     System.out.println(hms); 
     times.setText(hms); 
    } 


} 

private void setQuestionView() { 
    Random r = new Random(); 
    Intent intent2 = getIntent(); 
    int qid = intent2.getIntExtra("qid", 10); 
    switch (qid) { 
     case 10: 
      // the method which will put all things together 
      txtQuestion.setText(currentQ.getQUESTION()); 
      button1.setText(currentQ.getOPTA()); 
      button2.setText(currentQ.getOPTB()); 
      button3.setText(currentQ.getOPTC()); 
      qid = (r.nextInt(10) + 1); 
     case 20: 
      txtQuestion.setText(currentQ.getQUESTION()); 
      button1.setText(currentQ.getOPTA()); 
      button2.setText(currentQ.getOPTB()); 
      button3.setText(currentQ.getOPTC()); 
      qid = (r.nextInt(20) + 11); 
     case 30: 
      txtQuestion.setText(currentQ.getQUESTION()); 
      button1.setText(currentQ.getOPTA()); 
      button2.setText(currentQ.getOPTB()); 
      button3.setText(currentQ.getOPTC()); 
      qid = (r.nextInt(30) + 21); 
     } 

      } 
     } 
+0

を持っていないあなたは私がエラーログを示しすることはできますか? –

+0

私たちの伐採に関するあなたの問題を理解することは本当に難しいです – EasyE

答えて

0

ネットワークにアクセスするにはインターネットアクセス権が必要です。

AndroidManifest.xmlにこれらの権限を追加します。

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

詳細:例外はNULLポインタから上げてhttps://developer.android.com/training/basics/network-ops/connecting.html

0

、あなたの活動の開始目的は、余分なバンドル

java.lang.NullPointerException: Attempt to  
      invoke virtual method 'double 
     android.os.Bundle.getDouble(java.lang.String)' on a null    
       object reference 
       at  
       com.example.chaitanya.calculatornew. 
       MainActivity.findtheweather(MainActivity.java:27) 
関連する問題