When I run the app there is an Fatal exception error
android.content.ActivityNotFoundException: No Activity found to handle Intent
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get butten
Button bt= (Button) findViewById(R.id.bt);
// set a monitor
bt.setOnClickListener(new MyListener());
}
class MyListener implements View.OnClickListener{
public void onClick(View v) {
EditText et = (EditText) findViewById(R.id.et);
String number = et.getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel"+number));
startActivity(intent);
}
}
}
Very simple, your device does not have an app that handles phone calls. It is probably a tablet. When coding, you have to code for such errors, by using try...catch.
You have to specify the Intent by using a context & class name.
Since you haven't provide your Manifest file the easiest way to avoid the error is changing the code as follows
Intent intent = new Intent(this,MainActivity.class);
More details from android Documentation
android.content.Intent public Intent(android.content.Context packageContext,
java.lang.Class<?> cls)
Create an intent for a specific component. All other fields (action, data, type, class) are
null, though they can be modified later with explicit calls. This
provides a convenient way to create an intent that is intended to
execute a hard-coded class name, rather than relying on the system to
find an appropriate class for you; see setComponent for more
information on the repercussions of this.
Parameters: packageContext - A Context of the application package
implementing this class. cls - The component class that is to be
used for the intent.
Related
I'm new to android studio and i have a problem when I'm trying to jump to a new activity, so when the line is:
public class signup_activity extends AppCompatActivity {
ImageButton logupButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup_activity);
logupButton = findViewById(R.id.signuparrow);
logupButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, signup_activity.class);
startActivity(intent);
}
});
}}
I get the error:
'com.example.myapplication.MainActivity' is not an enclosing class
and i so a couple of of people advising to chane the intent to this instead of MainActivity.this
but when im changing to this i get the error:
Cannot resolve constructor 'intent'
Intent intent = new Intent(MainActivity.this, signup_activity.class);
Several things:
First, the first param of Intent() constructor is a context. Since you are on signup_activity you need to do signup_activity.this to use it as a context.
I'd assume you want to go to MainActivity, so your second param should be MainActivity.class. It seems you got the order altered there.
you are in signup_activity and when use Intent in fisrt part you should call the current contex to jump to other activity.
so you should replace
Intent intent = new Intent(signup_activity.this, MainActivity.class);
if you want to jump to signup_activity you can call intent from MainActivity.
I'm trying to send an object created from the Spotify API (a SpotifyAppRemote instance) from my MainActivity class to a BackgroundService (implemented as IntentService).
Since I can't use parcelable to send my object as I have no control over the API I was trying to use GSON to send it via the putExtra method from my intent like this:
intent.putExtra("spotifyRemote", gson.toJson(mSpotifyAppRemote, SpotifyAppRemote.class));
However, on runtime I get an error:
java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: com.spotify.protocol.types.ImageUri. Forgot to register a type adapter?
Is there another way to send this object to my Service? Looking for this error message didn't really help.
Here's the code from my MainActivity class:
#Override
public void onConnected(SpotifyAppRemote spotifyAppRemote) {
mSpotifyAppRemote = spotifyAppRemote;
getCurrentTrack();
// Now you can start interacting with App Remote
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
infoText.setText("Successfully started!");
counter = 1;
numSongs = Integer.parseInt(mEdit.getText().toString());
PlayerApi playerApi = mSpotifyAppRemote.getPlayerApi();
playerApi.seekTo(0);
playerApi.resume();
Intent intent = new Intent(MainActivity.this, BackgroundService.class);
intent.putExtra("counter", counter);
intent.putExtra("numSongs", numSongs);
intent.putExtra("firstTrack", gson.toJson(curTrack, Track.class));
intent.putExtra("spotifyRemote", gson.toJson(mSpotifyAppRemote, SpotifyAppRemote.class));
startService(intent);
}
});
}
I worked around this by declaring a static variable in my Service and setting the variable in my MainActivity like this:
BackgroundService.mSpotifyAppRemote = mSpotifyAppRemote;
Declaration in my Service:
public static SpotifyAppRemote mSpotifyAppRemote = null;
I have successfully used androidannotations #Extra to decode an intent and get the sent message as this snippet demonstrates:
#Extra(MyActivity.MESSAGE)
String intentMessage;
#ViewById(displayMessage)
TextView textView;
#AfterViews
protected void init() {
textView.setText(intentMessage);
}
I'm missing how, if possible, to create the intent in the first place using annotations. e.g replace the following
Intent intent = new Intent(this,DisplayMessageActivity_.class);
intent.putExtra(MESSAGE, s);
startActivity(intent);
with something. Is this possible? (I'm totally new to all this so probably am missing something terribly obvious)
SOLUTION:
DisplayMessageActivity_.intent(this).intentMessage(s).start();
Where intentMessage is the name of the extra field.
Yes, you can use the following:
// Starting the activity
MyListActivity_.intent(context).start();
// Building an intent from the activity
Intent intent = MyListActivity_.intent(context).get();
// You can provide flags
MyListActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start();
// You can even provide extras defined with #Extra in the activity
MyListActivity_.intent(context).myDateExtra(someDate).start();
// startActivityForResult() equivalent:
MyListActivity_.intent(context).startForResult();
Source: https://github.com/excilys/androidannotations/wiki/HowItWorks
Solution. Finally saw the pattern on how it works. Thanks.
DisplayMessageActivity_.intent(this).intentMessage(s).start();
where intentMessage is the #Extra defined in the activity to be started e.g
#EActivity(R.layout.activity_display)
public class DisplayMessageActivity extends Activity {
public static final String MESSAGE = "net.richardriley.MyFirst.MESSAGE";
#Extra(MESSAGE)
String intentMessage;
#ViewById(displayMessage)
TextView textView;
#AfterViews
protected void init() {
textView.setText(intentMessage);
}
}
I know this is a late answer but I have developed a library that does exactly this. It uses annotations to generate the intent code.
Check it out in here: https://github.com/kostasdrakonakis/android_navigator
i've tried to start an activity with my own function, but it doesn't work (think its a logic fault ;) )
Please have a look at:
Intent adder = new Add().execute(this, Add.class, "Test:","Add the test");
startActivity(adder);
This is the Activity which i want to start:
public class Add extends Activity {
public Intent execute(Context context, Class<?> cls, String addText, String buttonText) {
TextView addStr = (TextView) findViewById(R.id.addtext);
addStr.setText(addText);
Button addBtn = (Button) findViewById(R.id.addbtn);
addBtn.setText(buttonText);
return new Intent(context, cls);
}
}
What's wrong?
You should take a look at this Android developer guide, which talks about Activity and Intent. I think you will see that they are used in a completely different way than you are trying to use them.
I have a beginners problem. Here is my situation:
I want to start a new activity from the main activity. The code to launch the new activity is found in a separate class file. I seem to be passing the wrong arguments and I am ending up in a nullpointerexception when trying to launch the new activity. The new activity launches fine when I place the code in the main activity class file, therefore the second activity and the manifest are fine. Here is a sample of my code:
In my main activity class where I instanciate the second class (THIS IS MY MAIN ACTIVITY. I OMITTED THE REST BECAUSE I DO NOT THINK IT IS RELATED TO THE PROBLEM):
Tester mytest = new Tester();
mytest.test(this);
In my second class file (THIS IS NOT AN ACTIVITY; IT IS A CLASS THAT IS INSTANTIATED IN THE ACTIVITY):
public class Tester extends Activity {
Intent myIntent;
public void test (Context context) {
myIntent = new Intent (Intent.ACTION_VIEW);
myIntent.setClass(context, newActivity.class);
thebutton.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
startActivity(myIntent);
}
}
):}
When I perform the click I receive a nullpointerexception at startactivity. Can anyone enlighten me on this please?I am sure that I am wrongly using the context.
Activities are started with Intents. Please read the Android Application Fundamentals first and try the Hello World app :)
I understood that you will use your separate Tester class at all cost ;) so I'm trying to adapt and help you out there.
First of all, don't let your class inherit from Activity. This won't help you, cause this calls will probably not have any valid context. Activity somehow implements the template pattern, providing you key method like onCreate(...), onPause(...) etc and is instantiated by the Android OS.
If you still want to use the class, you have to pass in the context. Probably you're aiming for some MVC/MVP pattern structure, anyway.
public class Tester {
private Context context;
public Tester(Context context){
this.context = context;
}
public void test () {
final Intent myIntent = new Intent(context, NewActivity.class);
//guess this comes from somewhere, hope through a findViewById method
thebutton.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
context.startActivity(myIntent);
}
}
)};
}
}
This would be a proposed solution from my side. A problem I still see here is on how you retrieve the button in that test() method. In order to have that work properly you have to retrieve it from some View class (with view.findViewByid(R.id.myButton)) or to create it dynamically and associate it with the view during the onCreate(...) of your Activity (probably using an Inflater).