无法解析方法' addView(android.widget.TextView)

时间:2016-11-19 08:10:49

标签: android

以下是代码:

package com.example.android.miwok;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import static com.example.android.miwok.R.id.rootView;

public class NumberActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_number);

    // creating an ArrayList[]
    ArrayList<String> numbersList = new ArrayList<String>();
    numbersList.add(0, "One");
    numbersList.add(1, "Two");
    numbersList.add(2, "Three");
    numbersList.add(3, "Four");
    numbersList.add(4, "Five");
    numbersList.add(5, "Six");
    numbersList.add(6, "Seven");
    numbersList.add(7, "Eight");
    numbersList.add(8, "Nine");
    numbersList.add(9, "Ten");

    // Displaying Numbers

   /* LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView);
    TextView wordView = new TextView(this);
    wordView.setText(numbersList.get(0));
    rootView.addView(wordView);*/

    int index=0;

    // Keep looping until we've reached the end of the list (which means keep looping
    // as long as the current index position is less than the length of the list)
    while (index < numbersList.size()) {
        // Create a new TextView
        TextView wordView = new TextView(this);

        // Set the text to be word at the current index
        wordView.setText(numbersList.get(index));

        // Add this TextView as another child to the root view of this layout
        rootView.addView(wordView);

        // Increment the index variable by 1
        index++;
    }
}

}

我收到此错误, Android工作室显示&#34;无法解析方法&#39; addView(android.widget.TextView)&#39;&#34;

我该如何解决这个问题? 我试过清理代码仍然无法正常工作。

0 个答案:

没有答案