简体   繁体   中英

Using implicit intent to open activity

I'm getting the below error for NumbersClickListener class. I'm trying to use a intent to open NumbersActivity

All other activity can be opened using intents in the MainActivity . What is wrong with the code.

The implementation using NumbersClickListener class has some issue.

  Error:(14, 20) error: no suitable constructor found for Intent(NumbersClickListener,Class<NumbersActivity>) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; NumbersClickListener cannot be converted to String) constructor Intent.Intent(Context,Class<?>) is not applicable (argument mismatch; NumbersClickListener cannot be converted to Context) 
Error:(15, 9) error: cannot find symbol method startActivity(Intent)
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

NumbersClickListener class :

package com.example.android.miwok;

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


public class NumbersClickListener implements View.OnClickListener
{
    @Override
    public void onClick (View view)
    {
        Toast.makeText(view.getContext(),"Opening Numbers Category",Toast.LENGTH_SHORT).show();
        Intent i = new Intent(this, NumbersActivity.class);
        startActivity(i);

    }
}

The error is with these line of codes : Iv tried MainActivity.this also

Intent i = new Intent(this, NumbersActivity.class);
        startActivity(i);

Main Activity code

package com.example.android.miwok;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import static android.R.id.message;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
import static com.example.android.miwok.R.id.phrases;

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);

        NumbersClickListener clickListener = new NumbersClickListener();
        //Find views that shows number category
        TextView numbers = (TextView) findViewById(R.id.numbers);
        numbers.setOnClickListener(clickListener);



        TextView phrases = (TextView) findViewById(R.id.phrases);
        phrases.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, PhrasesActivity.class);
                startActivity(i);
            }
        });

        TextView family = (TextView) findViewById(R.id.family);
        family.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, FamilyActivity.class);
                startActivity(i);
            }
        });
    }

    public void openColorsList(View view) {
         // setContentView(R.layout.activity_colors);   //we can use this to colors xml in main actitvity
        Intent i = new Intent(this, ColorsActivity.class);
        startActivity(i);

    }
}

Problem is Intent i = new Intent(this, NumbersActivity.class); "this" is present of NumbersClickListener , to start activity you need context. So change it to :

Intent i = new Intent(view.getContext(), NumbersActivity.class);

or you need to pass context to NumbersClickListener to start an activity

Error:(14, 20) error: no suitable constructor found for Intent(NumbersClickListener,Class<NumbersActivity>)

See , your error says it all. You need first argument as context and second as the class (activity) name you want to open .

All other activity can be opened using intents in the MainActivity

this is because your MainActivity is extended from Activity class and can refer to context using this but not all plain classes which are not having any reference to Activity context .

What you can do is

Create a reference to context ,something like

Context context = getContext();

and use this context instead of this

getContext()

can be a public method in your MainActivity class that returns this .

Now startActivity(i) is red.

Do context.startActivity(your_intent_here);

I Hope It helped :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM