using of intent for linking activities - android

I want to link my two activites by clicking on a button i have written the following code
public class IHBCAPTUREActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
ImageView iv;
TextView tx;
Button b1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)findViewById(R.id.imageView1);
tx=(TextView)findViewById(R.id.textView1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent calIntent;
**calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);**
startActivity(calIntent);
}
}
error cumes in this line
calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);
for LoginActivity.class that
LoginActivity cannot be resolved to a type . How to solve it?

here is the simple code how you can load one activity to another,
i have created two activities like this
FirstActivity.java
package com.rdc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button btnload = (Button) findViewById(R.id.btn);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
SecondActivity.java
package com.rdc;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
and my manifest file is
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".FirstActivity"
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=".SecondActivity"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
try to implement this in your app and let me know if still any doubt is there.

Check whether you have declared LoginActivity in manifest file as:
<activity
android:name="your.package-name.LoginActivity" >
</activity>

You don't have anything wrong in code.
Its know issue that occurs randomly.
solution is to set the target to other API level in project->properties->android, then set it back.then clean you project once.
I think this will refresh the .classpath or some other files, not sure, but it works.

Related

Android Exception : unable to find explicit activity

I am new in android and when i run application i got this exception:
android.content.ActivityNotFoundException: Unable to find explicit
activity class {com.example.activity/com.example.activity.Second};
have you declared this activity in your AndroidManifest.xml?
so when i want to view an activity from anther activity i got this error.
(I used Lynda to learn android and my sdk version is 24.0.2 , adt is 23.0.6 , and use eclips mars)
main activity java code:
package com.example.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
second activity java code :
package com.example.activity;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
manifest XML Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
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=".second"></activity>
</application>
</manifest>
by the way , when i change my main activity java code like below.everything works fine :
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.second);
}
});
}
}
Android is case-sensitive. Replace:
<activity android:name=".second"></activity>
with:
<activity android:name=".Second"></activity>
In your manifest
<activity android:name=".Second"></activity>
Try this.

call activity in another package

I've been reading through some of the other posts about calling an activity in another package and it looks like I'm doing it right but still getting this error from my main activity:
AndroidTestProj1/src/com/testing/androidtest/TestProj1Activity.java:7: error: package com.testing.androidtest2 does not exist
I've declared the other activity in my manifest but it still cannot find it.
====================================
Here's AndroidTestProj1:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testing.androidtest"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" >
<activity android:name="TestProj1Activity"
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="com.testing.androidtest2.TestProj2Activity"
android:label="#string/app2_name" >
</activity>
</application>
and AndroidTestProj1/src/com/testing/androidtest/TestProj1Activity.java:
package com.testing.androidtest;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import com.testing.androidtest2.TestProj2Activity;
public class TestProj1Activity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// do some stuff here then call other package activity
Intent i = new Intent(this, TestProj2Activity.class);
startActivity(i);
}
}
Here's AndroidTestProj2: AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testing.androidtest2"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" >
<activity android:name="TestProj2Activity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
and AndroidTestProj2/src/com/testing/androidtest2/TestProj2Activity.java:
package com.testing.androidtest2;
import android.app.Activity;
import android.os.Bundle;
import com.testing.androidtest2.Helper;
public class TestProj2Activity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private boolean somekeyvalue = Helper.getSomeKeyValue();
// do stuff with keyvalue
}
And AndroidTestProj2/src/com/testing/androidtest2/Helper.java:
package com.testing.androidtest2;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class Helper {
private static final String SOMEKEY = "somekey";
private static SharedPreferences prefs;
public static void init(Context context)
{
prefs = PreferenceManager.getDefaultSharedPreferences(context);
initialize();
}
private static void initialize()
{
SharedPreferences.Editor editor = prefs.edit();
if (!prefs.contains(SOMEKEY)) editor.putBoolean(SOMEKEY, false);
editor.commit();
}
public static boolean getSomeKeyValue()
{
return prefs.getBoolean(SOMEKEY, true);
}
}
It is the same thing which you do in normal scenario but with a little tweak in it. In FirstActivity where ever you want to call other activity from other package, put following code
code
Intent i = new Intent();
i.setClassName("com.CodeArt.finalactivity", "com.CodeArt.finalactivity.FinalActivity");
startActivity(i);
Here com.CodeArt.finalactivity is the package name and com.CodeArt.finalactivity.FinalActivity is full class name.
Now go to the AndroidManifest.xml of the FirstActivity and Add following line
activity android:name=”com.CodeArt.finalactivity.FinalActivity” in application tag.
It will work fine.
Just an assumption, but have you already add your second project into your current one? Otherwise you cannot reach out its class even though you declare them in your manifest.
To know how to achieve that you can check here

Android Step 1 from Root Developer Page Activity not Working

I attempted to run "Clean Code" according to this link, however nothing is happening. From being a Renegade developer my intuition says "Button onClick" is missing but I could be wrong. Here is my code written according to what the instructions are:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class BuildingaSimpleUserInterfaceApi8Activity extends Activity {
public final static String EXTRA_MESSAGE = "com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/**This is how I work with Buttons and the Next Activity, however this method IS NOT defind
* in the very first Android Tutorial for sending information onClick.
Button bButtonone = (Button) findViewById(R.id.button_send);
bButtonone.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("com.developer.android.com_training_basics_firstapp_building_ui.DISPLAYMESSAGEACTIVITY"));
}
});
}
*/
/** Called when the user selects the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.developer.android.com_training_basics_firstapp_building_ui"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BuildingaSimpleUserInterfaceApi8Activity"
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="com.developer.android.com_training_basics_firstapp_building_ui.DisplayMessageActivity" />
</application>
</manifest>
It looks like you forgot the line to start the activity:
public void sendMessage(View view) {
...
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent); // add me!
}
I read through the code at the site listed and you have missed one line:
startActivity(intent);
at the end od sendMessage.

Stopping an Activity in Android

I had read about this thread around the Internet but unlucky to find a solution. The solutions that are available did not work for me like adding the android:noHistory="true" in the Manifest file. What I wanted is that after the user clicks the play game button, which will redirect the user into the select difficulty page, when the user clicks the back button (android emulator), it will go back to the main menu.
This is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kfc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".NewKFCActivity"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelpPageOne"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.HELPPAGEONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".HelpPageTwo"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.HELPPAGETWO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SelectDifficulty"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.SELECTDIFFICULTY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
This is my main activity:
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
public class NewKFCActivity extends Activity {
/** Called when the activity is first created. */
ImageButton bPlay, bHelp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
bPlay = (ImageButton) findViewById(R.id.playGame);
bPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), SelectDifficulty.class);
startActivityForResult(intent,0);
finish();
}
});
bHelp = (ImageButton) findViewById(R.id.Help);
bHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), HelpPageOne.class);
startActivityForResult(intent,0);
finish();
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
This is my difficulty class:
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class SelectDifficulty extends Activity{
ImageButton bEasy, bMedium, bHard;
#Override
protected void onCreate(Bundle kfcState) {
// TODO Auto-generated method stub
super.onCreate(kfcState);
setContentView(R.layout.difficultyxml);
bEasy = (ImageButton) findViewById(R.id.easy);
bEasy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent intent = new Intent(v.getContext(), NewKFCActivity.class);
//startActivityForResult(intent,0);
}
});
}
}
First of all, you should know the difference between startActivity() and startActivityForResult(), see Android developers website.
Second, I think you should need to understand the lifecycle of an Activity, you don't need to finish() every activity once you leave it, Android will manage that for you.
Hope it helps :)
Cheers!
Remove the lines:
finish();
after
startActivityForResult(...);
in your Activity NewKFCActivity because finish() is destroying your activity and when you press back, there is no more an Activity to go back to!

getting exception when starting new activity on an android

i have read a lot about this but didnt come up with a solution.
this is my first android project.
i am trying yo open an activity from my main activity.
i have doubled check the names and the configuration files, but the startactivity() procedure fails.
debbuger shows runtime errror.
as u will c, the startactivity is called upon onclick event.
i am really stuck here dont know what else to do.
thanks a lot!
* this is the main activity **
package com.example.helloandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
private OnClickListener gtScoringListener = new OnClickListener() {
public void onClick(View v) {
Intent mintent = new Intent(HelloworldActivity.this ,imgScoring.class);
startActivity(mintent);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(gtScoringListener);
}
}
* this is the target activity: *
package com.example.helloandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class imgScoring extends Activity {
private OnClickListener backBtnListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(imgScoring.this, HelloworldActivity.class);
startActivity(intent);
}
};
private OnClickListener scoreBtnListener = new OnClickListener() {
public void onClick(View v){
TextView tv = (TextView)findViewById(R.id.editText1);
tv.setText(((Button)v).getText());
}
};
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button01);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button03);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button02);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button04);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.button3);
button.setOnClickListener(backBtnListener);
setContentView(R.layout.imgscoring);
}
}
this is the androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="12" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".imgScoring"></activity>
<activity android:name=".HelloworldActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You should've posted the stacktrace, but I assume the error is on this line in your target activity:
button.setOnClickListener(scoreBtnListener);
This will give you a NULL:
Button button = (Button)findViewById(R.id.button1);
because you haven't set a contentview in THAT activity, so there is nothing for findViewById() to return. You'll get a nullpointer when calling that setOnclickLIstener()
Try change class name from imgScoring to ImgScoring everywhere. Java is not allowing class name to start from small letter ;-)
And place setContentView(R.layout.imgscoring); just after your super.onCreate();

Categories

Resources