Cannot resolve method startActivity()

后端 未结 4 710
天命终不由人
天命终不由人 2021-02-07 09:08

I\'m new to the android development and having a bit of a problem changing activities. I am trying to change activities from within a method but I am getting the error can

相关标签:
4条回答
  • 2021-02-07 09:48

    You should use the context of your adapter:

    public void open301(View view) {
      Intent openThree = new Intent(context,ThreeZeroOne.class);
      context.startActivity(openThree);
    }
    
    0 讨论(0)
  • 2021-02-07 09:57

    To start a new activity you will need a context to start from, and your current activity "BaseAdapter" is not a Context, luckly every view has a Context, so you can do like this:

    public void open301(View view) {
        Intent openThree = new Intent(view.getContext(), ThreeZeroOne.class);
        view.getContext().startActivity(openThree);
    }
    
    0 讨论(0)
  • 2021-02-07 09:59

    First you should get your Context:

    private Context context;
    
    public CustomAdapter(Context context) {
        this.context = context;
    }
    

    And then:

    context.startActivity(openThree);
    
    0 讨论(0)
  • 2021-02-07 10:00

    you can also pass messages

     Intent i = new Intent( getContext(),Chat.class);
                i.putExtra("id",user.id);
                i.putExtra("name",user.name);
                getContext().startActivity(i);
    
    0 讨论(0)
提交回复
热议问题