Nicholas Biantoro
Nicholas Biantoro

Reputation: 136

How to fix "error: cannot find symbol method startActivity(Intent)" in a class of Listener?

I can make an Intent to open other Activity with writing the code in MainActivity.java. Then I try to make an Intent using a class and called it in MainActivity.java. But it becomes error. How to solve this problem?

When I write startActivity(numberIntent); in MainActivity.java there is no error but when I move this line of code to NumbersClickListener.java

Errors come:

  1. error: cannot find symbol method startActivity(Intent)
  2. error: not an enclosing class: MainActivity

This my code In MainActivity.java

package com.example.android.*****;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override

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

    setContentView(R.layout.activity_main);

    NumbersClickListener clickListener = new NumbersClickListener();

    TextView numbers = (TextView)findViewById(R.id.numbers);

    numbers.setOnClickListener(clickListener);
}

in NumbersClickListener.java

package com.example.android.*****;

import android.content.Intent;
import android.view.View;
android.widget.Toast first
import android.widget.Toast;

OnClickListener should be written in capital letter
public class NumbersClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {//.makeText(view.getContext(),
    "open the list of numbers", Toast.LENGTH_SHORT).show();
    Intent numberIntent = new Intent(MainActivity.this, 
NumbersActivity.class);
    startActivity(numberIntent);
}
}    

Upvotes: 1

Views: 3036

Answers (5)

Raed
Raed

Reputation: 904

inside your NumberClickListener class you can do the following
Context context = view.getContext(); Intent numberIntent = new Intent (context, NumberActivity.class); context.startActivity(numberIntent);

By using this code you can use your NumberClickListener with any other activity. happy codding :)

Upvotes: 0

raghu
raghu

Reputation: 841

To startActivity you need Context.

It will be like this context.startActivity() In MainActivity it is not giving error because Activity internally extends Context. NumbersClickListener is not extended Context.

So, you can start activity using View context

Replace startActivity(numberIntent) with

view.getContext().startActivity(numberIntent);

Upvotes: 0

okcomputer_kid
okcomputer_kid

Reputation: 511

In place MainActivity.this, use its context.

Intent numberIntent = new Intent(context, NumbersActivity.class);
startActivity(numberIntent);

Notice the changes I have made

MainActivity.java

package com.example.android.*****;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override

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

    setContentView(R.layout.activity_main);

    NumbersClickListener clickListener = new NumbersClickListener(MainActivity.this);   // Context while creating ClickListener Object

    TextView numbers = (TextView)findViewById(R.id.numbers);

    numbers.setOnClickListener(clickListener);
}

NumbersClickListener.java

package com.example.android.*****;

import android.content.Intent;
import android.view.View;
import android.widget.Toast;

public class NumbersClickListener implements View.OnClickListener {
Context context; 
NumbersClickListener(Context c){
    this.context = c;
} 
@Override
public void onClick(View view) {
Intent numberIntent = new Intent(context, NumbersActivity.class);
startActivity(numberIntent);
}
}    

Upvotes: 0

Yat Pan Ng
Yat Pan Ng

Reputation: 1

You are defining NumbersClickListener in a separate java file. It has no way compiler will know that when u call startActivity you are referring to the Activity.startActivity

Unless you have a deeper purpose for NumbersClickListener.java, simply do inline declaration of View.Listener will do

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

setContentView(R.layout.activity_main);

NumbersClickListener clickListener = new NumbersClickListener();

TextView numbers = (TextView)findViewById(R.id.numbers);

numbers.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
        Intent numberIntent = new Intent(MainActivity.this,NumbersActivity.class);
        startActivity(numberIntent);
}
});
}

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69689

error: cannot find symbol method startActivity(Intent)” in a class of Listener?

Because if startActivity(Intent) is a method of activity and its required call from context

If you want call startActivity(Intent) outside activity you need to use Context.startActivity(numberIntent);

Use this

view.getContext().startActivity(numberIntent);

instead of this

startActivity(numberIntent);

SAMPLE CODE

public class NumbersClickListener implements View.OnClickListener {
    @Override
    public void onClick(View view) {
        Intent numberIntent = new Intent(view.getContext(),
                NumbersActivity.class);
        view.getContext().startActivity(numberIntent);
    }
}

Upvotes: 2

Related Questions