Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7580209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:51:33+00:00 2026-05-30T17:51:33+00:00

I am adding a spinner dynamically in a layout, as; public class FRQuestionsDetail extends

  • 0

I am adding a spinner dynamically in a layout, as;

   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) {
    }
}

But when I click on the spinner, my app crashes and I got the following error.

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)

I do not know, how to select an item in the spinner or how to make a spinner selectable or clickable.

Note: My this activity is one of the activities in my ActivityGroup.
Please help me out in this. Thank You!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:51:35+00:00Added an answer on May 30, 2026 at 5:51 pm

    Your logcat says android.view.WindowManager$BadTokenException which means you are passing invalid Context somewhere.

    I doubt this line

     Spinner spin = new Spinner(this);
    

    Try using Spinner spin=new Spinner(YourActivity.this) or Spinner spin=new Spinner(getApplicationContext())

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Adding more than one child to a WPF StatusBar results in poor layout with
Adding functionality to a class can be done by adding a method or by
I am dynamically adding the Spinners in my application by parsing the XML file.
I have been following the tutorial for adding spinner items to a spinner control
Adding any noncopyable member to a class would prevent the automatic generation of copy
How would I go about adding a context menu to a spinner popup? I
I've this code for adding UIActivityIndicatorView to a UITableViewCell . UIActivityIndicatorView *spinner = [[UIActivityIndicatorView
Adding an element to the head of an alist (Associative list) is simple enough:
Adding source files more than one directory away (e.g. ../../source.cpp or ../../../somewhere_else/source.cpp, vs. just
Adding Exceptional Handling is a good practise, but I have a doubt, In our

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.