Search code examples
javaandroidandroid-intentandroid-activityforceclose

Android app force closes when the button is clicked to go on another activity


I am making an android app using android 2.2 and eclipse. Its a simple app which should change activities from "CoverPageApp.java" to "LoginActivity.java" and then to another activity. But as I click the "Start Button" in CoverPageApp.java, the app force closes.

LoginActivity.java

import android.app.Activity;
import android.content.Context;
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 LoginActivity extends Activity {

    Button btnLinkToRegistrScrn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     setContentView(R.layout.login);
     addListenerOnButton();
     btnLinkToRegistrScrn = (Button) findViewById(R.id.LinkToRegisterScreen); 

   }


    public void addListenerOnButton() {

        final Context context2 = this;

        Button loginbtn1 = (Button) findViewById(R.id.btnLogin);

            loginbtn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context2, DashboardActivity.class);
                         context2.startActivity(intent);   
            }

        });
    }


    { 
        // Link to Register Screen 
        btnLinkToRegistrScrn.setOnClickListener(new View.OnClickListener() { 
           @Override
            public void onClick(View view) { 
                Intent i = new Intent(getApplicationContext(), CAActivity.class); 
                startActivity(i); 
                } 
       }); 

    }   

   }

LogCat

05-31 12:37:08.620: D/AndroidRuntime(629): Shutting down VM
05-31 12:37:08.620: W/dalvikvm(629): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-31 12:37:08.630: E/AndroidRuntime(629): FATAL EXCEPTION: main
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.app.Activity.startActivityForResult(Activity.java:2817)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.app.Activity.startActivity(Activity.java:2923)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.view.View.performClick(View.java:2408)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.view.View$PerformClick.run(View.java:8816)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.os.Handler.handleCallback(Handler.java:587)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.os.Looper.loop(Looper.java:123)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-31 12:37:08.630: E/AndroidRuntime(629):  at java.lang.reflect.Method.invokeNative(Native Method)
05-31 12:37:08.630: E/AndroidRuntime(629):  at java.lang.reflect.Method.invoke(Method.java:521)
05-31 12:37:08.630: E/AndroidRuntime(629):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-31 12:37:08.630: E/AndroidRuntime(629):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-31 12:37:08.630: E/AndroidRuntime(629):  at dalvik.system.NativeStart.main(Native Method)

Solution

  • I think you have not added the activity

    LoginActivity.java
    

    in Android manifest file Just add followin code to your AndroidManifest.xml file

    <application>
            <activity
                android:name=".LoginActivity"
                android:label="@string/login_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.HOME" />
                </intent-filter>
            </activity>
        </application>