Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using findviewbyid but it is not working.I gave an id in xml file by the name of rootView .I also created textview to display the text in this activity but it is not working.Please help...below is the code
Java
package com.example.android.miwok;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    public class NumbersActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_numbers);
            // array created
    
            /*String[] words= new String[10];
            words[0]="one";
            words[1]="two";
            words[2]="three";
            words[3]="four";
            words[4]="five";
            words[5]="six";
            words[6]="seven";
            words[7]="eight";
            words[8]="nine";
            words[9]="ten";*/
    
            // arraylist created
    
            ArrayList<string> words =new ArrayList<string>();
            words.add("one");
            words.add("two");
            words.add("three");
            words.add("four");
            words.add("five");
            words.add("six");
            words.add("seven");
            words.add("eight");
            words.add("nine");
            words.add("ten");
    
            LinearLayout rootView=(LinearLayout)findViewById(R.id.rootView);
            TextView wordView=new TextView(this);
            wordView.setText(words.get(0));
            rootView.addView(wordView);
            return rootView;
    
            // logs created
    
            /*Log.v("NumbersActivity", "Word at index 0: "+ words.get(0));
            Log.v("NumbersActivity","Word at index 1: "+ words.get(1));
            Log.v("NumbersActivity","Word at index 2: "+ words.get(2));
            Log.v("NumbersActivity","Word at index 3: "+ words.get(3));
            Log.v("NumbersActivity","Word at index 4: "+ words.get(4));
            Log.v("NumbersActivity","Word at index 5: "+ words.get(5));
            Log.v("NumbersActivity","Word at index 6: "+ words.get(6));
            Log.v("NumbersActivity","Word at index 7: "+ words.get(7));
            Log.v("NumbersActivity","Word at index 8: "+ words.get(8));
            Log.v("NumbersActivity","Word at index 9: "+ words.get(9));*/
    
    
    
        }
    }


XML
Java
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rootView"
        android:orientation="vertical"
        tools:context=".NumbersActivity">


What I have tried:

i researched and always find the code where fragments were used but i am not using fragment.
Posted
Updated 11-Sep-18 6:46am
v2
Comments
David Crow 10-Sep-18 8:33am    
Sounds like you are wanting to get the parent of some control (which you did not show) in your layout file. Call findViewById() on that child control, and then call getParent() until you find R.id.rootView.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900