Vidit Shah Vidit Shah - 2 months ago 18
Android Question

intent not getting passed via OnClickListener

Problem:
I would like my Intent to take me to new activity but showing me Constructor error.

package com.example.vidit.books;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

final EditText query = (EditText) findViewById(R.id.query);
Button submit= (Button) findViewById(R.id.submit);

submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent= new Intent(MainActivity.class,Request.class);
intent.putExtra ( "text", query.getText().toString() );
startActivity(intent);

}
});
}
}


The Intent where transition happens is Underlined in Red.
Request for help

Answer

Change this line :

                Intent intent= new Intent(MainActivity.class,Request.class);
                intent.putExtra ( "text", query.getText().toString() );
                startActivity(intent);

to this :

                Intent intent= new Intent(MainActivity.this,Request.class);
                intent.putExtra ( "text", query.getText().toString() );
                startActivity(intent);
Comments