Well, I'm trying to create an intent on a "login.java" the code is :
Button btEntrar = (Button) findViewById(R.id.btnSingIn);
btEntrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i;
i = new Intent(this, MainActivity.class);
startActivity(i);
}
});
But it says that I can't go to the other activity saying this :
Error:(24, 21) error: no suitable constructor found for Intent(,Class)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; cannot be converted to String)
constructor Intent.Intent(Context,Class) is not applicable
(argument mismatch; cannot be converted to Context)
and...
Error:Execution failed for task ':app:compileDebugJava'.
Compilation failed; see the compiler error output for details.
Just a few lines to explain the reason why this does not work in:
i = new Intent(this, MainActivity.class)
The intent is created inside another class, here an anonymous inner class OnClickListener. Thus this does not refer the instance of your Activity (or Context) as intended but the instance of your anonymous inner class OnClickListener.
So you should provide the correct context of your class.
i = new Intent(YourClassName.this, MainActivity.class)
use
if you want to send it from login.java to mainactivity.class use
Intent intent=new Intent(login.this,Mainactivity.class);
startActivity(intent);
updated code in to your activity
Button btEntrar = (Button) findViewById(R.id.btnSingIn);
btEntrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i;
i = new Intent(login.this, MainActivity.class);
startActivity(i);
}
});
try using
Intent i = new Intent(login.this, mainActivity.class);
hope this helps
Did you add manifest.xml or try this code ?
Intent i = new Intent(login.this,mainActivity.class);
Related
I am trying this intent so I can navigate from one activity to another activity but I'm getting this error `
final Button btnAdd = findViewById(R.id.addEmp);
btnAdd.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(v.getId() == R.id.addEmp){
Intent intent = new
Intent(getCallingActivity(),AddEmployee.class);
startActivity(intent);
}
}
});`
there is a red line under
(getCallingActivity(),AddEmployee.class);
there error says
cannot resolve constructor
Is anything wrong with this
(getCallingActivity(),AddEmployee.class);
statement?
You need to change your line from
Intent intent = new Intent(getCallingActivity(),AddEmployee.class);
to this:
Intent intent = new Intent(YourActivityName.this,AddEmployee.class);
OR
Intent intent = new Intent(getApplicationContext(),AddEmployee.class);
Edit
Whats wrong with getCallingActivty()?
getCallingActivity() returns ComponentName while intent constructor requires Context as a first argument.
Hope this will work
I have an activity, let's call it MainActivity which has an SomeAdapter.
In the adapter's code I have
#Override
public void onBindViewHolder(#NonNull OptionViewHolder holder, final int position) {
final Option o = values.get(position);
holder.textView.setText(o.getOption());
holder.foreGround.setBackgroundColor(o.getOptionLayout().getBackGroundColor());
holder.editOptionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("option",o);
context.startActivityForResult(intent,1);
}
});
}
When actually clicking the editOptionButton, I get the following stack trace
Process: com.company.app, PID: 20916
android.content.ActivityNotFoundException: No Activity found to handle Intent { (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2007)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
at android.app.Activity.startActivityForResult(Activity.java:4586)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult(Activity.java:4544)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
at com.company.app.adapters.OptionsAdapter$1.onClick(OptionsAdapter.java:86)
the context that calls startActivityForResult is the MainActivity and is not null and alive (visible) at the time of calling because the adapter runs in it.
Therefor, I have no idea why this error pops up nor what I could do about it. Do any of you may know why or have experienced it before?
You have to include the Activity name like this
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("option",o);
context.startActivityForResult(intent,1);
First
you are initlizing the intent in wrong way, you need init it as below:
Intent intent = new Intent(context, SecondActivity.class);
Second
If the result do not return to your MainActivity then you need to cast the context to activity before starting the second activity as:
((Activity) context).startActivityForResult(intent,1);
This means that intent doesn't know where to go. So give the context and the activity name where u want to go.
Intent intent = new Intent(getActivity(), SecondActivity.class);
intent.putExtra("Key",value);
startActvity(intent);
I am getting an exception at second startActivity(i) statement. My first startActivity(i) statement works fine.
public void onBClick(View v){
EditText a = (EditText) findViewById(R.id.TFUsername);
String str = a.getText().toString();
if(v.getId() == R.id.BLogin){
Intent i = new Intent(MainActivity.this, new_activ_java.class);
i.putExtra("username",str+',');
startActivity(i);
} else if(v.getId() == R.id.BforSignUp){
Intent i = new Intent(MainActivity.this, Signup.class);
startActivity(i); // I am getting an exception here
}
}
Please make sure Signup is an Activity declared in the Android Manifest. As a convention, append Activity at the end of the class name to indicate that it is indeed an Activity, for instance, SignUpActivity.
Make sure that the new activity you are trying to start is listed in Android Manifest.xml file.
I was having some problem when trying to move to a new intent in onActivityResult. Here is the code:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
if (scanningResult != null) {
barcode = scanningResult.getContents();
new GetBookByBarcode(
new GetBookByBarcode.OnRoutineFinished() {
public void onFinish() {
String bookID = GetBookByBarcode.bookID;
if (bookID.equals("")) {
Toast toast = Toast.makeText(getApplicationContext(),
"Book not found, please try another barcode.",
Toast.LENGTH_SHORT);
toast.show();
} else {
intent = new Intent(context, ReserveBook.class);
intent.putExtra("BookID", bookID);
startActivity(intent);
BarcodeScan.this.finish();
}
}
}).execute();
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
Firstly, I will perform a barcode scan. When onActivityResult, I will execute the AsyncTask class. When the AsyncTask class onFinish, I then move to a new intent. However, I am getting error message:
Cannot refer to non-final variable intent inside inner class defined in different method.
at the intent inside the else statement. Any ideas? Thanks in advance.
Just declare a new Intent variable. You're creating a new Intent anyways, so there's no point trying to reference the non-final parameter from onActivityResult.
The reason you get this message from the compiler is because only final variables (and effectively final variables in Java 8) can be referenced inside an anonymous inner class like that.
Instead of doing
intent = new Intent(context, ReserveBook.class);
intent.putExtra("BookID", bookID);
startActivity(intent);
BarcodeScan.this.finish();
Try doing
Intent newIntent = new Intent(context, ReserveBook.class);
newIntent.putExtra("BookID", bookID);
startActivity(newIntent);
BarcodeScan.this.finish();
Because as of now, you are trying to access the parameter from onActivityResult which gives you the error.
Every time I try to start new activity, an error occurs (Java null pointer exception) and the app fc's: I tried every single way and it just doesn't work! Here's an example for the code : MainAct is the first activity and NewAct is the activity to launch...
1- manifest:
<activity
android:name="com.example.app.NewAct"
android:label="#string/title_activity_newact" >
<intent-filter>
<action android:name="android.intent.action.NewAct" />
</intent-filter>
</activity>
2-MainAct:
Button bt;
bt = (Button) findViewById (R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i= new Intent ("com.example.app.NewAct");
startActivity(i);
}
});
I also tried other forms of intent such as
Intent i= new Intent ("android.intent.action.NewAct");
Intent i= new Intent (this, NewAct.class);
Intent i= new Intent (view.getContext(), StudentInfoActivity.class);
Don't get the context from the "view" get the context from the Activity. Wherever you need a context use getApplicationContext(), getActivity(), or this. But if you are inside of an Anonymous Inner Class, such as a View.OnClickListener() (as you are in the code you posted) you cannot use this because this will be referring to the Anonymous Inner Class that you are in.
A common practice would be storing the Context away in a private member variable, do this inside of onCreate()...
Ex.
....
private Context mContext;
...
#Override
onCreate(Bundle savedInstanceState){
mContext = this;
}
^By doing this you have a context variable to use freely throughout your activity without having to worry.
Try something like this to start your new Activity, after you have your Context variable:
Intent i = new Intent(mContext,
NewAct.class);
startActivity(i);
^You can use this code from within your OnClickListener().
Intent i = new Intent(MainAct.this, NewAct.class);
startActivity(i);
I think the problem is not with your Intent.please check your onCreate() of the second Activity[NewAct.java].