I just following a tutorial to start a basic intent, and I get an error. Doing exact the same as the tutorial, created an empty Activity and just passed as argument to the intent. What is the problem?
Using a video tutorial: Lynda.com - Developing Android Apps Essential Training (2015)
The error you've got in IDE usually happens when you try to use Activity Context (keyword this) inside some callback, listener or anonymous function. In such situation this does not refer to the Context of the Activity.
That's why solution provided by #Vishal Patoliya should fix your problem because you're explicitly referring to the Context of the concrete Activity as follows:
Intent intent = new Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
Try this
Intent intent = new Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
startActivity(intent);
In case of Activtiy, you are going to another activity:--
Intent intent = new
Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
startActivity(intent);
If you are passing intent from a fragment to open a activity then
Intent intent = new Intent(getActivity(),ItemUserSettingRattingActivity.class);
startActivity(intent);
Have you already created a class named ItemUserSettingRattingActivity.java? maybe you get an error it's because the intent you created didn't detect the class.
Related
I am in a non-activity class. In this place I want to run the activity from a 3rd party library which I attached to my project. I have written some code which I thought will do it:
Intent intent = new Intent();
intent.putExtra(THIRD_PARTY_ACTIVITY.REPORT_EXTRA, parts.toString());
Activity activity = new THIRD_PARTY_ACTIVITY();
activity.startActivity(intent);
This code compiles successful but when I execute it, I get the error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
How can I run activity from a 3rd party library?
You're doing it wrong. You NEVER create an Activity with new. It won't initialize correctly. Instead, you create an Intent to launch that Activity and call context.startActivity() to create and launch it.
void launchThirdPartyActivity(Context context) {
Intent intent = new Intent(context, THIRD_PARTY_ACTIVITY.class);
context.startActivity(intent);
}
That should work if the activity is in a library. If you're trying to launch an Activity in another app on the device, you'd use one of the other Intent constructors (which one depends on how/what you're trying to launch- a specific activity in a specific app? An activity that can perform an action (like share or view) on a specific data type? Something else?)
In your library you can do this:
Intent intent = new Intent();
// Here you need to set the ACTION or the COMPONENT in the Intent
// so that Android knows which Activity you want to start
// Also, you need to set FLAG_ACTIVITY_NEW_TASK because you aren't calling
// startActivity() on an Activity Context
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(THIRD_PARTY_ACTIVITY.REPORT_EXTRA, parts.toString());
Application.getApplicationContext().startActivity(intent);
I'm trying to modify vuforia's sample videoplayback application in Android Studio. In that sample app, video will be played whenever targeted image is focused. I try to modify like I want to go next activity when the video is played fully. So I just used Intent, but it will throw and error as "Cannot resolve constructor Intent". Here I have attached screenshot of the error.Can you guys please help me to resolve this issue. Thanks in advance.
Screenshot of the error VideoPlayerHelper imports
Intent intent = new Intent(this, SearchResult.class);
"this" should be context or child's context.
Intent intent = new Intent(type of context, SearchResult.class);
This is just a class you need to pass the context to the class(passing through constructor would be better). Then do following:
Intent intent = new Intent(context,SearchResult.class);
and better way is create listeners(interface) and call appropriate method on activity using them.
Try Intent intent = new Intent(VideoPlayerHelper.this,SearchResult.class);
Just change u're coding
Intent intent = new Intent(this,SearchResult.class);
to
Intent intent = new Intent(getIntent(),SearchResult.class);
How can i launch an AndroidAnnotations Activity_ inside of an App (main)
since external Activity (another App).
this is my current code:
Intent codeScannerActivity = new Intent(PACKAGE, CODE_SCANNER_ACTIVITY);
codeScannerActivity.putExtra("codeScannerType", CameraUtils.CODE_SCANNER_SINGLE);
startActivityForResult(codeScannerActivity, Core.ActivityResult.RequestCode.CODE_SCANNER);
where PACKAGE = "main.app.package"
and CODE_SCANNER_ACTIVITY = PACKAGE + ".activity.MyActivity_"
but logs throws:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=main.app.package dat=main.app.package.activity.MyActivity_ (has extras) }
Activity is defined in the Manifest's Main App with the Class "etc.MyActivity_".
You are constructing the Intent incorrectly. For the constructor you are using, the first parameter is interpreted as an "action" and the second as a URI. The error says that there is no activity which can respond to the action "main.app.package" and the URI "main.app.package.activity.MyActivity_".
To fix the problem, first read Starting Another Activity and the Intent javadocs from the Android Developer site. Especially look at the documentation for the available constructors. There might be one more appropriate for your purposes than the one you are trying to use. The Intent documentation has a list of standard Activity actions. If you want to start a specific activity, you should use Intent (Context packageContext, Class<?> cls):
Intent intent = new Intent(this, main.app.package.activity.MyActivity_.class);
I was creating wrong the Intent, this is the right way:
Intent codeScannerActivity = new Intent();
codeScannerActivity.setComponent(new ComponentName(PACKAGE, CODE_SCANNER_ACTIVITY));
codeScannerActivity.putExtra("codeScannerType", CameraUtils.CODE_SCANNER_SINGLE);
startActivityForResult(codeScannerActivity, Core.ActivityResult.RequestCode.CODE_SCANNER);
I want to call a class of another project. I added it in the build path and also declared that class in the manifest file but when I call it gives me a no class found error. I am calling it from intent.
Intent intent = new Intent(getApplicationContext(), org.coolreader.CoolReader.class);
intent.putExtra("path", adapter.getItem(position).getPath());
startActivity(intent);
If you want to launch CoolReader from your program than look at this answer:
Intent coolReaderIntent = getPackageManager().getLaunchIntentForPackage("org.coolreader.CoolReader");
coolReaderIntent.putExtra("path", adapter.getItem(position).getPath());
startActivity(coolReaderIntent);
Of course, CoolReader (program) should be installed on the device.
I Mean that i want to use one activity into another activity, Like class using create instance of that class. Is it Possible?
Well, I think you should use Intents to call an activity from another activity.
Call this from your Activity:
Intent in = new Intent(getApplicationContext(), NextActivity.class);
startActivity(in);
you can do it only by saying startActivity(), no other go. you can't make an instance of Activity because , an Activity gets created when its onCreate() method gets called, but when you say new MyActivity() its default constructor is called and not its onCreate() method (which Android OS will not accept). so always say startActivity() or startActivityForResult() which are handled by android OS
Write this code from where you want to run activity
Intent intent = new Intent(current_Activity_name.this,New_Activity_name.class);
startActivity(intent);
And add the following code into manifest file
<activity android:name=".New_activity_name" />
Well, since an Activity is a displayable-window, the appropriate concept would be that one Activity can be "launched" from another. This is how you achieve that:
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
CurrentActivity.this.startActivity(i);
This code snippet can launch NewActivity from any point in the CurrentActivity code, for example, an 'OnClickListener'.
Yes, it is possible. This is achieved through Intents.
Intent intent = new Intent(this.getApplication(), TARGET_ACTIVITY_NAME.class);
//To add data use intent.putExtra(NAME,VALUE);
intent.setData(data.getData());
try
{
startActivity(intent); // This ll launch the TARGET_ACTIVITY_NAME
}
catch(Exception e)
{
}
For more information refer this link.
Shash