android simulator crashes when using intent - java

I'm new to android studio, I'm using java to write my application.
I found that when I use intent to make the page jump from a page call PhotosActivity to another page call AndroidTabLayoutActivity was failed,I have no idea with what is going on.The android simulator does't give me any error massage and it just close the application automatically.
the code of calling intent:
if (!error) {
Toast.makeText(getApplicationContext(), "Offer successfully inserted.", Toast.LENGTH_LONG).show();
// Jump to the AndroidTabLayoutActivity page
Intent intent = new Intent(
PhotosActivity.this,
AndroidTabLayoutActivity.class);
startActivity(intent);
finish();
}
The code of AndroidTabLayoutActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
//there is still some others tab in here
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
}

You have using getApplicationContext() in this toast method
Replace with activity context, i am sure your code is working fine
Toast.makeText(getApplicationContext(), "Offer successfully inserted.",
Toast.LENGTH_LONG).show();
Use like this
Toast.makeText(PhotosActivity.this, "Offer successfully inserted.",
Toast.LENGTH_LONG).show();

As there is no any particular error log. We have to backtrack all the possibilities .
First of all replace your toast with this.
Toast.makeText(getApplicationContext(), "Offer successfully inserted.", Toast.LENGTH_LONG).show();
After that ensure that you've declared your AndroidTabLayoutActivity in your AndroidManifest.xml file
In your AndroidTabLayoutActivity Cooment out all your code and check its working ? if yes than problem is in AndroidTabLayoutActivity. reply me with whats working for you

Related

android studio AsyncTask not intenting and context.finish crashes

i am new to android,and trying to use AsyncTask to get a connection to mysql from the server
protected void onPostExecute(String result) {
if(result.equalsIgnoreCase("true"))
{
Intent intent = new Intent(context,profile.class);
//not usefull
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
((Activity)context).finish();
Toast.makeText(context, "done", Toast.LENGTH_LONG).show();
}else //..... the rest of the code .....//
}
everything works fine but when the result is true it doesn't intent to the new activity
and by using ((Activity)context).finish(); it will crash
the app always toast done
context is defined Context context , i got its value from MainActivity by typing this
Using this is reference to the activity, I think you should be referencing the application context to start an activity.
if(result.equalsIgnoreCase("true"))
{
Intent intent = new Intent(context.getApplicationContext(),profile.class);
context.getApplicationContext().startActivity(intent);
((YourActivityName)context).finish();
Toast.makeText(context, "done", Toast.LENGTH_LONG).show();
}else //..... the rest of the code .....//

how to start new activity for share action from CardView

I am trying to give the user the option to share cardview content.
i use this code in the main activity and it is working fine:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "some text to share";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
but when i use the same code from recyclerview i get error and the application crashed.
also i modified the code by adding FLAG and still not working
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = " teext to be shared";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(sharingIntent, "Share via"));
iam getting this error:
Calling startActivity() from outside of an Activity context requires the
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
holder.iconFavorites_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// iam calling the code here and this block of code is located inside adapter class for recycleview .... the code located inside onBindViewHolder mithod
}
});
any body have solution for this problem ? if any information needed just tell me
In order to start an activity outside of another activity, you must use the FLAG_ACTIVITY_NEW_TASK flag for the intent (as you posted).
You would set this via sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Starting react-native activity from java module (android)

I see that you can bring your activity form the background to the foreground in android using the following lines
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Now the problem is I don't know how to get the name of the react activity to work - When I try to compile I get
error: cannot find symbol Intent i = new Intent(mReactContext,
MainActivity.class);
symbol: class MainActivity
I'm not very experienced with java - so this is part of my problem, I tried using getCurrentActivity() - but this doesn't seem to work either.
Anyone has an idea?
Thanks!
If you have access to reactContext (I see you have) you can try this:
Context context = getReactApplicationContext();
String pn = context.getApplicationContext().getPackageName();
Intent li = context.getPackageManager().getLaunchIntentForPackage(pn);
context.startActivity(li);
String pn is just a package name (e.g. "com.yourapp"), so you can skip getPackageName() and just pass your package name to getLaunchIntentForPackager(). You can check this solution on react-native-system-notification
You should get reference of your first activity. Then Create your intent and start activity. Run the code below somewhere in your code(for example in a react method which triggers an activity)
Activity currentActivity = getCurrentActivity();
Intent intent = new Intent(name of your activity);
currentActivity.startActivityForResult(intent,FLAG_ACTIVITY_NEW_TASK
);
// create onActivityResult method to process your task according to result
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FLAG_ACTIVITY_NEW_TASK ) {
// do your job
}
}
It seems you use MainActivity.class as parameter in intent. I'm not an expert but it seems wrong. In a react-native project mainactivity will be triggered automatically and index.android.js will be called. You can use an activity you create as well you don't have to use native android activities like Intent.ACTION_GET_CONTENT which can be used to get information of an image from gallery.

Sending messages to multiple contacts using Intent

I'm trying to send messages through in built sms app through Intent. Its working fine. Here is my code
public class Main_Act extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startBtn = (Button) findViewById(R.id.button);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(sendSMS()) {
Intent intent = new Intent(Main_Act.this, Sample.class);
startActivity(intent);
}
}
});
}
protected boolean sendSMS() {
ArrayList<String> nums = new ArrayList<String>();
nums.add("111111111");
nums.add("222222222");
Log.i("Send SMS", "");
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address" ,nums);
smsIntent.putExtra("sms_body" , "Test ");
try {
startActivity(smsIntent);
finish();
return true;
}
catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Main_Act.this,
"SMS faild, please try again later.", Toast.LENGTH_SHORT).show();
return false;
}
}
}
But the problem is it gets navigated to another activity without clicking send button in sms application. It should goto another activity only after clicking the send button in messaging app. Can anyone help me with this problem, Thanks in advance.
Let's clear out a slight misunderstanding in your code:
You should not try to start both intents in the same part/run of the code as you do here.
A startActivity will not execute directly going to the activity and then return to the same place in the code when activity execution finishes. In stead it asynchronously queues the intent for execution. Then your code queues another intent for execution. After the current code finishes (in this case when the button onClick() method ends) Android queue mgmt can start picking off the queue. Probably the first intent is executed shortly and then directly overrun by an immediate execution of the second.
So what happens in summary is that you first add one intent to the queue in sendSMS and then add intent 2 to the queue in onClick, before leaving. Now both the intents are executed.
What you need to do is to change the sendSMS code to something like:
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address" ,nums);
smsIntent.putExtra("sms_body" , "Test ");
// To force the SMS app to return immediately after sent SMS
smsIntent.putExtra("exit_on_sent", true);
startActivityForResult(smsIntent, MY_SMS_REQUEST_RESPONSE_CODE);
Note the startActivityForResult() method that indicates that we expect Android to return and the "exit_on_sent" extra, to force a swift return.
MY_SMS_REQUEST_RESPONSE_CODE is just any random code you select to recognize the returning result in the callback method (even if you currently do not expect any other returning results, you may have some in the future).
Next thing to do is to remove the second intent creation and queuing. In stead you implement the following callback method (added to this activity):
#Override
protected void onActivityResult(
int callbackIdentifier, int resultCode, Intent intent) {
// Is this the expected sendSMS callback ?
if (callbackIdentifier== MY_SMS_REQUEST_RESPONSE_CODE) {
if (resultCode == RESULT_OK) {
// Continue where you left off (e.g. execute intent 2)
Intent intent = new Intent(Main_Act.this, Sample.class);
startActivity(intent);
} else if (resultCode == RESULT_CANCELED) {
// Error handling/retrying etc
}
}
// Support inherited callback functions
super.onActivityResult(callbackIdentifier,resultCode,intent);
}
Note: if you want to pass data and type don't call method separately because will delete each other you must pass it in one method
wrong
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");
true
smsIntent.setDataAndType(Uri.parse("smsto:"),"vnd.android-dir/mms-sms");

Opening a new activity crashes my application

I do not know if this bug is in the code that I use to open the activity, or if it lies in the activity itself. This is the code for opening the activity, please tell me if I have an error in this, or if it is something else.
public void openGallery(View view){
Intent intent = new Intent(this, PhotoGallery.class);
startActivity(intent);
}

Categories