Second activity won't launch - java

I'm doing the Android Fundamentals 2.2 Coding Challenge and I'm unable to get the second activity to launch using the logic described in the preceding lessons.
Here is the code for my first activity:
package com.homing.a22codingchallenge;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
public static final String EXTRA_MESSAGE = "com.homing.mainactivity.extra.message";
public static final int TEXT_REQUEST = 1;
private TextView TV1, TV2, TV3, TV4, TV5, TV6, TV7, TV8, TV9, TV10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TV1 = findViewById(R.id.TV1);
TV2 = findViewById(R.id.TV2);
TV3 = findViewById(R.id.TV3);
TV4 = findViewById(R.id.TV4);
TV5 = findViewById(R.id.TV5);
TV6 = findViewById(R.id.TV6);
TV7 = findViewById(R.id.TV7);
TV8 = findViewById(R.id.TV8);
TV9 = findViewById(R.id.TV9);
TV10 = findViewById(R.id.TV10);
}
public void addItems(View view) {
Log.d(LOG_TAG, "Button clicked");
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(EXTRA_MESSAGE, "What?");
startActivityForResult(intent, TEXT_REQUEST);
Log.d(LOG_TAG, "startActivityForResult()");
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TEXT_REQUEST) {
if (resultCode == RESULT_OK) {
String reply = data.getStringExtra(SecondActivity.EXTRA_RETURN);
fillList(reply);
}
}
}
public void fillList(String string) {
String[] list = { TV1.toString(), TV2.toString(), TV3.toString(), TV4.toString(), TV5.toString(), TV6.toString(), TV7.toString(), TV8.toString(), TV9.toString(), TV10.toString() };
for (int i = 0; i < list.length; i++) {
list[i] = string;
}
}
}
Here is the code to my second activity:
package com.homing.a22codingchallenge;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
public class SecondActivity extends AppCompatActivity {
private Button BTN1, BTN2, BTN3, BTN4, BTN5, BTN6, BTN7, BTN8, BTN9, BTN10;
public static final String EXTRA_RETURN = "com.homing.22codingchallenge.secondactivity.extra.return";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
BTN1 = findViewById(R.id.BTN1);
BTN2 = findViewById(R.id.BTN2);
BTN3 = findViewById(R.id.BTN3);
BTN4 = findViewById(R.id.BTN4);
BTN5 = findViewById(R.id.BTN5);
BTN6 = findViewById(R.id.BTN6);
BTN7 = findViewById(R.id.BTN7);
BTN8 = findViewById(R.id.BTN8);
BTN9 = findViewById(R.id.BTN9);
BTN10 = findViewById(R.id.BTN10);
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
}
}
I've tried comparing the code with the project I was walked through in the guide and everything is consistent so far as I can see. Posts with issues similar to mine made a few suggestions that didn't make sense since my first project worked fine.
I've let up debug logs and confirmed in Logcat that the button is registering the click and it's even running through the block through the startActivityForResult() method.
There was one Logcat entry that seemed relevant, but searched didn't really yield anything helpful to me:
2018-10-18 07:01:37.386 1624-1677/system_process W/ActivityManager: Unable to start service Intent { act=com.google.android.gms.drive.ApiService.RESET_AFTER_BOOT flg=0x4 cmp=com.google.android.gms/.drive.api.ApiService (has extras) } U=0: not found
I've since tried to reproduce this error a number of times, but have not been able to. The only entries I'm seeing across my attempts are along the lines of the following:
2018-10-18 07:00:44.979 1369-1401/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 754681 , only wrote 603360
But I'm not sure that this is really related to the issue of launching the second activity.
Edit:
In response to some comments here is my manifest.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="22CodingChallenge"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/SecondActivity_name"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.homing.a22codingchallenge.MainActivity" />
</activity>
</application>

If your onActivityResult is reached that means the SecondActivity has been started. You have just to check what to do inside your second activity before finishing it. For example with the code above, you are calling finishing the activity at its creation.
Your code works, you have just to find the right place to place this code:
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
According to your logic.
I think this will help.

If the second Activity is not added to the AndroidManifest.xml, IDE will complaining that it is not added.
However, if you get: Button clicked in the logs, so main Activity has no problem but, check the codes in the second Activity:
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
As soon as it do the putExtra(), it actually finishes the Activity: finish(); after.

For what I see, I suppose you are just starting the SecondActivity in an onClick attribute in the activity_main layout, and, once in the SecondActivity, as soon as the onCreate happens, you are just calling finish() here:
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
So as soon as the activity is created, it is finished...
Maybe what you are trying to do is to return the button clicked text in the SecondActivity?
Something like:
BTN1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
}
});
Maybe you should add a onClick attribute to each button, and add a call to a method that will obtain the text of the button clicked like:
public void clickButton(View view) {
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, ((Button) view).getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
I haven't tried the code, but I hope this helps you!

Related

Android application swap between activitys stops

Hei guys. I am new to Android programming. I am trying to make an app that controls a rc car (Arduino). In this app are the next activities:
Button Commands
Tilt Command
Vocal Command
Instructions
About
When I open the app it suddenly stops and I don't know why. Can someone help me by looking at the code and tell me what I am doing wrong. Thanks a lot.
Button buttonCommand;
Button tiltCommand;
Button vocalCommand;
Button instructions;
Button about;
Button arduino; // Links Button
Button android; // Links Button
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
buttonCommand = (Button)findViewById(R.id.buttons);
tiltCommand = (Button)findViewById(R.id.tilt);
vocalCommand = (Button)findViewById(R.id.vocal);
instructions = (Button)findViewById(R.id.instructions);
about = (Button)findViewById(R.id.about);
arduino = (Button)findViewById(R.id.arduino);
android = (Button)findViewById(R.id.android);
}
public void onClickButton(View view){
Intent i = new Intent(getApplicationContext(),ButtonCommand.class); // ButtonCommand Activity
startActivity(i);
}
public void onClickTilt(View view){
Intent i = new Intent(getApplicationContext(),TiltCommand.class); // TiltCommand Activity
startActivity(i);
}
public void onClickVocal(View view){
Intent i = new Intent(getApplicationContext(),VocalCommand.class); // VocalCommand Activity
startActivity(i);
}
public void onClickInstructions(View view){
Intent i = new Intent(getApplicationContext(),Instructions.class); // Instructions Activity
startActivity(i);
}
public void onClickAbout(View view){
Intent i = new Intent(getApplicationContext(),About.class); // About Activity
startActivity(i);
}
public void onClickArduino(View view){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://forum.arduino.cc/"));
startActivity(intent);
}
public void onClickAndroid(View view){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://forum.xda-developers.com/"));
startActivity(intent);
}
If your app stops at the start, probably you are missing to add the registry of your activities into your AndroidManifest.xml :
<activity android:name=".ButtonCommand" />
<activity android:name=".TiltCommand" />
<activity android:name=".VocalCommand" />
<activity android:name=".Instructions" />
<activity android:name=".About" />
George, you need to create an OnClickListener for each button for example:
buttonCommand = (Button)findViewById(R.id.buttons);
buttonCommand.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),ButtonCommand.class); // ButtonCommand Activity
startActivity(i);
}
});
Plus: and the internet permission to open urls:
<uses-permission android:name="android.permission.INTERNET" />

Action on Click second button

I have 9 buttons, and I need to make 9 pages. Every button needs his own page to navigate to when clicked. 1 of them needs to navigate to a website instead of his own activity. So that one is done, and that works already.
Now I need to make an activity for a button so that it navigates to that on click. Once i've done that, I know how to do it and I can repeat that for the other buttons. I have the code listed below for whay my main activity looks like. That is the code for the button that navigates to the website. If any one you can refer me where to place the new code, and how to place it, would be very much appreciated. I'm pretty new to this so it might be an easy question for the lot of you.
I know that you have to change some code in the Manifest as well, but I think i can sort that one out, just the Mainactivity is the problem..
package com.example.rodekruis;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
public class MainActivity extends Activity {
private static Button button_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener() {
button_sbm = (Button)findViewById(R.id.button2);
button_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("https://www.rkz.nl/nieuws_agenda_nieuws");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
);
}
}
You can create a generic function to be trigger when the user click the button and check the id of the button clicked.
Example:
public void navigate(View v) {
int id = view.getId();
Intent intent;
if (id == R.id.button1) {
Uri.parse("https://www.rkz.nl/nieuws_agenda_nieuws1");
intent = new Intent(Intent.ACTION_VIEW, uri);
}
else if(id == R.id.button2) {
intent = new Intent(MainActivity.this, ActivityButton2.class);
}
else if(id == R.id.button3) {
intent = new Intent(MainActivity.this, ActivityButton3.class);
}
//Repeat for every button
startActivity(intent);
}
And set onclick attribute in the button calling this function.
Example:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:onClick="navigate" />
Declare your new activities in AndroidManifest.xml
Example:
<activity
android:name=".ActivityButton2"
android:label="ActivityButton2" >
</activity>
<activity
android:name=".ActivityButton3"
android:label="ActivityButton3" >
</activity>
I think you should take some time out and read Basics First app, specially Starting Activity
to answer your question, you'll need to declare 8 new activities (if each of button have a specific task) or 1 new activity (if all button leads to similar content different format or something)
so lets assume its the second case, so you create a new activity i.e. ActivityB (in android you have to register all the activities you are using in Manifest <activity android:name=".ActivityB"/>). to start that activity following is the code
Intent intent = new Intent(MainActivity.this, ActivityB.class);
startActivity(intent);
so in terms of your code it should be something like below
public class MainActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
findViewById(R.id.button8).setOnClickListener(this);
findViewById(R.id.button9).setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.button1:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button2:
Uri uri = Uri.parse("https://www.rkz.nl/nieuws_agenda_nieuws");
intent = new Intent(Intent.ACTION_VIEW, uri);
break;
case R.id.button3:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button4:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button5:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button6:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button7:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button8:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
case R.id.button9:
intent = new Intent(MainActivity.this, ActivityB.class);
break;
}
startActivity(intent);
}}
firstly you have to create new WebViewActivity and in this xml class put these code
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
and in your new activity class put this code.
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.rkz.nl/nieuws_agenda_nieuws");
}
now open this WebViewActivity On button click.
public void OnClickButtonListener() {
button_sbm = (Button)findViewById(R.id.button2);
button_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
}
);
}

Android Studio app crashes

I have a problem with my application I am making on Android Studio. I don't know why, but after the splash screen, the application crashes. I want the activity "Accueil" to open after the splash screen. It worked fine last week and now it doesn't anymore. I didn't touch anything. I show you the manifest, and the .java files.
Splashscreen.java:
import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class Splashscreen extends Activity {
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
Thread splashTread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
StartAnimations();
Thread loading = new Thread() {
public void run() {
try {
sleep(5000);
Intent main = new Intent(Splashscreen.this,Menu.class);
startActivity(main);
finish();
} catch (Exception e) {
e.printStackTrace();
} finally {
finish();
}
}
};
loading.start();
}
private void StartAnimations() {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
anim.reset();
LinearLayout l = (LinearLayout) findViewById(R.id.lin_lay);
l.clearAnimation();
l.startAnimation(anim);
anim = AnimationUtils.loadAnimation(this, R.anim.rotate);
anim.reset();
ImageView iv = (ImageView) findViewById(R.id.splash);
iv.clearAnimation();
iv.startAnimation(anim);
splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while (waited < 3500) {
sleep(100);
waited += 100;
}
Intent intent = new Intent(Splashscreen.this,Menu.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
}
}
};
}
}
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thibaudmangeot.erdfapplicationsecurite">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity android:name=".Splashscreen"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Accueil"
android:label="#string/title_activity_accueil"/>
</application>
</manifest>
Accueil.java:
package com.example.thibaudmangeot.erdfapplicationsecurite;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.view.View;
public class Accueil extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accueil);
Button buttonfis = (Button) findViewById(R.id.buttonfis);
buttonfis.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goTosignin();
}
});
}
private void goTosignin() {
Intent intent = new Intent(this, Menu.class);
startActivity(intent);
}
}
Do this
Intent main = new Intent(Splashscreen.this, Accueil.class);
startActivity(main);
finish();
Instead of
Intent main = new Intent(Splashscreen.this, Menu.class);
startActivity(main);
finish();
You have not even mentioned Menu activity in your manifest.
This is your error:
Intent main = new Intent(Splashscreen.this,Menu.class);
it should be:
Intent main = new Intent(Splashscreen.this, Accueil.class);
I think your application is crashing maybe because of two reasons
There is no Activity named "Menu"
Menu Activity is not mentioned in manifest file
If you want to open "Accueil" activity then write your intent in following fashion.
Intent openAccueil = new Intent(Splashscreen.this,Menu.class);
startActivity(openAccueil);

Android: No Activity found to handle Intent (trying to add an activity to exsisting app)

Ok so I am very new to the Android programming, I am starting week 2 of this class and cannot for the life of me figure out what is going wrong. I have read/watched tons of tutorials on adding new Activities and nothing works.
Assignment: Use the Activities app and add a fourth activity
My activity is simple, 3 buttons and an image. One button makes the image visible and the other makes it invisible. Third returns back to the main.
Note: I edited the original app to have buttons on Main Activity because it had me hitting center on the d-pad which I found dumb. Another note is that Activity 2 & 3 use the same layout and do basically the same thing from what I can tell
public class MainActivity extends Activity {
String tag = "Events";
int request_Code = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---hides the title bar---
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Log.d(tag, "In the onCreate() event");
Button act2Butt = (Button) findViewById(R.id.act2Butt);
Button act3Butt = (Button) findViewById(R.id.act3Butt);
Button act4Butt = (Button) findViewById(R.id.act4Butt);
act2Butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivityForResult(new Intent("net.learn2develop.ACTIVITY2"), request_Code);
}
});
act3Butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivityForResult(new Intent("net.learn2develop.ACTIVITY2"), request_Code);
}
});
act4Butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivityForResult(new Intent("net.learn2develop.MYACTIVITY"), request_Code);
}
});
}
/*
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
//startActivity(new Intent("net.learn2develop.ACTIVITY2"));
//startActivity(new Intent(this, Activity2.class));
startActivityForResult(new Intent(
"net.learn2develop.ACTIVITY2"),
request_Code);
}
return false;
}
*/
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,data.getData().toString(),
Toast.LENGTH_LONG).show();
}
}
}
public void onStart()
{
super.onStart();
Log.d(tag, "In the onStart() event");
}
public void onRestart()
{
super.onRestart();
Log.d(tag, "In the onRestart() event");
}
public void onResume()
{
super.onResume();
Log.d(tag, "In the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag, "In the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag, "In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag, "In the onDestroy() event");
}
public class MyActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity4);
Button yesButt = (Button) findViewById(R.id.yesButton);
Button noButt = (Button) findViewById(R.id.noButton);
Button finButt = (Button) findViewById(R.id.finButton);
final ImageView img1 = (ImageView) findViewById(R.id.image1);
yesButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
img1.setVisibility(View.VISIBLE);
}
});
noButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
img1.setVisibility(View.INVISIBLE);
}
});
finButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent data = new Intent();
data.setData(Uri.parse("OMG IT WORKS"));
setResult(RESULT_OK, data);
finish();
}
});
}
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
String defaultName="";
Bundle extras = getIntent().getExtras();
if (extras!=null)
{
defaultName = extras.getString("Name");
}
//---get the EditText view---
EditText txt_username =
(EditText) findViewById(R.id.txt_username);
txt_username.setHint(defaultName);
//---get the OK button---
Button btn = (Button) findViewById(R.id.btn_OK);
//---event handler for the OK button---
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
Intent data = new Intent();
//---get the EditText view---
EditText txt_username =
(EditText) findViewById(R.id.txt_username);
//---set the data to pass back---
data.setData(Uri.parse(
txt_username.getText().toString()));
setResult(RESULT_OK, data);
//---closes the activity---
finish();
}
});
}
I have entered the code for Main Activity, My Activity (the one I made), and Activity 2. My Activity runs great and does exactly what I want it to but if I try to access it from main it dies.
928-928/net.learn2develop.Activities E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.learn2develop.Activities, PID: 928
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=net.learn2develop.MYACTIVITY }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
at android.app.Activity.startActivityForResult(Activity.java:3736)
at android.app.Activity.startActivityForResult(Activity.java:3697)
at net.learn2develop.Activities.MainActivity$3.onClick(MainActivity.java:48)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
This code is for my last attempt and making it work before throwing my hands up. Last thing I did was make My Activity act like the other and use startActivityForResult.
Any help would help greatly. I don't know if it matters or not but I do not have a .class for My Activity in the bin directory but there is one for all the others.
If you need any more info please just ask.
Like I said before I'm really new to the whole Android area.
Edit: Manifest
<activity android:name=".MyActivity"
android:label="My Activity">
<intent-filter>
<action android:name="net.learn2develop.MYACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You may need to add the Activity to the manifest. If you are sure you have done this, I would recommend using an Intent slightly differently.
Try using an Intent the following way:
Intent intent = new Intent(MyActivity.this, SecondActivity.class);
startActivityForResult(intent, requestCode);
The first parameter of this Intent is the current Activity. The second parameter is theActivity you wish to navigate to. Handling an Intent this way prevents a small typo in the package name which would throw that exception. As long as the Activity you are trying to navigate to is in the manifest and you have set up your Intent like the code above, everything should work fine.
Good luck and happy coding!
you need to add your activity to your manifest
<activity
android:name=".MySecondActivity"
android:label="#string/title_second_activity">
</activity>
net.learn2develop.MYACTIVITY
Android is not finiding the above activity like other engineers said,add this activity in your manifest file so JVM can find which class you are referring to.

Going back to previous activity using ActionBar navigation

In my app, I model Lists of items. In MainActivity, I see ListView containing Lists. I can click on each list to see its items (DisplayListActivity). On DisplayListActivity, I have button in the ActionBar to display the list properties. This launches the third activity (EditListPropertiesActivity).
Main
| ^
V |
DisplayListActivity (listId is passed with Intent, default=1)
| ^
V |
EditListPropertiesActivity (listId is passed with Intent, default=1)
The problem appears, when I select List id=2 on the MainActivity, and then I select the properties button on the DisplayListActivity. Once I am done with the EditListPropertiesActivity, i click '<' (back) on the ActionBar: .
I return to the DisplayListActivity, but instead of going back to the list id=2, I see the list with id=1 (which is default).
How to pass the ListId back form EditListPropertiesActivity to the DisplayListActivity?
startActivityForResult and return the id - would work, but I see it ugly
use the SharedPreferences and store sth like lastVisitedList - ugly
store the lastVisitedList information in the db - even uglier
What is the usual solution for that problem?
Code snippets below.
MainActivity
public class MainActivity extends Activity {
...
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MetaList mlist = (MetaList) listView.getItemAtPosition(i);
final Intent intent;
intent = new Intent(getApplicationContext(), DisplayListActivity.class);
intent.putExtra(META_LIST_SELECTED, mlist.getId());
startActivity(intent);
}
}
);
...
DisplayListActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list);
intent = getIntent();
metaListId = intent.getLongExtra(MainActivity.META_LIST_SELECTED, 1); //read the data or take 1 as default
...
//start the third activity
final Intent intent;
intent = new Intent(getApplicationContext(), EditListPropertiesActivity.class);
intent.putExtra(META_LIST_SELECTED, metaListId);
startActivity(intent);
....
EditListPropertiesActivity
public class EditListPropertiesActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_list_parameters);
getActionBar().setDisplayHomeAsUpEnabled(true); //this enables the '<' (back) button
intent = getIntent();
metaListId = intent.getLongExtra(DisplayMetaListActivity.META_LIST_SELECTED, 1);
...
Manifest
<application>
<activity
android:name=".MainActivity">
</activity>
<activity
android:name="com.example.tutorial1.DisplayListActivity"
android:parentActivityName="com.example.tutorial1.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tutorial1.MainActivity" />
</activity>
<activity
android:name="com.example.tutorial1.EditListPropertiesActivity"
android:parentActivityName="com.example.tutorial1.DisplayListActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tutorial1.DisplayListActivity" />
</activity>
</application>
try this to finish an activity
Intent intent = new Intent();
intent.putExtra(EXTRA, value);
setResult(RESULT_OK, output);
finish();
and this for getting the result in the previous activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data.getExtras().containsKey(EXTRA)){
// do stuff with data from finished activity
String bla = data.getStringExtra(EXTRA)
}
}
EDIT: read comments! need to use startActivityForResult
try to call finish(); that you should call on destroy of the current activity and display the previous activity.
I have found the solution that works. I post it here, because it is cleaner (in my opinion) and different than the other proposed. I set the launch mode of the second activity to singleTask in the manifest file: android:launchMode="singleTask"
...
<activity
android:name="com.example.tutorial1.DisplayListActivity"
android:label="Meta List"
android:parentActivityName="com.example.tutorial1.MainActivity"
android:launchMode="singleTask">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tutorial1.MainActivity" />
</activity>
...
If there is a better solution for the stated problem, I am open for discussion :)

Categories