android- crashing application while switching between activities - android

I have edit the manifest file as well. and did everything i could do. still my code is crashing. please help me with this error..............................................................................................
Activity 1(MainActivity):
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent act2 = new Intent(view.getContext(),welcomemessage.class);
startActivity(act2);
}
});
Acitivity 2(welcomemessage):
super.onCreate(savedInstanceState);`
setContentView(R.layout.activity_welcomemessage);
TextView text = (TextView) findViewById(R.id.textView);
Button btn = (Button) findViewById(R.id.button4);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent act1 = new Intent(view.getContext(), MainActivity.class);
startActivity(act1);
}
});
manifestfile:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".welcomemessage"
android:label="#string/app_name">
</activity>
layouts name are: Layout 1(acitivity_main) and
layout 2(activity_welcomemessgae)

All java classes have to be capitalized, welcomemessage is not and in the mainfest it needs an intent filter block as well, if you post the logcat I can help track down any other errors

can you post the error log?
it will be easier to solve the error if we know it.
you can try these steps:
step 1:
clean the project
Project >> clean >> clean projects selected below >> choose your project
step 2:
try this one:
button3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,welcomemessage.class);
startActivity(intent);
}
});

Related

Start Activity from different Application (APK)

I have Two Applications AppOne & AppTwo
App Two has activity as the following:
<activity android:name=".AppTwoActivity">
<intent-filter>
<action android:name="com.myAction.TestAction"></action>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
What I should do to lunch AppTwoActivity from AppOne (Different App)?
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Intent
Intent intent = new Intent();
startActivity(intent);
}
});
Add the below code to the manifest of your App One. Replace the name with your second apps package name.
<queries>
<package android:name="second.app.package.name" />
</queries>
Then launch that activity using the code below. Or you can use #CommonsWare answer too.
Intent intent = new Intent();
intent.setClassName("second.app.package.name", "second.app.package.name.ActivityName");
startActivity(intent);
Use an Intent that will resolve to the other app's activity:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Intent
Intent intent = new Intent("com.myAction.TestAction");
try {
startActivity(intent);
} catch (Throwable t) {
Log.e("AppOne", "Exception starting AppTwo", t);
// TODO something to let the user know that the other app is not installed or we otherwise cannot start the activity
}
}
});

Ordering activities in Android Manifest

Having issues trying to order the some of my activities in my application. Have implemented some ideas that I have seen on SO, but to no avail.
At the minute, my application is going SplashScreen > MainActivty. I want SplashScreen > LoginActivity > MainActivity
Any indications on where I'm going wrong would be appreciated.
Manifest
<activity
android:name="com.example.XXX.myapplication.SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.XXX.myapplication.LoginActivity"
android:parentActivityName=".SplashScreen">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.XXX.myapplication.SplashScreen" />
</activity>
<activity
android:name=".SignUpActivity" />
<activity
android:name="com.example.XXX.myapplication.MainActivity"
android:parentActivityName=".LoginActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.XXX.myapplication.LoginActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar" />
</activity>
You don't order activities in the manifest like this. Set your splash screen to the default activity. In the Java code for splash activity start mainactivity with the startactivity method. Then in loginactivity call startactivity for mainactivity.
https://developer.android.com/training/basics/firstapp/starting-activity.html
You should do it programmatically in Java, in your SplashScreen Class, you should have something like :
startActivity(new Intent(SplashScreen.this, LoginActivity.class));
Example :
private final int SPLASH_DISPLAY_TIME = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent myIntent = new Intent(SplashScreen.this, LoginActivity.class);
startActivity(myIntent);
finish();
}
},SPLASH_DISPLAY_TIME);
}
Place and `Intent-filter` tag in your intended entry activity in AndroidManifest.xml file like this
```
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
```
If your initial activity is a place, start the next activity in your runnable thread as shown below.
```
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent(SplashActivity.this,ACTIVITY_TO_GO_TO.class);
startActivity(mainIntent);
finish();
}
}, TIME_OUT_NOW);
```
You can then move to anywhere else in ACTIVITY_TO_GO_TO activity.
You can not ordering Activities by Manifest
1.Your Launch page is Splash.So you can write below code in splash page
Thread timerThread = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
startActivity(new Intent(SplashScreenActivity.this,YourActivity.class));
finish();
}
}
};
timerThread.start();
You can write your Login Activity on plcae of YourActivity.
2.On Click Login button Ho to main Activity

Android Application with only Service

I need to make an application "x" that has only service in it(no activity and no broadcast receiver) which can be called from activity of another application "y". I need to ask is it possible because on clicking a button in application "y" service of application "x" must be called but it is not happening. I don't see service running in the android phone. So is it even possible to have a single service in a project and nothing else?
application "y" code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button);
if(button!=null){
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startService(new Intent("com.example.sonali.serviceonly.OnlyService"));
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();// Perform action on click
}
});
}
}
}
application "x" manifest file
``
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service android:name="com.example.sonali.serviceonly.OnlyService" android:exported="true" android:enabled="true">
<intent-filter>
<action android:name="com.example.sonali.serviceonly.OnlyService"/>
</intent-filter>
</service>
</application>
``
Yes it's possible.
You have to start service like this.
Intent intentService = new Intent();
intentService.setComponent(new ComponentName("com.example.app", "com.example.app.servicename"));
startService(intentService);
You must have to set this exported as true in menifest file.
android:exported="true"
I have tested it's working fine on 4.4.4 Version.
Try Explicit Intent for version 5+
ComponentName n = new ComponentName("com.xxx.yyy", "com.xxx.yyy.OnlyService");
Intent a = new Intent();
a.setComponent(n);
a.setAction(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_LAUNCHER);
a.putExtra("destinationAddress","5554");
ComponentName c = getApplication().startService(a);
if(c==null)
{
Log.e("error", "failed to start with " + a);
}

I am having issue with my splash screen as am not getting into the next activity

This is an example of splash screen but facing difficulty with it as I couldn't view my MainActivity class, couldn't be able to recognize the issue.Tried in manifest file by changing the action and the category name as well but could not be able to resolve to it.
Basically changed my Intent intent = new Intent(); as well but still the same goes on.
public class SplashActivity extends Activity
{
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
player = MediaPlayer.create(SplashActivity.this, R.raw.splash);
player.start();
Thread timer = new Thread()
{
#Override
public void run()
{
try
{
sleep(4000);
}catch(InterruptedException e)
{
e.printStackTrace();
}
finish();
Intent intent = new Intent();
intent.setClass(SplashActivity.this, MainActivity.class);
startActivity(intent);
stop();
}
};
timer.start();
}
#Override
protected void onPause()
{
super.onPause();
player.release();
finish();
}
}
====>And here is my manifest file --
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mainsplashcreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/android_splash"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SplashActivity"
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>
I would guess, that your problem is, that you finish your activity before you start the new one. If you call finish() on your activity, it will be destroyed, and so the startActivity() won't be called anymore (or if it will be called, it won't get a valid context anymore). So try to move the finish() method, at the end of your run method, that should solve the problem.
Another Very Short,Simple and Effective way of Implementing Flash Screen is as below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler=new Handler();
Runnable gotoMain=new Runnable() {
public void run()
{
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
};
handler.postDelayed(gotoMain, 4000);
}
You just Replace you OnCreate Method with this one.
There are a few things which you should take care of:
1. The usage of stop() and the place it is being used are not encouraged. Besides being deprecated, it will also give you UnsupportedOperationExcecption.
2. Is your MediaPlayer being initialized correctly without any errors? Check the logs.
3. What is the reason you are using finish() in onPause() method? It is not recommended.
4. You are assigning MAIN action to both your activities. While it is allowed, it should be for a specific reason.
However, all these things should not avoid your application to go to the main activity. There may be the reasons with your main activity code.

Linking two activity in android

I'm coding a simple android app where you write in a box your name then click ok and a new page will show your name... The problem is that when you click ok nothing happens.
Here the main activity
public class Click extends Activity implements OnClickListener{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String TypedText = (String)MyText.getText().toString();
Intent MyInt = new Intent(this, HelloWorld.class);
MyInt.putExtra("user", TypedText);
this.startActivity(MyInt);
Bundle Retrive = this.getIntent().getExtras();
Retrive.getString("user");
setContentView(R.id.Text);
TextView TextV = (TextView)findViewById(R.id.Text);
TextV.setText("user");
}
android.widget.EditText MyText;
public void OnCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.name_getter);
MyText = (EditText)this.findViewById(R.id.editText1);
this.findViewById(R.id.button1);
android.widget.Button RefBut = (Button)this.findViewById(R.id.button1);
RefBut.setOnClickListener(this);
}
And here the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.calpoly.android.lab1Sada"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Click"
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="HelloWorld" ></activity>
</application>
</manifest>
Starting the android emulator will launch the first activity click but then the app doesn't show the new view...
I dont know what you are doing but you can send/retrieve data from/to activity to another activity like this way:
For that you need to understand the concept of Intent.
From First activity:
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("name", "paresh");
i.putExtra("technology", "android");
startActivity(i);
From Second activity:
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String strName = extras.getString("name");
String strTechnology = extras.getString("technology");
Still for your reference, here is the article to know more about the same: Android Intents
You must pass the text from activity 1 and receive it as bundle in activity 2.
Go through the helloworld program as your first tutorial for android.
Just extending Activity will work. You do not need to mention the entire package of the superclass.

Categories

Resources