ошибка на setOnClickListener, которая продолжает говорить, не может разрешить символ [duplicate]

Docopt имеет реализацию C, которую я считал довольно приятной: https://github.com/docopt/docopt.c

Из стандартного формата в формате man, параметры командной строки, docopt infers и создает парсер аргументов. Это началось в python; версия python буквально просто анализирует docstring и возвращает dict. Для этого в C требуется немного больше работы, но он чист в использовании и не имеет внешних зависимостей.

-4
задан RamithDR 2 September 2016 в 20:39
поделиться

3 ответа

Ваш код должен быть внутри метода onCreate, попробуйте:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

    // Find the View that shows the numbers category
    TextView numbers = (TextView) findViewById(R.id.numbers);

        // Set a click listener on that View
        numbers.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when the numbers View is clicked on.

        @Override
        public void onClick(View view) {
            Intent numbersIntent = new Intent(MainActivity.this, Numbers.class);
            startActivity(numbersIntent);
        }
    }
}

Подробнее о действиях и их жизненном цикле: Жизненный цикл активности

2
ответ дан RamithDR 15 August 2018 в 22:48
поделиться
  • 1
    Также исключение null-указателя в методе onClick. Помогите мне избавиться от этого. – Akshay Mg 3 September 2016 в 16:35
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the content of the activity to use the activity_main.xml layout file
    setContentView(R.layout.activity_main);

    // Find the View that shows the numbers category
    TextView numbers = (TextView) findViewById(R.id.numbers);

    // Set a click listener on that View
    numbers.setOnClickListener(new View.OnClickListener() {
    // The code in this method will be executed when the numbers View is clicked on.

    @Override
    public void onClick(View view) {
        Intent numbersIntent = new Intent(MainActivity.this, Numbers.class);
        startActivity(numbersIntent);
    }
}

или:

public class MainActivity extends AppCompatActivity implements OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        // Find the View that shows the numbers category
        TextView numbers = (TextView) findViewById(R.id.numbers);

        // Set a click listener on that View
        numbers.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent numbersIntent = new Intent(MainActivity.this, Numbers.class);
        startActivity(numbersIntent);
    }
}
0
ответ дан Amg91 15 August 2018 в 22:48
поделиться

Ваш onClickListener должен быть в вашем методе onCreate.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);
        // Find the View that shows the numbers category
        TextView numbers = (TextView) findViewById(R.id.numbers);

        // Set a click listener on that View
        numbers.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when the numbers View is clicked on.
            @Override
            public void onClick(View view) {
                Intent numbersIntent = new Intent(MainActivity.this, Numbers.class);
                startActivity(numbersIntent);
           }
     });
     }
}

Это должно исправить ошибку. Также убедитесь, что вы добавили Numbers.class в AndroidManifest, иначе вы получите еще одну ошибку при запуске намерения.

<activity
        android:name=".Numbers"
        android:label="Numbers"/>
1
ответ дан LBJ33 15 August 2018 в 22:48
поделиться
  • 1
    почему он создает исключение нулевого указателя в методе «numers.setOnClickListener (...)»? – Akshay Mg 6 September 2016 в 17:32
  • 2
    Является ли текстовое число "номерами" в activity_main.xml? – LBJ33 6 September 2016 в 17:40
  • 3
    да, это в activity_main.xml – Akshay Mg 9 September 2016 в 17:40
Другие вопросы по тегам:

Похожие вопросы: