0

В Fragment я сделал кнопку, обработчик нажатия и с помощью Intent хочу перейти в класс Karta, но не получается. Eclipse ругается The constructor Intent(new View.OnClickListener(){}, Class<Karta>) is undefined

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Fragment1 extends Fragment {

    Button button1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment1,
                container, false);

        button1=(Button)rootView.findViewById(R.id.button1);

        button1.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent (this,Karta.class);
                startActivity(intent);
            }});

        return rootView;

    }

}

import android.app.Activity;
import android.os.Bundle;

public class Karta extends Activity {

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

        }
    }
6
  • Код класса Karta покажите что ли. 4 мар 2014 в 19:53
  • Он в принципе пустой...
    – qwen
    4 мар 2014 в 19:56
  • а в манифесте ты добавить Karta не забыл часом? 4 мар 2014 в 19:59
  • 1
    Вот правильный ответ Intent intent = new Intent(Fragment1.this.getActivity(),Karta.class);
    – qwen
    4 мар 2014 в 20:58
  • 1
    Можно просто: Intent intent = new Intent(getActivity(),Karta.class);
    – Opalosolo
    5 мар 2014 в 8:24

1 ответ 1

2

Смысл в том, что this во фрагменте

public void onClick(View v) { Intent intent = new Intent (this,Karta.class); startActivity(intent); }

ссылается на интерфейс OnClickListener, в котором нет реализации класса Context поэтому правильным будет уже приведенный в комментарии ответ

Intent intent = new Intent(Fragment1.this.getActivity(),Karta.class);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.