-6

Here is the manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.paidquery.mobile">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SignUpActivity"
            android:label="@string/title_activity_sign_up"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
    </application>

</manifest>

This is login Activity java file, where I am writing on click function and this is also my first activity. I see both the console log in my console but whenever do click on signup button, Application go close itself and give the error in console which says couldn't find sign up activity.

package com.paidquery.mobile;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class LoginActivity extends AppCompatActivity{

    public static final String LOGIN_TAG = "login_msg";
    private static Button sign_up_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        onClickSignUpBtnListner();
        Log.i(LOGIN_TAG, "Login onCreate function");
    }

    /** Sign up button click function */

    public void onClickSignUpBtnListner() {

        Log.i(LOGIN_TAG,"In onClickSignUpBtnListner call");
        sign_up_btn = (Button)findViewById(R.id.signUpBtn);

        sign_up_btn.setOnClickListener(
            new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
                    startActivity(intent);
                }
            }
        );

    }
}

Here is latest console log error when i click on sign up button of login activity.

01-13 16:25:18.844 28104-28104/com.paidquery.mobile E/AndroidRuntime: FATAL EXCEPTION: main Process: com.paidquery.mobile, PID: 28104 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paidquery.mobile/com.paidquery.mobile.SignUpActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference at android.support.v7.widget.ToolbarWidgetWrapper.(ToolbarWidgetWrapper.java:98) at android.support.v7.widget.ToolbarWidgetWrapper.(ToolbarWidgetWrapper.java:91) at android.support.v7.app.ToolbarActionBar.(ToolbarActionBar.java:73) at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:205) at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99) at com.paidquery.mobile.SignUpActivity.onCreate(SignUpActivity.java:17) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Vikas Rana
  • 33
  • 2
  • 9

3 Answers3

2

Please read carefully Intent constructors.

You're using Intent (String action) (new Intent("com.paidquery.mobile.SignUpActivity")) to start your second activity.

Androdi will assume that com.paidquery.mobile.SignUpActivity is an Intent action Filter,which is not available in your manifest.xml file.

Change your code to:

Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(intent);
Rami
  • 7,879
  • 12
  • 36
  • 66
  • Unable to start activity ComponentInfo{com.paidquery.mobile/com.paidquery.mobile.SignUpActivity} – Vikas Rana Jan 13 '16 at 10:14
  • Try to remove tag (with his actions) for the SignUpActivity in manifest. – Rami Jan 13 '16 at 10:17
  • Removed. Main post updated with latest code. Same Error : Unable to start activity ComponentInfo{com.paidquery.mobile/com.paidquery.mobile.SignUpActivity} – Vikas Rana Jan 13 '16 at 10:28
  • Update the complete console log in main post in bottom. – Vikas Rana Jan 13 '16 at 10:57
  • Its another problem **NullPointerException**, i think your Toolbar object is null so when you call *Toolbar.getTitle()* you get the NullPointerException. – Rami Jan 13 '16 at 11:03
  • Actually in SignUpActivity, when I do remove this code it do work. otherwise not and give the same error in console. "Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);" – Vikas Rana Jan 13 '16 at 11:16
  • It's another problem, you should open a new question to not confuse future users. – Rami Jan 13 '16 at 12:02
0

As stated in comments to your OP, we need code.

However, this is the most common way to start an Activity from an Activity.

Assume the following :

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button sign_up_btn = (Button) findViewById(R.id.sign_up_btn);
        sign_up_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // start activity sign up here :
                Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
                startActivity(intent);
            }
    }
}

However, also as stated in the comments, you have to declare every Activities in your AndroidManifest.xml file.

Inside the <application> </application> scope, your SignUpActivity as follows :

<activity android:name="SignUpActivity"/>

Make sure that you write the name of the package that contains this activity if you placed it in a specific package. No need to write down the your application package as it should be declared in the manifest as well.

Make also sure that there is a proper import inside your calling activity (in my example, the MainActivity) but Android Studio should help you with that.

[UPDATE 1]

Given your crash log and your comments, the error is that your ToolBar inside your SignUpActivity does not exist so when you try to find it returns null.

This is because the them of your SignUpActivity declared in your AndroidManifest file is set to remove the ToolBar :

<activity [...] android:theme="@style/AppTheme.NoActionBar"> </activity>

Therefore you should use another theme. To do so, modify your styles.xml file where AppTheme is defined and change for something like this :

<style name="AppTheme" parent="Base.Theme.AppCompat.DarkActionBar">

And voilà you can now use the ToolBar in your SignUpActivity.

Mackovich
  • 3,319
  • 6
  • 35
  • 73
  • As explained in my answer : 1) no need to specifiy the package that contains your **SignUpActivity** if it is the same as stated in the beginning of your **AndroidManifest**. Please remove `"com.paidquery.mobile"`. 2) The way you create the **Intent** is used for actions wich is not what you want to do. Try as follows : `Intent intent = new Intent(LoginActivity.this, SignUpActivity.class)`. It should work as stated in my answer. – Mackovich Jan 13 '16 at 10:14
  • I have updated the main post wit current code again. Not working, Error : Unable to start activity ComponentInfo{com.paidquery.mobile/com.paidquery.mobile.SignUpActivity} – Vikas Rana Jan 13 '16 at 10:34
  • Update the complete console log in main post in bottom. – Vikas Rana Jan 13 '16 at 10:58
  • Thank you. Can you also post the content of the **onCreate()** method of your `SignUpActivity` ? and also, please place the stack error code in SO code for better readability – Mackovich Jan 13 '16 at 11:04
  • Actually in SignUpActivity, when I do remove this code it do work. otherwise not and give the same error in console. "Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);" – Vikas Rana Jan 13 '16 at 11:15
  • If I remove that code, it remove my toolbar from signup activity. what to do if I want to keep the same toolbar. – Vikas Rana Jan 13 '16 at 11:18
  • Well the problem is that the **ToolBar** is null (hence the nullpointer exception in you crash log) because the **theme** of your `SignUpActivity` is `@style/AppTheme.NoActionBar` which removes the ToolBar in this Activity. That's all! If you want to keep the toolbar, change this activity theme so it uses the ToolBar ;-) – Mackovich Jan 13 '16 at 11:28
0

Its crashes because SignUpActivity.java line no 17. .

I think you are trying to set custom actionbar / toolbar but which is not mention in your second xml (for SignUpActivity).