Я добавляю счетчик динамически в макет, как;
public class FRQuestionsDetail extends Activity implements OnClickListener,
OnItemSelectedListener {
private TextView tvCategoryType;
private TextView tvCategoryObtainScore;
private TextView tvCategoryTotalScore;
private TextView tvQuestionNumber;
private TextView tvQuestionPercent;
private TextView tvQuestion;
private Button btnNextQuestion;
private Button btnPrevQuestion;
private LinearLayout linearDynamicQuesOptions;
private int scoringType;
private int reviewId;
private int reviewLocationId;
private int reviewCategoryId;
private int questionId;
private int count = 0;
private int position;
private long categoryScore;
private long questionScore;;
private String reviewCategoryType;
private String question;
private ArrayAdapter<String> qOptionsAdapter;
private static ArrayList<FRReviewQuestions> questionsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question_detail_page);
tvCategoryType = (TextView) findViewById(R.id.tv_category_type_q_detail);
tvCategoryObtainScore = (TextView) findViewById(R.id.tv_category_score_obtain_q_detail);
tvCategoryTotalScore = (TextView) findViewById(R.id.tv_category_score_total_q_detail);
tvQuestionNumber = (TextView) findViewById(R.id.tv_question_number);
tvQuestionPercent = (TextView) findViewById(R.id.tv_question_percent);
tvQuestion = (TextView) findViewById(R.id.tv_question);
btnNextQuestion = (Button) findViewById(R.id.btn_nextQuestion);
btnPrevQuestion = (Button) findViewById(R.id.btn_prevQuestion);
linearDynamicQuesOptions = (LinearLayout) findViewById(R.id.ll_dynamic_options);
btnNextQuestion.setOnClickListener(this);
btnPrevQuestion.setOnClickListener(this);
SharedPreferences savedReviewPref = getSharedPreferences(
Configuration.START_REVIEW, MODE_WORLD_READABLE);
scoringType = savedReviewPref.getInt(
Configuration.REVIEW_TEMPLATE_SCORING_TYPE, 0);
Bundle extras = getIntent().getExtras();
reviewLocationId = extras.getInt("reviewLocationId");
reviewId = extras.getInt("reviewId");
reviewCategoryId = extras.getInt("reviewCategoryId");
reviewCategoryType = extras.getString("reviewCategoryType");
questionId = extras.getInt("questionId");
categoryScore = extras.getLong("categoryScore");
questionScore = extras.getLong("questionScore");
position = extras.getInt("position");
question = extras.getString("question");
position = position + 1;
questionsList = getQuestionDbContent();
count = FRReviewQuestionsTable.countQuestions;
tvCategoryType.setText(reviewCategoryType);
if (scoringType == 3) {
tvCategoryTotalScore.setText("(" + categoryScore + "%)");
// tvQuestionPercent.setText(" (" + questionScore + "%)" );
} else if (scoringType == 2) {
tvCategoryTotalScore.setText("(" + categoryScore + ")");
// tvQuestionPercent.setText(" (" + questionScore + ")" );
} else if (scoringType == 1) {
tvCategoryTotalScore.setText(" ");
// tvQuestionPercent.setText(" ");
}
drawQuestion(position);
// tvQuestionNumber.setText(" " + position + " of " + count);
// tvQuestion.setText(question);
}
private ArrayList<FRReviewQuestions> getQuestionDbContent() {
ArrayList<FRReviewQuestions> arrayQuestions = new ArrayList<FRReviewQuestions>();
FRReviewQuestionsTable db = new FRReviewQuestionsTable(
FRQuestionsDetail.this);
arrayQuestions = db
.getReviewQuestionWrtIdFromDB(
"review_question_id,review_question_category_id,review_question,review_question_type,review_question_is_pic_req,review_question_score,review_question_is_deleted,review_question_is_ans_req,review_question_max_length,review_question_last_sync_date",
reviewCategoryId);
db.CloseDB();
return arrayQuestions;
}
public void drawQuestion(int index) {
FRReviewQuestions questions = new FRReviewQuestions();
questions = questionsList.get(index - 1);
String ques = questions.getReviewQuestion();
int quesId = questions.getReviewQuestionId();
int isAnsReq = questions.getIsAnsReq();
int questionType = questions.getQuestionType();
long maxLength = questions.getMaxLength();
long questionScore = questions.getQuestionScore();
if (scoringType == 3) {
tvQuestionPercent.setText(" (" + questionScore + "%)");
} else if (scoringType == 2) {
tvQuestionPercent.setText(" (" + questionScore + ")");
} else if (scoringType == 1) {
tvQuestionPercent.setText(" ");
}
tvQuestionNumber.setText(" " + index + " of " + count);
tvQuestion.setText(ques);
if (questionType == 1) {
dropDownQuestion(quesId);
} else if (questionType == 2) {
} else if (questionType == 3) {
} else if (questionType == 4) {
} else {
}
}
private void dropDownQuestion(int quesId) {
ArrayList<FRQuestionOptions> arrayQuestionOptions = new ArrayList<FRQuestionOptions>();
arrayQuestionOptions = getQuestionOptionsDbContent(quesId);
Spinner spin = new Spinner(this);
spin.setClickable(true);
// spin.setContentDescription("Select");
spin.setPrompt("Select");
spin.setOnItemSelectedListener(this);
// linearDynamicQuesOptions.addView(spin, new LinearLayout.LayoutParams(
// LinearLayout.LayoutParams.MATCH_PARENT,
// LinearLayout.LayoutParams.WRAP_CONTENT));
qOptionsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item);
qOptionsAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(qOptionsAdapter);
int len = arrayQuestionOptions.size();
FRQuestionOptions qOptions;
for (int i = 0; i < len; i++) {
qOptions = arrayQuestionOptions.get(i);
String value = qOptions.getValue();
qOptionsAdapter.add(value);
qOptionsAdapter.notifyDataSetChanged();
}
linearDynamicQuesOptions.addView(spin, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
public void onClick(View v) {
if (v == btnNextQuestion) {
} else if (v == btnPrevQuestion) {
}
}
private ArrayList<FRQuestionOptions> getQuestionOptionsDbContent(
int questionId) {
ArrayList<FRQuestionOptions> arrayQuestionOptions = new ArrayList<FRQuestionOptions>();
FRQuesOptionsTable db = new FRQuesOptionsTable(FRQuestionsDetail.this);
arrayQuestionOptions = db
.getReviewQuestionWrtIdFromDB(
"option_id,question_id,question_score,question_value,question_is_deleted,question_last_sync_date",
questionId);
db.CloseDB();
return arrayQuestionOptions;
}
public void onItemSelected(AdapterView<?> arg0, View view, int position,
long id) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Но когда я нажимаю на счетчик, мое приложение вылетает, и я получаю следующую ошибку.
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): FATAL EXCEPTION: main
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40b5bdb0 is not valid; is your activity running?
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.view.ViewRoot.setView(ViewRoot.java:562)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:179)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.view.Window$LocalWindowManager.addView(Window.java:433)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.app.Dialog.show(Dialog.java:265)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.widget.Spinner.performClick(Spinner.java:260)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.view.View$PerformClick.run(View.java:9320)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.os.Handler.handleCallback(Handler.java:587)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.os.Handler.dispatchMessage(Handler.java:92)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.os.Looper.loop(Looper.java:150)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at android.app.ActivityThread.main(ActivityThread.java:4385)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at java.lang.reflect.Method.invoke(Method.java:507)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
03-01 11:40:31.864: ERROR/AndroidRuntime(31327): at dalvik.system.NativeStart.main(Native Method)
Я не знаю, как выбрать элемент в счетчике или как сделать его доступным для выбора или щелчка.
Примечание. Это действие является одним из действий в моей группе действий.Пожалуйста, помогите мне в этом.Спасибо!